有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java为什么我需要单击两次才能让JButton正常工作?

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

问题1:

Whenever I type a number in my JTextfield,I need to click twice on the JBUtton for Rectangles to show up.

问题2:

When i type a new number in the JTextField the number of that don't show the rectangle but it shows the rectangles I typed in previous.

代码:

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) 个答案

  1. # 1 楼答案

    问题1:您第一次没有看到在MyDrawing{}上绘制正方形,因为您正在调用setPreferredSize(...)方法,而此时您真的应该覆盖getPreferredSize()方法,正如this answer所解释的。它们也有可能从屏幕上消失。将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中键入的矩形数量,因为:

    • 在添加新的west之前,您忘记从MyDrawing中删除先前添加的MyDrawing
    • 当您应该在其父组件west上调用它时,您正在revalidate()上调用repaint()

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

    ...
    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();
    }
    ...
    

    其他事项

    • 您在FRAME_WIDTH中切换了TH
    • 您可以将JButton1ActionPerformed(...)中的代码放入实际的actionPerformed方法中
    • 您的^ { }看起来完全相同,并且没有调用^ {< CD25> },在^ {< CD10> }、^ {CD27>}和^ {CD28>}和^ {A3}之前,我建议不使用这些方法,因此考虑删除它们。<李>