Java Belisha信标程序中的JButton查询

Java Belisha信标程序中的JButton查询,java,swing,jbutton,Java,Swing,Jbutton,基本上,当我点击稳定按钮时,我的Belisha信标必须保持橙色,但在我的程序中,当我点击稳定按钮时,信标保持浅灰色。有人能告诉我哪里出了问题吗?非常感谢:)。这是我的密码: package Homework; import java.awt.*; import java.awt.geom.*; import java.awt.event.*; import javax.swing.Timer; import javax.swing.JButton; import javax.swing.JF

基本上,当我点击稳定按钮时,我的Belisha信标必须保持橙色,但在我的程序中,当我点击稳定按钮时,信标保持浅灰色。有人能告诉我哪里出了问题吗?非常感谢:)。这是我的密码:

package Homework;


import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import javax.swing.Timer;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class BelishaBeacon {
    private static Timer timer;
    public class Drawing extends JPanel {

        private int x = 125;
        private int y = 80;
        private boolean changeColors = false;


        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g;

            Rectangle box1 = new Rectangle(165, 180, 20, 45);
            Rectangle box2 = new Rectangle(165, 225, 20, 45);
            Rectangle box3 = new Rectangle(165, 270, 20, 45);
            Rectangle box4 = new Rectangle(165, 315, 20, 45);
            Rectangle box5 = new Rectangle(165, 360, 20, 45);
            Rectangle box6 = new Rectangle(165, 405, 20, 45);


            Ellipse2D.Double ball = new Ellipse2D.Double(x, y, 100, 100);
            g2.draw(ball);
            g2.draw(box1);
            g2.draw(box2);
            g2.draw(box3);
            g2.draw(box4);
            g2.draw(box5);
            g2.draw(box6);

            g2.setColor(Color.BLACK);
            g2.fill(box1);
            g2.fill(box3);
            g2.fill(box5);
            g2.setColor(Color.ORANGE);
            g2.fill(ball);
            changeColors = !changeColors;
            if (changeColors) {
                g2.setColor(Color.lightGray);
                g2.fill(new Ellipse2D.Double(x, y, 100, 100));
            }
        }




        public void changeColors() {
            changeColors = true;
            repaint();
        }
    }

    public BelishaBeacon() {

        JFrame frame = new JFrame();
        frame.setSize(350, 570);
        frame.setTitle("Belisha Beacon");
        frame.setLayout(new BorderLayout(0, 0));
        final Drawing shapes = new Drawing();

        timer = new Timer(500, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                shapes.repaint();
            }
        });



        JButton jbtFlash = new JButton("Flash");
        jbtFlash.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("Flashing");
                if (!timer.isRunning()) {
                    timer.start();
                }

            }
        });




        final JButton jbtSteady = new JButton("Steady");
        jbtSteady.addActionListener(
                new ActionListener() {
                    public void actionPerformed(ActionEvent e) {

                        timer.stop();
                    }
                });


        JPanel controlPanel = new JPanel();
        controlPanel.setLayout(new GridLayout(1, 2, 0, 0));
        controlPanel.add(jbtFlash);
        controlPanel.add(jbtSteady);

        frame.add(controlPanel, BorderLayout.SOUTH);
        frame.add(shapes);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        new BelishaBeacon();
        timer.start();
    }
}
试试这个:

public class BelishaBeacon {
    private static Timer timer;
    public class Drawing extends JPanel {

        private int x = 125;
        private int y = 80;
        private boolean changeColors = false;


        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g;

            Rectangle box1 = new Rectangle(165, 180, 20, 45);
            Rectangle box2 = new Rectangle(165, 225, 20, 45);
            Rectangle box3 = new Rectangle(165, 270, 20, 45);
            Rectangle box4 = new Rectangle(165, 315, 20, 45);
            Rectangle box5 = new Rectangle(165, 360, 20, 45);
            Rectangle box6 = new Rectangle(165, 405, 20, 45);


            Ellipse2D.Double ball = new Ellipse2D.Double(x, y, 100, 100);
            g2.draw(ball);
            g2.draw(box1);
            g2.draw(box2);
            g2.draw(box3);
            g2.draw(box4);
            g2.draw(box5);
            g2.draw(box6);

            g2.setColor(Color.BLACK);
            g2.fill(box1);
            g2.fill(box3);
            g2.fill(box5);
            g2.setColor(Color.ORANGE);
            g2.fill(ball);
           // changeColors = !changeColors;
            if (changeColors) {
                g2.setColor(Color.lightGray);
                g2.fill(new Ellipse2D.Double(x, y, 100, 100));
            }
        }




        public void changeColors() {
            changeColors = !changeColors;
            repaint();
        }

        public boolean getChangeColors() {
            return changeColors;
        }
    }

    public BelishaBeacon() {

        JFrame frame = new JFrame();
        frame.setSize(350, 570);
        frame.setTitle("Belisha Beacon");
        frame.setLayout(new BorderLayout(0, 0));
        final Drawing shapes = new Drawing();

        timer = new Timer(500, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //shapes.repaint();
                shapes.changeColors();
            }
        });



        JButton jbtFlash = new JButton("Flash");
        jbtFlash.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("Flashing");
                if (!timer.isRunning()) {
                    timer.start();
                }

            }
        });




        final JButton jbtSteady = new JButton("Steady");
        jbtSteady.addActionListener(
                new ActionListener() {
                    public void actionPerformed(ActionEvent e) {

                        timer.stop();
                        if(shapes.getChangeColors()) {
                            shapes.changeColors();
                        }
                    }
                });


        JPanel controlPanel = new JPanel();
        controlPanel.setLayout(new GridLayout(1, 2, 0, 0));
        controlPanel.add(jbtFlash);
        controlPanel.add(jbtSteady);

        frame.add(controlPanel, BorderLayout.SOUTH);
        frame.add(shapes);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        new BelishaBeacon();
        timer.start();
    }
}
试试这个:

public class BelishaBeacon {
    private static Timer timer;
    public class Drawing extends JPanel {

        private int x = 125;
        private int y = 80;
        private boolean changeColors = false;


        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g;

            Rectangle box1 = new Rectangle(165, 180, 20, 45);
            Rectangle box2 = new Rectangle(165, 225, 20, 45);
            Rectangle box3 = new Rectangle(165, 270, 20, 45);
            Rectangle box4 = new Rectangle(165, 315, 20, 45);
            Rectangle box5 = new Rectangle(165, 360, 20, 45);
            Rectangle box6 = new Rectangle(165, 405, 20, 45);


            Ellipse2D.Double ball = new Ellipse2D.Double(x, y, 100, 100);
            g2.draw(ball);
            g2.draw(box1);
            g2.draw(box2);
            g2.draw(box3);
            g2.draw(box4);
            g2.draw(box5);
            g2.draw(box6);

            g2.setColor(Color.BLACK);
            g2.fill(box1);
            g2.fill(box3);
            g2.fill(box5);
            g2.setColor(Color.ORANGE);
            g2.fill(ball);
           // changeColors = !changeColors;
            if (changeColors) {
                g2.setColor(Color.lightGray);
                g2.fill(new Ellipse2D.Double(x, y, 100, 100));
            }
        }




        public void changeColors() {
            changeColors = !changeColors;
            repaint();
        }

        public boolean getChangeColors() {
            return changeColors;
        }
    }

    public BelishaBeacon() {

        JFrame frame = new JFrame();
        frame.setSize(350, 570);
        frame.setTitle("Belisha Beacon");
        frame.setLayout(new BorderLayout(0, 0));
        final Drawing shapes = new Drawing();

        timer = new Timer(500, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //shapes.repaint();
                shapes.changeColors();
            }
        });



        JButton jbtFlash = new JButton("Flash");
        jbtFlash.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("Flashing");
                if (!timer.isRunning()) {
                    timer.start();
                }

            }
        });




        final JButton jbtSteady = new JButton("Steady");
        jbtSteady.addActionListener(
                new ActionListener() {
                    public void actionPerformed(ActionEvent e) {

                        timer.stop();
                        if(shapes.getChangeColors()) {
                            shapes.changeColors();
                        }
                    }
                });


        JPanel controlPanel = new JPanel();
        controlPanel.setLayout(new GridLayout(1, 2, 0, 0));
        controlPanel.add(jbtFlash);
        controlPanel.add(jbtSteady);

        frame.add(controlPanel, BorderLayout.SOUTH);
        frame.add(shapes);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        new BelishaBeacon();
        timer.start();
    }
}
不要尝试在
paintComponent()
中切换颜色,而是为
绘图提供
颜色
,并使用它渲染球:

private Color color = Color.lightGray;

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        …
        g2.setColor(color);
        g2.fill(ball);
        …
    }
使
更改颜色()
实际更改颜色:

    public void changeColors() {
        if (Color.orange.equals(color)) {
            color = Color.lightGray;
        } else {
            color = Color.orange;
        }
        repaint();
    }
并添加一个
makestable()
方法:

public void makeSteady() {
    color = Color.orange;
    repaint();
}
现在,计时器的操作可以只执行
shapes.changeColors()
,Flash按钮处理程序可以只执行
timer.restart()
,而稳定按钮处理程序可以执行以下操作:

timer.stop();
shapes.makeSteady();
另外,不要忘记。

不要尝试在
paintComponent()
中切换颜色,而是给
绘图
一个
颜色
,并使用它渲染球:

private Color color = Color.lightGray;

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        …
        g2.setColor(color);
        g2.fill(ball);
        …
    }
使
更改颜色()
实际更改颜色:

    public void changeColors() {
        if (Color.orange.equals(color)) {
            color = Color.lightGray;
        } else {
            color = Color.orange;
        }
        repaint();
    }
并添加一个
makestable()
方法:

public void makeSteady() {
    color = Color.orange;
    repaint();
}
现在,计时器的操作可以只执行
shapes.changeColors()
,Flash按钮处理程序可以只执行
timer.restart()
,而稳定按钮处理程序可以执行以下操作:

timer.stop();
shapes.makeSteady();

还有,别忘了。

这和这个完全一样。你有可能是费德勒的双胞胎吗?你可能可以用我给费德勒留下的提示:)@bili我和他在同一个班啊哈,我和他都试过了,但都没有结果:(.也许你可以分享你和他试过的东西?你现在不想错过作业的提交日期,是吗?:)哦,我会更新问题:)。是的,我必须确保我准时啊哈。这和这一模一样。你有可能是费德勒的双胞胎吗?你可能可以用我给费德勒留下的提示:)@bili我和他在同一个班啊哈,我和他都试过了,但都没有结果:(.也许你可以分享你和他试过的东西?你现在不想错过作业的提交日期,是吗?:)哦,我会更新问题:)。是的,我必须确保我准时啊哈。我试过了,但我需要灯塔在手前闪烁,只有当我点击稳定按钮时,灯塔才会变成橙色,当我再次点击闪烁按钮时,灯塔才会再次闪烁。我刚刚试过,它就工作了。哦,我非常感谢你的帮助:)。我试过了,但我需要灯塔在手前闪烁,只有当我单击“稳定”按钮时,灯塔才会变成橙色,当我再次单击“闪烁”按钮时,灯塔才会再次闪烁。我刚刚试过,它就工作了。哦,我非常感谢你的帮助:)。