为什么在repaint()之后不会显示正方形?

我早些时候发布了这个问题,并被告知将其设为SSCCE(如果我可以进行任何改进,随时告诉我):
我想知道为什么当单击我的按钮“确认”时,旧的正方形消失并且重新绘制的正方形没有出现在我的GUI(用摆动制作)上. Squares类绘制了200个隔开的正方形,这些正方形的ID是从不同类中获得的(ID为0、1、2或3,作为字符串)(出于这个问题的目的,我们假设它始终为0,不包括该类).为了澄清起见:Squares第一次可以完美地绘制所有内容(还可以检索正确的ID),但是我希望它在单击带有新ID的按钮后能够重新绘制所有内容.
正方形代码:

import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.util.ArrayList;

public class Squares extends JPanel{

private ArrayList<Rectangle> squares = new ArrayList<Rectangle>();
private String stringID = "0";

public void addSquare(int x, int y, int width, int height, int ID) {
      Rectangle rect = new Rectangle(x, y, width, height);
      squares.add(rect);
      stringID = Integer.toString(ID);
      if(ID == 0){
          stringID = "";
      }
}

   @Override
   protected void paintComponent(Graphics g) {

  super.paintComponent(g);
  Graphics2D g2 = (Graphics2D) g;
  FontMetrics fm = g2.getFontMetrics();
  int fontAscent = fm.getAscent();
  g2.setClip(new Rectangle(0,0,Integer.MAX_VALUE,Integer.MAX_VALUE));
  for (Rectangle rect : squares) {
     g2.drawString(stringID, rect.x + 7, rect.y + 2 + fontAscent);
     g2.draw(rect);
  }
 }
}

GUI代码:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class GUIReserver extends JFrame implements Runnable{

private int myID;
private JButton confirm = new JButton("Check Availability and Confirm Reservation");    
private JFrame GUI = new JFrame();
private Squares square; 

public GUIReserver(int i) {
    this.myID = i;
}

@Override
public void run() {
    int rows = 50;
    int seatsInRow = 4;
    confirm.addActionListener(new ActionListener() {
        @Override
         public void actionPerformed(ActionEvent evt) {
                GUI.getContentPane().remove(square);
                square = new Squares();
                int spaceNum = 0;
                int rowNum = 0;
                int offsetX = 200;
                int offsetY = 0;
                for(int i = 0; i < rows * seatsInRow; i++){
                    square.addSquare(rowNum * 31 + offsetX,spaceNum * 21 + 50 + offsetY,20,20, 0); //normally the 4th parameter here would retrieve the ID from the main class
                    rowNum++;
                    if(rowNum == 10){
                        rowNum = 0;
                        spaceNum++;
                    }
                    if(spaceNum == 2){
                        spaceNum = 3;
                        rowNum = 0;
                    }
                    if(spaceNum == 5){
                        spaceNum = 0;
                        offsetY += 140;
                    }
                }
                GUI.getContentPane().add(square); //this does not show up at all (could be that it wasn't drawn, could be that it is out of view etc...)
                GUI.repaint(); //the line in question
         }          
    });
    GUI.setLayout(new FlowLayout());
    GUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    GUI.setLocation(0,0);
    GUI.setExtendedState(JFrame.MAXIMIZED_BOTH);
    square = new Squares();
    int spaceNum = 0;
    int rowNum = 0;
    int offsetX = 200;
    int offsetY = 0;
    for(int i = 0; i < rows * seatsInRow; i++){
        square.addSquare(rowNum * 31 + offsetX,spaceNum * 21 + 50 + offsetY,20,20, 0); //normally the 4th parameter here would retrieve the ID from the main class
        rowNum++;
        if(rowNum == 10){
            rowNum = 0;
            spaceNum++;
        }
        if(spaceNum == 2){
            spaceNum = 3;
            rowNum = 0;
        }
        if(spaceNum == 5){
            spaceNum = 0;
            offsetY += 140;
        }
    }
    GUI.getContentPane().add(square); //this shows up the way I wish
    GUI.add(confirm);
    GUI.pack();
    GUI.setVisible(true);
}
}

主要代码:

public class AircraftSeatReservation {

static AircraftSeatReservation me = new AircraftSeatReservation();
private final int rows = 50;
private final int seatsInRow = 4;
private int seatsAvailable = rows * seatsInRow;
private Thread t3;

public static void main(String[] args) {
    GUIReserver GR1 = new GUIReserver(3);
    me.t3 = new Thread(GR1);
    me.t3.start();
}
}

解决方法:

一个主要问题:Squares JPanels的首选大小仅为20 x 20,实际上可能是该大小,因为它似乎已添加到使用FlowLayout的容器中.接下来,您似乎要在远远超出此组件范围的位置绘制图形,因此这些图形很可能永远都看不到.考虑允许您的Squares对象更大,并确保仅在此组件的边界内绘制.

注意,还有一些没有意义的代码,包括:

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类(希望您在该类中确实没有静态方法).

而且我们无法编译或运行您的当前代码.我们不想看到您的整个程序,但是您应该将代码压缩为仍可编译的最小位,没有与您的问题无关的额外代码,但仍然可以演示您的问题.因此,如安德鲁·汤普森(Andrew Thompson)所建议,为了获得更好的帮助,请创建并发布您的Minimal, Complete, and Verifiable exampleShort, Self Contained, Correct Example.

我将尝试尽可能地OOP化您的问题,以便您分而治之.这可能涉及:

>创建一个SeatClass枚举,其中一个可能包含两个元素:FIRST和COACH.
>创建一个非GUI Seat类,其中包含几个字段,可能包括:int行,char座位(例如A,B,C,D,E,F),SeatClass字段,以查看它是头等舱座位还是教练和一个布尔值保留字段,仅当保留座位时才为true.
>此类还具有getId()方法,该方法返回行号和位子char的String串联.
>创建一个非GUI飞机类,其中一个包含两排座椅,一个用于SeatClass.FIRST或头等舱座位,一个用于SeatClass.COACH.
>它还将具有一个行数字段和一个座位数(列数)字段.
>创建所有这些之后,然后在您的GUI类上工作.
>我将为Seats创建一个GUI类,也许是GuiSeat,使其包含一个Seat对象,或者使其扩展JPanel,允许它显示从其包含的Seat对象获取的自己的id字符串,并覆盖getBackground(. ..),因此其颜色将取决于座位是否预留.
>等…..

上一篇:Java Graphics2D浮点数精确的drawOval替代方法?


下一篇:java-旋转矩形并将其在sin波中移动-使用graphics2D进行帮助