且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么在重绘()之后不会出现方块?

更新时间:2023-02-05 23:37:43

一个主要问题:你的Squares JPanels首选大小只有20乘20,实际上可能是那个大小,因为它似乎被添加到FlowLayout使用容器中。接下来,您似乎正在绘制超出此组件边界的位置,因此可能永远不会看到图纸。考虑允许Squares对象更大,并确保仅在此组件的范围内绘制。

One major problem: Your Squares JPanels preferred size is only 20 by 20, and will likely actually be that size since it seems to be added to a FlowLayout-using container. Next you seem to be drawing at locations that are well beyond the bounds of this component, and so the drawings likely will never be seen. Consider allowing your Squares objects to be larger, and make sure to only draw within the bounds of this component.

请注意,代码没有意义,包括:

Note also there is code that doesn't make sense, including:

private int myID;
private JTextField row, column, instru draft saved // ???
package question2;ction1, instruction2, seatLabel, rowLabel; // ???

我猜它是

private int myID;
private JTextField row, column, instruction1, instruction2, seatLabel, rowLabel;

这不会为我们编译:

int rows = AircraftSeatReservation.getRows();
int seatsInRow = AircraftSeatReservation.getSeatsInRow(); // and shouldn't this take an int row parameter?

因为我们没有你的AircraftSeatReservation类(希望你真的没有静态方法) ()

since we don't have your AircraftSeatReservation class (hopefully you don't really have static methods in that class).

我们无法编译或运行您当前的代码。我们不希望看到您的整个程序,而是您应该将代码压缩到仍然编译的最小位,没有与您的问题无关的额外代码,但仍然可以证明您的问题。正如Andrew Thompson所建议的,为了获得更好的帮助,请创建并发布您的最小,完整和可验证的示例简短,自包含,正确的例子

And we can't compile or run your current code. We don't want to see your whole program, but rather you should condense your code into the smallest bit that still compiles, has no extra code that's not relevant to your problem, but still demonstrates your problem. So as Andrew Thompson recommends, for better help, please create and post your Minimal, Complete, and Verifiable example or Short, Self Contained, Correct Example.

我会尽可能地尝试OOP-ify你的问题,让你分而治之。这可能涉及:

I would try to OOP-ify your problem as much as possible, to allow you to divide and conquer. This could involve:


  • 创建一个 SeatClass 枚举,一个可能有两个元素, FIRST和COACH。

  • 创建一个非GUI Seat类,包含多个字段,包括:int行,char座位(如A,B,C,D,E,F) ,一个SeatClass字段,用于查看它是否是头等舱座位或教练,以及一个布尔保留字段,仅当座位被保留时才为真。

  • 此类还有一个getId(返回行号和座位字符串的字符串连接的方法。

  • 创建一个非GUI飞机类,一个包含两个座位数组,一个用于SeatClass.FIRST或第一个-class座位,以及一个SeatClass.COACH。

  • 它还有一个行计数字段和一个座位数(列数)字段。

  • 创建所有这些后,然后处理你的GUI类。

  • 我为Seats创建了一个GUI类,也许是GuiSeat,让它包含一个Seat对象,也许它可以扩展JPanel,允许它 要显示它自己从包含的Seat对象获取的id字符串,让它覆盖 getBackground(...),这样它的颜色将取决于座位是保留还是没有。

  • 等.....

  • Creating a SeatClass enum, one with possibly two elements, FIRST and COACH.
  • Creating a non-GUI Seat class, one with several fields including possibly: int row, char seat ( such as A, B, C, D, E, F), a SeatClass field to see if it is a first class seat or coach, and a boolean reserved field that is only true if the seat is reserved.
  • This class would also have a getId() method that returns a String concatenation of the row number and the seat char.
  • Creating a non-GUI Airplane class, one that holds two arrays of Seats, one for SeatClass.FIRST or first-class seats, and one for SeatClass.COACH.
  • It would also have a row count field and a seat count (column count) field.
  • After creating all these, then work on your GUI classes.
  • I'd create a GUI class for Seats, perhaps GuiSeat, have it contain a Seat object, perhaps have it extend JPanel, allow it to display its own id String that it gets from its contained Seat object, have it override getBackground(...) so that it's color will depend on whether the seat is reserved or not.
  • etc.....