我的听众有什么问题?

时间:2015-04-02 16:17:46

标签: java user-interface jframe repaint

我创建了界面gui并添加了按钮。但现在我被困在" Steady"按钮。当我点击它时,我想要圆圈"灯泡"将颜色从黄色变为橙色。

我在代码中做错了什么,当我按下" Steady"按钮什么都没发生?

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

1 个答案:

答案 0 :(得分:0)

您需要为Drawing Swing组件添加处理逻辑,例如

  1. 将方法添加到Drawing以更改颜色
  2. 根据第一步修改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();
        }
    }