有 Java 编程相关的问题?

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

导致其他组件不显示的java绘制方法

我有一个简单的GUI程序,我正在尝试开始工作。当用户按下底部按钮时,我正在尝试绘制一些形状。当我在paint()中去掉if(buttonClicked)时,一切都显示得很好,但paint()似乎是自动执行的,并且形状显示时没有任何按钮单击。当我使用if(buttonClicked)添加环绕paint()主体时,无论ButtonHandler类如何处理它,我的其余组件甚至不会显示在框架中。我不知道为什么会这样。在paint()中使用和不使用if逻辑测试代码,您将看到发生了什么

 public class GUI extends JFrame {

      //declare components
     Container container;
     JPanel centerPanel, northPanel, southPanel, eastPanel, westPanel, mouseClickPanel;
     JLabel topLabel;
     JTextArea textArea;
     JButton buttonA, buttonB, drawButton;
     boolean buttonClicked;

     public GUI(String title) {

         super(title); // invoke JFrame Constructor

         container = getContentPane();
         container.setLayout(new BorderLayout());

         centerPanel = new JPanel();
         centerPanel.setBackground(Color.CYAN);
         northPanel = new JPanel();
         northPanel.setBackground(Color.GREEN);
         southPanel = new JPanel();
         southPanel.setBackground(Color.MAGENTA);
         eastPanel = new JPanel();
         eastPanel.setBackground(Color.ORANGE);
         westPanel = new JPanel();
         westPanel.setBackground(Color.YELLOW);
         mouseClickPanel = new JPanel();
         mouseClickPanel.setBackground(Color.GRAY);
         mouseClickPanel.setPreferredSize(new Dimension(400, 400));

         topLabel = new JLabel("Press either button to make something happen");
         topLabel.setFont(new Font("Calibri", Font.PLAIN, 16));
         topLabel.setHorizontalAlignment(JLabel.CENTER);

         textArea = new JTextArea(3, 20);
         textArea.setEditable(false);
         buttonA = new JButton("Press Here");
         buttonB = new JButton("Press Here");
         drawButton = new JButton("Press here");

         container.add(centerPanel, BorderLayout.CENTER);
         container.add(northPanel, BorderLayout.NORTH);
         container.add(southPanel, BorderLayout.SOUTH);
         container.add(eastPanel, BorderLayout.EAST);
         container.add(westPanel, BorderLayout.WEST);

         northPanel.add(topLabel, BorderLayout.NORTH);
         centerPanel.add(buttonA);
         centerPanel.add(textArea);
         centerPanel.add(buttonB);
         centerPanel.add(mouseClickPanel);
         centerPanel.add(drawButton);

         buttonClicked = false;
         ButtonHandler buttonHandler = new ButtonHandler(buttonA, drawButton, textArea, buttonClicked);



         buttonA.addActionListener(buttonHandler); // add actionListeners to buttonA and B
         buttonB.addActionListener(buttonHandler);
         drawButton.addActionListener(buttonHandler);

         setSize(525, 600);
         setVisible(true);
     }



     public void paint(Graphics g) {


        if (buttonClicked) {
            super.paint(g);

            g.setColor(Color.RED);
            g.fillOval(150, 150, 50, 50);
            g.draw3DRect(200, 200, 50, 50, true);
        }
     }
 }

处理程序类:

 public class ButtonHandler extends JFrame implements ActionListener {

     private JButton buttonA, drawButton;
     private JTextArea textArea;
     boolean buttonClicked;

 public ButtonHandler(JButton buttonA, JButton drawButton, JTextArea textArea, boolean buttonClicked) {
    this.buttonA = buttonA;
    this.textArea = textArea;
    this.drawButton = drawButton;
    this.buttonClicked = buttonClicked;

}

@Override
public void actionPerformed(ActionEvent e) {

    if (e.getSource() == buttonA) {
        textArea.setText(" <- You pressed left button");
    }
    else if (e.getSource() == drawButton) {

        textArea.setText("You pressed button to draw rectangle");
        buttonClicked = true;
        repaint();
    }
    else {
        textArea.setText("You pressed right button ->");
    }
}
}

共 (1) 个答案

  1. # 1 楼答案

    从if语句中去掉super.paint(g);。把它作为第一行。否则,除非点击按钮,否则根本不会进行绘制(包括JPanel内部构件,如背景)