paintComponent不工作java,

-1 java swing paintcomponent

java swing,paintComponent在下面的代码中,它正在执行但是paintComponent函数没有工作,所以它的原因是什么.我是java的新手,所以犯了这些错误.

package practice;

    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    public class gui extends JPanel implements ActionListener  
    {
    JFrame frame;
    JButton button;
    public void paint(Graphics g)
    {
        super.paint(g);
        g.setColor(Color.black);
        g.fillRect(0, 0, getWidth(), getHeight());
    }
    public void go()
    {
        frame=new JFrame();
        button=new JButton("click me");
        frame.getContentPane().add(button);
        button.addActionListener(this);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(2000,2000);
        frame.setVisible(true);

    }
    public static void main(String[] args)
    {

    gui g=new gui();
    g.go();

    }

    public void actionPerformed(ActionEvent e)
    {

        button.setText("mmmmm");
    }

    } 
Run Code Online (Sandbox Code Playgroud)

Hov*_*els 5

您永远不会向JFrame添加gui实例,因此不会显示JPanel.您可以new gui()在main方法中创建一个对象,但是永远不要将此对象添加到JFrame中.一个等效的位是添加thisgo()方法中的JFrame .您还需要将JButton添加this到JFrame,而不是JFrame.必须将JPanel添加到顶级窗口才能使其可视化.它不会神奇地出现.

例如:

public void go()
{
    frame=new JFrame();
    button=new JButton("click me");

    // frame.getContentPane().add(button); // !! Nope

    this.add(button);  // !! add button to the JPanel
    frame.getContentPane().add(this); // !! add the JPanel to the JFrame

    button.addActionListener(this);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(2000,2000);
    frame.setVisible(true);
}
Run Code Online (Sandbox Code Playgroud)

此外,您应该重写paintComponent,而不是绘制.但话说setBackground(Color.BLACK)回来,为了绘制JPanel黑色的背景,你只需要在设置JPanel的地方打电话:例如:

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

@SuppressWarnings("serial")
public class Gui extends JPanel {
    private static final int PREF_W = 800;
    private static final int PREF_H = 650;
    private JButton button = new JButton(new ButtonAction("Click Me"));

    public Gui() {
        add(button);
        setBackground(Color.BLACK); // now no need to override paint/paintComponent
    }

    @Override
    // to make GUI bigger
    public Dimension getPreferredSize() {
        if (isPreferredSizeSet()) {
            return super.getPreferredSize();
        }
        return new Dimension(PREF_W, PREF_H);
    }

    // @Override  // no longer needed
    // protected void paintComponent(Graphics g) {
    //     super.paintComponent(g);
    //     g.setColor(Color.black);
    //     g.fillRect(0, 0, getWidth(), getHeight());
    // }

    // better than having the GUI class implement ActionListener:
    private class ButtonAction extends AbstractAction {
        public ButtonAction(String name) {
            super(name);
        }

        @Override
        public void actionPerformed(ActionEvent arg0) {
            putValue(NAME, "mmmmmmm");
        }
    }

    private static void createAndShowGui() {
        Gui mainPanel = new Gui();

        JFrame frame = new JFrame("Gui");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGui();
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

另外,作为附带建议,现在帮助我们并在将来帮助自己,请编辑代码并更改变量名称以符合Java命名约定:类名称全部以大写字母开头,方法/变量名称以小写字母.在大多数情况下,字段也应该是私有的.