repaint() not invoking paintComponent()?



你好,我正在尝试解决以下问题:编写一个程序,提示用户使用文本字段输入中心点和半径的x和y位置。当用户单击"绘制"按钮时,在零部件中绘制一个具有该圆心和半径的圆。我看不出我的代码有什么问题,但有一点是因为它看起来不像是重新绘制()在调用paintComponent(),因为消息将变为TESTING 1,而不是TESTING 2,并且没有绘制。我的代码:

import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.applet.*;
import java.awt.geom.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class q3{
    public static class cgPanel extends JPanel{
        private static double x;
        private static double y;
        private static double r;
        private static JTextField xField;
        private static JTextField yField;
        private static JTextField rField;
        private static JButton draw;
        private static JLabel message;
//This is all just Layout work.
        public cgPanel(){
            setLayout(new BorderLayout());
            JPanel drawPanel = new JPanel();
            drawPanel.setBackground(Color.WHITE);
            add(drawPanel, BorderLayout.CENTER);
            message = new JLabel("");
            JPanel sub1ForSub1 = new JPanel();
            sub1ForSub1.add(message);
            JLabel coordinates = new JLabel("Coordinates:");
            JPanel sub2ForSub1 = new JPanel();
            sub2ForSub1.add(coordinates);
            JPanel subPanel1 = new JPanel();
            subPanel1.setLayout(new GridLayout(2, 1));
            subPanel1.add(sub1ForSub1);
            subPanel1.add(sub2ForSub1);
            JLabel xLabel = new JLabel("x:");
            xField = new JTextField(4);
            JLabel yLabel = new JLabel(" y:");
            yField = new JTextField(4);
            JLabel rLabel = new JLabel(" Radius:");
            rField = new JTextField(4);
            JPanel subPanel2 = new JPanel();
            subPanel2.add(xLabel);
            subPanel2.add(xField);
            subPanel2.add(yLabel);
            subPanel2.add(yField);
            subPanel2.add(rLabel);
            subPanel2.add(rField);
            draw = new JButton("Draw");
            ActionListener bL = new ButtonListener();
            draw.addActionListener(bL);
            JPanel subPanel3 = new JPanel();
            subPanel3.add(draw);
            JPanel Panel = new JPanel();
            Panel.setLayout(new BorderLayout());
            Panel.add(subPanel1, BorderLayout.NORTH);
            Panel.add(subPanel2, BorderLayout.CENTER);
            Panel.add(subPanel3, BorderLayout.SOUTH);
            add(Panel, BorderLayout.SOUTH);
            setVisible(true);
        }
        static class ButtonListener extends JComponent implements ActionListener{
            public void actionPerformed(ActionEvent e){
                try{
                    String xString = xField.getText();
                    String yString = yField.getText();
                    String rString = rField.getText();
                    message.setText("TESTING 1");
                    x = Double.parseDouble(xString);
                    y = Double.parseDouble(yString);
                    r = Double.parseDouble(rString);
                    repaint();
                }
                catch (NumberFormatException exception){
                    message.setText("Please enter a number.");
                }
            }
//This is where I cant seem to get the code in paintComponent to run when the Draw button is pressed.
            public void paintComponent(Graphics g){
                Graphics2D g2 = (Graphics2D) g;
                Ellipse2D.Double circle = new Ellipse2D.Double(x - r, y - r, r*2, r*2);
                g2.draw(circle);
                message.setText("TESTING 2");
            }
        }
    }
    public static void main(String[] args){
        JFrame frame = new JFrame();
        frame.setSize(800, 800);
        frame.setTitle("Circle Generator");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        cgPanel panel = new cgPanel();
        frame.add(panel);
        frame.setVisible(true);
    }
}

所以,有几件事。

您的问题源于这样一个事实,即您的ButtonListener正在扩展一个JComponent,因此repaint()方法正在为ButtonListen调用该方法(它实际上不是JComponet)。

paintComponent方法也适用于ButtonListener。

相反,您希望您的按钮监听器能够访问您的cgPanel,这样它就可以告诉it重新绘制。您的paintComponent需要移动到cgPanel,但即使这样,您也可能不希望它出现在那里,因为cgPanel上有很多其他组件。

从您的代码中还不清楚您真正希望在哪里绘制圆。

您可能应该创建一个CirclePanel来扩展JPanel,并重写paintComponent来绘制圆,然后将其添加到cgPanel中。然后让ButtonListener告诉CirclPanel实例重新绘制。

相关内容

  • 没有找到相关文章

最新更新