重绘() JPanel

repaint() JPanel

我目前正在尝试理解 paintComponent()repaint() 方法,但我就是做不到。我有一个带按钮的面板。我想做的是当我单击按钮清除面板(删除按钮)并可能调整它的大小并添加一个 JTextField,但我根本不知道在 [=13] 中写什么=]方法。

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);
    }
}
}

这是在 MadProgrammer 的 post 之后我的 mousePressed 事件的代码:

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);
        }

但是我在 window.pack():

收到这个错误
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at AppPackage.Test.mousePressed(Test.java:34)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access0(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
  1. paintComponent 与组件管理没有任何关系,绝不能用于从 UI
  2. 添加或删除组件
  3. MouseListener 是与 JButton 一起使用的错误侦听器,用户可能会使用键盘、键盘快捷键和鼠标以及以编程方式触发按钮,而是使用 ActionListener

首先仔细看看:

所以,根据您的要求,类似...

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 Swing and Performing Custom Painting