paintComponent() 未被 actionListener 激活

paintComponent() not activatedby actionListener

我目前正在为学校编写一段代码,但遇到了严重的困难。所以我想做的是通过一个 JTextField,让用户输入 "rektangel" 或 "cirkel" 然后按下一个按钮。之后,程序应该打印出一个矩形或一个圆形。

所以我将一个 ActionListener 连接到按钮,它又激活了一个方法来决定用户想要打印什么。因此,我尝试通过创建我拥有的画家 Class 的实例来访问 paintComponent 方法。但是,它似乎不起作用,因为它从不打印任何东西。

抱歉,我的描述很差,但我真的迷路了,除了可能与 repaint() 相关外,我真的不知道问题出在哪里;未激活 paintCompontent。

这是我的代码,所以如果有人能帮助我找到问题,我将不胜感激。

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; 
public class FigurRitare {
public String Figur = " ";
public Boolean activator = false;
public int draw = 0;
private JPanel mainPanel;
private JButton registreraButton;
private JTextField textField1;
private JPanel DrawingBord;

public FigurRitare() {
    registreraButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            String inputString = textField1.getText();
            decider(inputString);
        }
    });
}

public static void main(String[] args) {
    //Skapa ditt fönster
    JFrame frame = new JFrame("Hello World!");
    //Tala om att du vill kunna stänga ditt förnster med krysset i högra hörnet
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //Ange storleken på ditt fönster och att det ska vara fast
    frame.setSize(500, 600);
    frame.setResizable(false);
    //Positionera ditt fönster i mitten av skärmen
    frame.setLocationRelativeTo(null);

    //Skapa en instans av din den här klassen som hanterar din panel
    FigurRitare myForm = new FigurRitare();
    //Lägg in din panel i programfönstret
    frame.setContentPane(myForm.mainPanel);
    //Visa programfönstret på skärmen
    frame.setVisible(true);

}

public void decider(String input) {
    ritPanel rita = new ritPanel();
    String Cirkel = "cirkel";
    String Rektangel = "rektangel";
    if (input.matches(Cirkel)) {
        rita.whatToDraw(1);
        textField1.setText("");
        textField1.requestFocusInWindow();
    } else if (input.matches(Rektangel)) {
        rita.whatToDraw(2);
        Figur = Rektangel;
        textField1.setText("");
        textField1.requestFocusInWindow();
    } else {
        JOptionPane.showMessageDialog(mainPanel, "Vänligen ange antingen cirkel eller rektangel i rutan ovanför");
        textField1.setText("");
        textField1.requestFocusInWindow();
    }
}

private void createUIComponents() {
    // TODO: place custom component creation code here
    DrawingBord = new ritPanel();
}

class ritPanel extends JPanel {

    public void whatToDraw(int toDraw) {
        draw = toDraw;
        repaint();
    }

    @Override
    public void paintComponent(Graphics grafik) {
        super.paintComponent(grafik);
        if (draw == 2) {
            grafik.fillRect(100, 100, 100, 100);
        }
        if (draw == 1) {
            grafik.fillOval(100, 100, 100, 100);
        }
    }
}

对于非常迟到的回答,我深表歉意,但这是代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; 
public class FigurRitare
{
    public String Figur = " ";
    public Boolean activator = false;
    public int draw = 0;
    private JPanel mainPanel;
    private JButton registreraButton;
    private JTextField textField1;
    private JPanel DrawingBord;

    public FigurRitare() {
        registreraButton = new JButton("Submit");
        mainPanel = new JPanel();
        textField1 = new JTextField();

        mainPanel.setLayout(new GridLayout(2, 1));

        registreraButton.addActionListener(new ActionListener()
            {
                @Override
                public void actionPerformed(ActionEvent e) {
                    String inputString = textField1.getText();
                    decider(inputString);
                }
            });

        mainPanel.add(registreraButton);
        mainPanel.add(textField1);
    }

    public static void main()
    {
        //Skapa ditt fönster
        JFrame frame = new JFrame("Hello World!");
        //Tala om att du vill kunna stänga ditt förnster med krysset i högra hörnet
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //Ange storleken på ditt fönster och att det ska vara fast
        frame.setSize(500, 600);
        frame.setResizable(false);
        //Positionera ditt fönster i mitten av skärmen
        frame.setLocationRelativeTo(null);

        //Skapa en instans av din den här klassen som hanterar din panel
        FigurRitare myForm = new FigurRitare();
        frame.setLayout(null);
        //Lägg in din panel i programfönstret
        frame.setContentPane(myForm.mainPanel);
        //Visa programfönstret på skärmen
        frame.setVisible(true);
    }

    public void decider(String input)
    {
        ritPanel rita = new ritPanel();
        if (input.equalsIgnoreCase("Cirkel")) {
            JFrame NewFrame = new JFrame("Cirkel");
            NewFrame.setVisible(true);
            NewFrame.setSize(500, 500);
            NewFrame.add(rita);
            rita.whatToDraw(1);
            textField1.setText("");
            textField1.requestFocusInWindow();
        } else if (input.equalsIgnoreCase("Rektangel")) {
            JFrame NewFrame = new JFrame("Rektangel");
            NewFrame.setVisible(true);
            NewFrame.setSize(500, 500);
            NewFrame.add(rita);
            rita.whatToDraw(2);
            textField1.setText("");
            textField1.requestFocusInWindow();
        } else {
            JOptionPane.showMessageDialog(mainPanel, "Vänligen ange antingen cirkel eller rektangel i rutan ovanför");
            textField1.setText("");
            textField1.requestFocusInWindow();
        }
    }

    private void createUIComponents() {
        // TODO: place custom component creation code here
        DrawingBord = new ritPanel();
    }

    class ritPanel extends JPanel {

        public void whatToDraw(int toDraw) {
            draw = toDraw;
            repaint();
        }

        @Override
        public void paintComponent(Graphics grafik) {
            super.paintComponent(grafik);
            if (draw == 2) {
                grafik.fillRect(100, 100, 100, 100);
            }
            if (draw == 1) {
                grafik.fillOval(100, 100, 100, 100);
            }
        }
    }
}

如评论中所述,如果您的代码按原样执行,将抛出 NullPointerException。为了避免这种情况,您应该在构造函数中初始化所有成员变量,这里是 FigureRitare().

我已经删除了一些不必要的变量,例如String RektangelString Cirkel,但除此之外,原始代码保持不变。

我已经将 mainPanel 的布局设置为 GridLayout 两行一列,只是为了更好的视觉吸引力:)
请记住将必要的组件(如 JButton 和 JTextField)添加到面板中,如下所示:mainPanel.add(registreraButton) 以便它们 确实出现 在面板上。

另一件事是您成功地在 JPanel 上绘制了形状,但是您没有将面板添加到任何 JFrame。我冒昧地使用:

JFrame NewFrame = new JFrame("Cirkel");
NewFrame.setVisible(true);
NewFrame.setSize(500, 500);
NewFrame.add(rita);

对于这两种情况,用户输入的是'rektangel'还是'cirkel'。

最后,我删除了您用来检查输入的 input.matches(),并将其替换为 input.equalsIgnoreCase(),后者检查输入是否与 "rektangel" 相同或"cirkel", 输入是否大写(case-insensitive)