repaint()JPanel

时间:2015-01-30 23:46:46

标签: java swing jpanel

我目前正在尝试了解paintComponent()repaint()方法,但我不能。我有一个带按钮的面板。我想要做的是当我单击按钮清除面板(删除按钮)并可能调整它的大小,并添加JTextField,但我根本不知道在{{1方法。

paintComponent()

这是MadProgrammer发布后我的mousePressed事件的代码:

public class Test extends JFrame{
public Test(){
    ImageIcon image = new ImageIcon("Buton.png");
    JLabel buton = new JLabel(image);
    JPanel panel = new JPanel();

    buton.addMouseListener(new MouseListener(){
        public void mousePressed(MouseEvent e){
            ImageIcon image = new ImageIcon("Buton-Pressed.png");
            buton.setIcon(image);
            remove(panel);
            revalidate();
            repaint();

        }

    panel.setOpaque(true);
    panel.setBackground(Color.BLACK);
    panel.add(buton);
    add(panel);

public static void main(String[] args) {
    JFrame frame = new Test();
    frame.getContentPane().setBackground(Color.BLACK);
    frame.pack();
    frame.setVisible(true);
    frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public class Paint extends JPanel{
    protected void paintComponent(Graphics g){
        super.paintComponent(g);
    }
}
}

但是我收到了这个错误,在window.pack():

buton.addMouseListener(new MouseListener(){
        public void mousePressed(MouseEvent e){
            ImageIcon image = new ImageIcon("Buton-Pressed.png");
            buton.setIcon(image);
            removeAll();
            add(new JTextField("Big text field"));

           Window window = SwingUtilities.getWindowAncestor(Test.this);
            window.pack();
            window.setLocationRelativeTo(null);
        }

1 个答案:

答案 0 :(得分:1)

  1. paintComponent与组件管理没有任何关系,不应该用于在UI中添加或删除组件
  2. MouseListener是与JButton一起使用的错误监听器,用户可以使用键盘,键盘快捷键和鼠标以及以编程方式触发按钮,而不是使用ActionListener
  3. 首先仔细看看:

    所以,根据你的要求,就像......

    import java.awt.EventQueue;
    import java.awt.Window;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.SwingUtilities;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class Test {
    
        public static void main(String[] args) {
            new Test();
        }
    
        public Test() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                        ex.printStackTrace();
                    }
    
                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class TestPane extends JPanel {
    
            public TestPane() {
                JButton btn = new JButton("Big Button");
                btn.addActionListener(new ActionListener() {
    
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        removeAll();
                        add(new JTextField("Big text field"));
                        Window window = SwingUtilities.getWindowAncestor(TestPane.this);
                        window.pack();
                        window.setLocationRelativeTo(null);
                    }
                });
                add(btn);
            }
    
        }
    
    }
    

    应该可以正常工作

    如果您真的想了解绘画在Swing中的运作方式,那么您需要仔细查看Painting in AWT and SwingPerforming Custom Painting