我正在构建一个在JPanel上绘制random (User input) rectangles的程序。

问题一:


每当我在JTextfield中键入数字时,我需要单击两次
JBUtton显示为Rectangles


问题2:


当我在JTextField中输入新数字时,该数字不
显示矩形,但显示我之前输入的矩形。


码:

private void init() {

    final int FRAME_WIDHT = 800;
    final int FRAME_HEIGHT = 1000;
    int input = 3;

    JFrame frame = new JFrame();
    frame.setSize(FRAME_WIDHT, FRAME_HEIGHT);
    frame.setTitle("Frame");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    west = new JPanel();
    west.setSize(500, 500);
    west.setBorder(BorderFactory.createLineBorder(Color.black));

    east = new JPanel();
    east.setSize(300, 1000);

    button = new JButton("Add squares");
    field = new JTextField(10);
    button.setSize(100, 50);
    east.add(button);
    east.add(field);
    east.setBorder(BorderFactory.createLineBorder(Color.black));

    button.addActionListener(new java.awt.event.ActionListener() {

        @Override
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            JButton1ActionPerformed(evt);
        }

        public void JButton1ActionPerformed(ActionEvent evt) {

            int aantalRect = Integer.parseInt(field.getText());

            MyDrawing draw = new MyDrawing(aantalRect);

            west.add(draw);
            draw.revalidate();
            draw.repaint();

        }
    });

    frame.add(west, BorderLayout.CENTER);
    frame.add(east, BorderLayout.EAST);

    frame.setResizable(true);
    frame.setVisible(true);

}

public static void main(String[] a) {

    P1027 form = new P1027();

}

}

class MyDrawing extends JPanel {

int input = 0;

public MyDrawing(int i) {

    this.input = i;

}

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Random r = new Random();

    setPreferredSize(new Dimension(500, 1000));

    for (int i = 0; i < input; i++) {

        int x = r.nextInt(460);
        int y = r.nextInt(960);

        g.drawRect(x, y, 40, 40);

    }

}


Can any one tell me how to fix that?

最佳答案

问题1:您第一次看到MyDrawing JPanel时没有看到正方形,因为您实际上是在按this answer的解释覆盖setPreferredSize(...)方法时调用了getPreferredSize()方法>。它们也有可能在屏幕外绘制。将MyDrawing的首选高度设置为1000,这不适用于my laptop's screen(绿线是MyDrawing的边框)。

要解决问题1,请覆盖方法,并在必要时降低首选高度:

class MyDrawing extends JPanel {

    ... //Constructor

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(500, 500); //Changed from 1000 to 500
    }

    ... //paintComponent(...)
        //If you change 1000 to 500, don't forget to change 960 to 460 too
}


问题2:您看到以前在JTextField中键入的矩形数量,原因是:


您忘记了在添加新的MyDrawing之前将其从west删除。
您应该在其父组件revalidate()上调用repaint()drawwest上。


要解决问题2,请从MyDrawing中删除​​旧的west,添加新的revalidate(),然后调用repaint()T

...
public void JButton1ActionPerformed(ActionEvent evt) {
    west.removeAll(); //If the old MyDrawing is the only thing
                      //that has been added to west. Otherwise use
                      //remove(int index) or remove(Component comp)
    west.add(draw);
    west.revalidate();
    west.repaint();
}
...


其他事情:


您在H中切换了FRAME_WIDTHJButton1ActionPerformed(...)
您可以将actionPerformed中的代码放入实际的JFrame方法中。
前面提到的setSize(...)westeastthat answer上的button调用和不调用的情况下,您的看起来完全相同,建议不要使用这些方法,因此请考虑删除它们。

10-08 15:21