简体   繁体   English

重绘时JButton复制了吗?

[英]JButton copied when repainting?

I have a JFrame with 2 JPanel in it: a PaintPanel (with a paint() method) and a ButtonPanel (with buttons). 我有一个带有2个JPanelJFrame :一个PaintPanel (带有paint()方法)和一个ButtonPanel (带按钮)。 When I invoke the repaint() of the PaintPanel (but clicking the button) the button of the ButtonPanel is being painted in the PaintPanel ! 当我调用PaintPanelrepaint() (但单击按钮)时, ButtonPanel的按钮正在PaintPanel It isn't clickable or anything, it is just there. 它不是可点击的或任何东西,它就在那里。

I tried to recreate the problem with this code: 我尝试使用此代码重新创建问题:

public class Main {

    public static void main(String[] args) {
        JFrame frame = new JFrame("frame");
        frame.setSize(400,400);
        frame.setLayout(new GridLayout(2,1));
        PaintPanel paint = new PaintPanel();
        ButtonPanel buttons = new ButtonPanel(paint);
        frame.add(paint);
        frame.add(buttons);
        frame.setVisible(true);
    }
}

public class PaintPanel extends JPanel{
    public void paint(Graphics g){
        g.drawRect(10, 10, 10, 10);
    }
}

public class ButtonPanel extends JPanel implements ActionListener{

    private PaintPanel paintPanel;

    public ButtonPanel(PaintPanel paintPanel){
        this.paintPanel=paintPanel;
        JButton button = new JButton("button");
        button.addActionListener(this);
        add(button);
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        paintPanel.repaint();           
    }
}

This sould recreate the problem I have (sorry for the odd code markings, can't seem to get it right). 这可以重现我的问题(对于奇怪的代码标记感到遗憾,似乎无法正确)。

I really hope one of you knows what is happening here because i don't... 我真的希望你们中的一个人知道这里发生了什么,因为我不...

First of all, you should override paintComponent() instead of paint() . 首先,你应该覆盖paintComponent()而不是paint() It's part of the best practices in Swing when it comes to do some panel customization. 在进行一些面板定制时,它是Swing最佳实践的一部分。

Secondly, here is the code that works for me (I don't know why yours doesn't though :S): 其次,这是适合我的代码(我不知道为什么你的代码不是:S):

public class Main {

    public static void main(String[] args) {

        JFrame frame = new JFrame("frame");
        frame.setSize(400, 400);
        // frame.setLayout(new GridLayout(2, 1));
        PaintPanel paint = new PaintPanel();
        ButtonPanel buttons = new ButtonPanel(paint);
        // frame.add(paint);
        // frame.add(buttons);
        frame.setVisible(true);

        JPanel pan = new JPanel(new BorderLayout());
        pan.add(paint);
        pan.add(buttons, BorderLayout.SOUTH);
        frame.add(pan);

    }
}

class PaintPanel extends JPanel {

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(new Color(new Random().nextInt()));
        g.drawRect(10, 10, 10, 10);
    }
}

class ButtonPanel extends JPanel implements ActionListener {

    private final PaintPanel paintPanel;

    public ButtonPanel(PaintPanel paintPanel) {

        this.paintPanel = paintPanel;
        JButton button = new JButton("button");
        button.addActionListener(this);
        add(button);
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        if (getParent() != null) {
            getParent().repaint();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM