Why wont squares show up after repaint()?

前端 未结 1 1319
挽巷
挽巷 2021-01-16 06:18

I posted this question a bit earlier and was told to make it SSCCE so here goes (if I can make any improvements feel free to let me know): I\'m wondering why when my button

相关标签:
1条回答
  • 2021-01-16 07:03

    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; // ???
    

    I'm guessing that it's

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

    And this won't compile for us:

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

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

    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.


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

    • 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.....
    0 讨论(0)
提交回复
热议问题