Java 通过单击JButton绘制形状

Java 通过单击JButton绘制形状,java,swing,jbutton,paint,jcomponent,Java,Swing,Jbutton,Paint,Jcomponent,我目前正试图通过单击按钮绘制图形。我的问题发生在我单击按钮时,它没有显示在面板中,但我知道它正在绘制,因为它穿过循环 当我请求面板在构造器内部绘制它时,会发生绘制,但不会在构造器内部的按钮内部 如果我把代码放在构造函数中的“stuff()”方法中,它会把一切都画得很好 import java.awt.BorderLayout; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import j

我目前正试图通过单击按钮绘制图形。我的问题发生在我单击按钮时,它没有显示在面板中,但我知道它正在绘制,因为它穿过循环

当我请求面板在构造器内部绘制它时,会发生绘制,但不会在构造器内部的按钮内部

如果我把代码放在构造函数中的“stuff()”方法中,它会把一切都画得很好

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class MainFrame{
    public static void main(String[]args){
        MainFrame f = new MainFrame();
    }
public JFrame frame = new JFrame();
public JPanel panel = new JPanel(new BorderLayout());
public MainFrame(){
    JButton button1 = new JButton("Shweet Button");
    button1.setBounds(185, 10, 130, 20);
    frame.setBounds(1680/4,1050/4,500, 500);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setResizable(false);
    frame.add(panel);
    panel.setBackground(Color.black);
    frame.setVisible(true);
    frame.getContentPane().setLayout(null);
    frame.add(button1);
    button1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            stuff();
        }
    });
}
public void stuff(){
    for(int i = 0;i<1000;i++){
        panel.add(new paintComponent());
        panel.setBackground(Color.black);
        frame.repaint();
        try {
            Thread.sleep(80);
        } catch (InterruptedException e1){e1.printStackTrace();}
    }
}
static class paintComponent extends JComponent{
    public int options;
    public void paint(Graphics g){
        Graphics2D g2 = (Graphics2D)g;
        g2.setColor(Color.white);

        if(options == 0){
            options = 1;
            g2.drawOval(50, (JFrame.HEIGHT/2)+100, 50, 50);
            g2.drawOval(60, (JFrame.HEIGHT/2)+110, 8, 8);
            g2.fillOval(60, (JFrame.HEIGHT/2)+110, 8, 8);
            g2.drawOval(80, (JFrame.HEIGHT/2)+110, 8, 8);
            g2.fillOval(80, (JFrame.HEIGHT/2)+110, 8, 8);
            g2.drawArc(65, (JFrame.HEIGHT/2)+130, 100, 0, 140, 30);
            g2.drawLine(75, (JFrame.HEIGHT/2)+150, 75, (JFrame.HEIGHT/2)+220);
            g2.drawLine(75, (JFrame.HEIGHT/2)+180, 100, (JFrame.HEIGHT/2)+160);
            g2.drawLine(75, (JFrame.HEIGHT/2)+180, 65, (JFrame.HEIGHT/2)+210);
            g2.drawLine(75, (JFrame.HEIGHT/2)+220, 50, (JFrame.HEIGHT/2)+260);
            g2.drawLine(75, (JFrame.HEIGHT/2)+220, 100, (JFrame.HEIGHT/2)+260);
        }else if(options == 1){
            options = 0;
            g2.drawOval(50, (JFrame.HEIGHT/2)+100, 50, 50);
            g2.drawOval(60, (JFrame.HEIGHT/2)+110, 8, 8);
            g2.fillOval(60, (JFrame.HEIGHT/2)+110, 8, 8);
            g2.drawOval(80, (JFrame.HEIGHT/2)+110, 8, 8);
            g2.fillOval(80, (JFrame.HEIGHT/2)+110, 8, 8);
            g2.drawArc(65, (JFrame.HEIGHT/2)+130, 100, 0, 140, 30);
            g2.drawLine(75, (JFrame.HEIGHT/2)+150, 75, (JFrame.HEIGHT/2)+220);
            g2.drawLine(75, (JFrame.HEIGHT/2)+180, 100, (JFrame.HEIGHT/2)+180);
            g2.drawLine(75, (JFrame.HEIGHT/2)+180, 65, (JFrame.HEIGHT/2)+210);
            g2.drawLine(75, (JFrame.HEIGHT/2)+220, 50, (JFrame.HEIGHT/2)+260);
            g2.drawLine(75, (JFrame.HEIGHT/2)+220, 100, (JFrame.HEIGHT/2)+260);
        }
    }
}
}
导入java.awt.BorderLayout;
导入java.awt.Color;
导入java.awt.Graphics;
导入java.awt.Graphics2D;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入javax.swing.JButton;
导入javax.swing.JComponent;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
公共类主机{
公共静态void main(字符串[]args){
大型机f=新的大型机();
}
公共JFrame=新JFrame();
public JPanel panel=新JPanel(new BorderLayout());
公共主机(){
JButton button1=新JButton(“Shweet按钮”);
按钮1.立根(185、10、130、20);
机架立根(1680/41050/4500500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setresizeable(false);
框架。添加(面板);
面板.立根背景(颜色.黑色);
frame.setVisible(true);
frame.getContentPane().setLayout(null);
框。添加(按钮1);
button1.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
填充();
}
});
}
公共物品{

对于(inti=0;i您正在阻止事件调度线程,该线程负责处理绘制请求

你不应该做这样的事

for(int i = 0;i<1000;i++){
    panel.add(new paintComponent());
    panel.setBackground(Color.black);
    frame.repaint();
    try {
        Thread.sleep(80);
    } catch (InterruptedException e1){e1.printStackTrace();}
}

您不应该使用
JFrame.HEIGHT
这实际上与帧高度无关,但它是
ImageObserver
支持的一部分……或者任何类型的幻数。您正在阻止事件调度线程,这会阻止它更新UID您必须发布这么多代码吗?这就足够了吗如果两个选项各有一条drawOvel线?应用程序看起来像挂起了执行你太棒了!我在重写paintComponent方法时遇到了问题,我意识到它必须受到保护,idk这意味着什么,但感谢你付出了额外的努力帮助我!受保护的
方法意味着只有类声明的类、同一包中的类以及扩展该类的任何子类都可以调用该方法。有关更多详细信息,请查看sage reserved Java methods names
static class**paintComponent**extensed JComponent{
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class MainFrame {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                MainFrame f = new MainFrame();
            }
        });
    }
    public JFrame frame = new JFrame();
    public JPanel panel = new JPanel(new BorderLayout());
    private WavePane waver;

    public MainFrame() {
        waver = new WavePane();
        JButton button1 = new JButton("Shweet Button");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(panel);
        panel.setBackground(Color.black);
        frame.add(button1, BorderLayout.SOUTH);
        button1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                stuff();
            }
        });
        panel.add(waver);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public void stuff() {
//        for (int i = 0; i < 1000; i++) {
//            panel.add(new paintComponent());
//            panel.setBackground(Color.black);
//            frame.repaint();
//            try {
//                Thread.sleep(80);
//            } catch (InterruptedException e1) {
//                e1.printStackTrace();
//            }
//        }
        waver.walk(!waver.isWaving());
    }

    public class WavePane extends JComponent {

        private int options;
        private Timer timer;

        public WavePane() {
            setOpaque(false);
            timer = new Timer(80, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.out.println("tick");
                    options++;
                    repaint();
                }
            });
            timer.setRepeats(true);
            timer.setCoalesce(true);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        public void walk(boolean walk) {
            if (walk) {
                timer.start();
            } else {
                timer.stop();
            }
        }

        public boolean isWaving() {
            return timer.isRunning();
        }

        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g;
            g2.setColor(Color.white);

            int height = getHeight();

            if (options % 2 == 0) {
                g2.drawOval(50, 100, 50, 50);
                g2.drawOval(60, 110, 8, 8);
                g2.fillOval(60, 110, 8, 8);
                g2.drawOval(80, 110, 8, 8);
                g2.fillOval(80, 110, 8, 8);
                g2.drawArc(65, 130, 100, 0, 140, 30);
                g2.drawLine(75, 150, 75, 220);
                g2.drawLine(75, 180, 100, 160);
                g2.drawLine(75, 180, 65, 210);
                g2.drawLine(75, 220, 50, 260);
                g2.drawLine(75, 220, 100, 260);
            } else {
                g2.drawOval(50, 100, 50, 50);
                g2.drawOval(60, 110, 8, 8);
                g2.fillOval(60, 110, 8, 8);
                g2.drawOval(80, 110, 8, 8);
                g2.fillOval(80, 110, 8, 8);
                g2.drawArc(65, 130, 100, 0, 140, 30);
                g2.drawLine(75, 150, 75, 220);
                g2.drawLine(75, 180, 100, 180);
                g2.drawLine(75, 180, 65, 210);
                g2.drawLine(75, 220, 50, 260);
                g2.drawLine(75, 220, 100, 260);
            }
        }
    }
}