Java:按下按钮时无法渲染矩形

问题描述 投票:0回答:1

我试图通过按按钮在我的面板中添加一个Rectangle,然后再将其删除。它应该可以工作,但是什么也不能渲染,我绝对不知道为什么。我现在搜索了几个小时...我逐渐感到沮丧:(有人可以解释我做错了什么,并给我一些好的建议,我可以用我的代码改进吗?

谢谢您的回答!

public class GUI extends JPanel {

    public static boolean isRecVisible = false;

    public static void main(String[] args) {

    createGui();

}
    @Override
    protected void paintComponent(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        super.paintComponent(g);
        g2d.drawRect(10, 10, 200, 200);
    }

    public static void createGui() {

        int frameWidth = 550;
        int frameHeight = 400;

        int buttonWidth1 = 250;
        int buttonHeight1 = 30;

        int buttonWidth2 = 500;
        int buttonHeight2 = 30;

        int displayWidth = frameWidth - 20;
        int displayHeight = frameHeight - 105;

        GUI drawRec = new GUI();

        JFrame f = new JFrame("Rectangle");
        JPanel p = new JPanel();
        JPanel display = new JPanel();
        JButton addRec = new JButton("Add Rectangle");
        JButton removeRec = new JButton("Remove Rectangle");
        JButton colorRec = new JButton("Color Rectangle");

        f.add(p);
        p.add(addRec);
        p.add(removeRec);
        p.add(colorRec);
        p.add(display);

        display.setBackground(Color.LIGHT_GRAY);

        f.setSize(frameWidth, frameHeight);
        f.setLocationRelativeTo(null);
        f.setResizable(false);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);

        display.setBounds(frameWidth / 2 - displayWidth / 2, 10, displayWidth, displayHeight);
        addRec.setBounds(frameWidth / 2 - buttonWidth1 / 2 - 250 / 2, frameHeight - 85, buttonWidth1, buttonHeight1);
        removeRec.setBounds(frameWidth / 2 - buttonWidth1 / 2 + 250 / 2, frameHeight - 85, buttonWidth1, buttonHeight1);
        colorRec.setBounds(frameWidth / 2 - buttonWidth2 / 2, frameHeight - 60, buttonWidth2, buttonHeight2);

        addRec.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (isRecVisible == false) {
                    isRecVisible = true;
                    display.add(drawRec);
                    display.repaint();
                    System.out.println("Rectangle has been drawn!");

                } else {
                    System.out.println("Rectangle has already been drawn!");
                    return;
                }
            }
        });

        removeRec.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (isRecVisible == true) {
                    isRecVisible = false;
                    System.out.println("Rectangle has been removed!");

                } else {
                    System.out.println("Rectangle has already been removed");
                    return;
                }
            }
        });
    }
}
java swing awt
1个回答
0
投票
display.add(drawRec);
display.repaint();

当您将组件添加(或删除)到可见框架时,基本逻辑是:

display.add(...);
display.revalidate();
display.repaint(); // sometimes needed

revalidate()是关键方法,因为它调用布局管理器,因此可以设置组件的大小/位置。

但是,由于您的自定义面板没有首选的尺寸,因此仍然无法解决问题,因此您的组件没有任何内容。

您需要重写自定义面板的getPreferredSize()方法,以返回自定义组件的首选大小。因此,根据您的情况,您可以将首选大小设置为(220,220),以便矩形位于面板的中心。

© www.soinside.com 2019 - 2024. All rights reserved.