首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Belisha Beacon Java程序按钮查询

Belisha Beacon Java程序按钮查询
EN

Stack Overflow用户
提问于 2016-04-14 20:26:52
回答 1查看 173关注 0票数 0

任务是为Belisha Beacon编写代码,它从闪烁开始,颜色在浅灰色和橙色之间交替,并有两个按钮,即闪光和稳定。因此,当我单击稳定按钮时,信标必须保持橙色,但对于我的程序,当我单击稳定按钮时,信标保持稳定的颜色,当我单击稳定按钮后,当我单击闪光按钮时,信标不再闪烁。到目前为止,这是我的代码,请有人帮助我哪里出了问题,谢谢:)

代码语言:javascript
复制
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;
            //creating the shapes
            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);

            //drawing the shapes
            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);
            //coloring the shapes
            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.darkGray);
                g2.fill(new Ellipse2D.Double(x, y, 100, 100));
            }
        }




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

    public BelishaBeacon() {
        //Creation of frame
        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) {


            }
        });




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

        //Positioning
        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();
    }
}
EN

回答 1

Stack Overflow用户

发布于 2016-04-14 20:40:27

因此,对于Flash按钮,侦听器当前为空。您要做的是检查闪光灯按钮是否被按下,然后再次启动计时器。

尝尝这个

代码语言:javascript
复制
JButton jbtFlash = new JButton("Flash");
    jbtFlash.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("Flashing");
            if (!timer.isRunning()) {
                timer.start();
            }

        }
    });

还有你的稳定按钮问题。目前没有逻辑来检查当前的颜色是什么,即它是灰色还是黄色。因此,您需要以某种方式修改代码来检查当前颜色状态,并在颜色为黄色时停止它。有一些想法:)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36623091

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档