Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我的听众怎么了?_Java_User Interface_Jframe_Repaint - Fatal编程技术网

Java 我的听众怎么了?

Java 我的听众怎么了?,java,user-interface,jframe,repaint,Java,User Interface,Jframe,Repaint,我已经创建了界面gui并添加了按钮。但现在我被“稳定”按钮卡住了。当我点击它时,我想圈出“灯泡”,将颜色从黄色变为橙色 我在代码中做错了什么?当我按下“稳定”按钮时,什么都没有发生 /** * Created by Metallion on 30/03/2015. */ import javax.swing.*; import java.awt.*; import java.awt.geom.*; import java.awt.event.*; public class Belisha

我已经创建了界面gui并添加了按钮。但现在我被“稳定”按钮卡住了。当我点击它时,我想圈出“灯泡”,将颜色从黄色变为橙色

我在代码中做错了什么?当我按下“稳定”按钮时,什么都没有发生

/**
 * Created by Metallion on 30/03/2015.
 */

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

public class BelishaBeacon {

    public class Drawing extends JPanel {
        private int x = 125;
        private int y = 80;

        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.YELLOW);
            g2.fill(ball);
        }
    }

    public class changeColors extends JPanel {
        private boolean choseColor = false;
        private int x = 125;
        private int y = 80;

        public void changeColor(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g;
            g2.setColor(Color.ORANGE);
            if (!choseColor) {
                g2.fill(new Ellipse2D.Double(x, y, 100, 100));
            }
        }

        public void changeColor() { choseColor = false; }
    }


    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();
        final changeColors colorinG = new changeColors();
        JButton jbtFlash = new JButton("Flash");
        final JButton jbtSteady = new JButton("Steady");
        jbtSteady.addActionListener(
                new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        colorinG.changeColor();
                        colorinG.repaint();
                    }
                });

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

您需要将处理逻辑添加到
图形中
Swing组件,例如

  • 将方法添加到
    图形中以更改颜色
  • 根据第一步修改
    paintComponent(Graphics)

    import javax.swing.*;
    import java.awt.*;
    import java.awt.geom.*;
    import java.awt.event.*;
    
    public class BelishaBeacon {
    
        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.YELLOW);
                g2.fill(ball);
    
                if (changeColors) {
                    g2.setColor(Color.ORANGE);
                    g2.fill(new Ellipse2D.Double(x, y, 100, 100));
                }
    
                changeColors = false;
            }
    
            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();
    
            JButton jbtFlash = new JButton("Flash");
            final JButton jbtSteady = new JButton("Steady");
            jbtSteady.addActionListener(
                    new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                            shapes.changeColors();
                        }
                    });
    
            //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();
        }
    }
    

  • 这个问题还可以,但如果你能把它简化成一个简单的问题就更好了。你甚至可能会在过程中发现这个问题。好的,我会在下一篇文章中记住这一点。谢谢