交通信号灯使用Java Swing:灯光未显示

huangapple go评论67阅读模式
英文:

Traffic light using Java Swing: the light is not showing

问题

这是翻译好的内容:

我创建了一个交通信号灯,必须与下面的这个完全一样:

交通信号灯使用Java Swing:灯光未显示

我应该使用上面视频中的确切代码,但由于某种原因,尽管我的代码与上面的代码匹配,但它不起作用。我认为这是因为视频中的人使用Eclipse来编码,而我使用repl.it Java Swing来完成这个任务。

我的代码:


Main.java

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Main {
   public static void main(String[] args) {

      JFrame frame = new JFrame();
      JPanel panel = new StopLightPanel();

      frame.add(panel);
      frame.setSize(250, 350);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setVisible(true);
   }
}

StopLightPanel.java

public class StopLightPanel extends JPanel {

  StopLightDrawing light = new StopLightDrawing();

  public StopLightPanel() {
    JButton changeButton = new JButton("Switch");
    light.setPreferredSize(new Dimension(160, 260));

    ButtonListener listener = new ButtonListener();
    changeButton.addActionListener(listener);

    add(light);
    add(changeButton);
  }
  
  public class ButtonListener implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
      light.changeColor();
    }
  }

StopLightDrawing.java

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JComponent;

public class StopLightDrawing extends JComponent {
  Color go = Color.gray;
  Color slow = Color.gray;
  Color stop = Color.red;

  String activeLight = "red";

  public void paintComponent(Graphics g) {
    g.setColor(Color.yellow);
    g.fillRect(0, 0, 150, 250);

    g.setColor(Color.black);
    g.drawRect(0, 0, 150, 250);

    g.setColor(stop);
  }
}

运行主文件后,我遇到了很多错误:

./StopLightPanel.java:22: error: reached end of file while parsing
} Λ
./StopLightPanel.java:1: error: cannot find symbol public class StopLightPanel extends JPanel{
symbol: class JPanel
./StopLightPanel.java:15: error: cannot find symbol public class ButtonListener implements ActionListener{ A
symbol: class ActionListener location: class StopLightPanel
./StopLightPanel.java:18: error: cannot find symbol public void actionPerformed (ActionEvent e) {
symbol: class ActionEvent
location: class StopLightPanel.ButtonListener
Main.java:8: error: incompatible types: StopLightPanel cannot be converted to JPanel JPanel panel = new StopLightPanel();
Λ
Main.java:12: error: cannot find symbol

我认为问题在于转换以及我在代码中使用指定元素的方式。我尝试修复了StopLightPanel.java,因为我认为问题就出在那里,但没有帮助。

英文:

so I created a Traffic light that has to be exactly like this one below:

交通信号灯使用Java Swing:灯光未显示

I am supposed to use the exact code from the above video, but for some reason, it does not work even though my code matches with the code above. I think it is due to the person in that video using eclipse to code whereas I used repl.it java swing to accomplish this task.

My code:


Main.java

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Main{
   public static void main(String[]args){

      JFrame frame = new JFrame();
      JPanel panel = new StopLightPanel();

      frame.add(panel);
      frame.setSize(250, 350);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_ClOSE);
      frame.setVisible(true);
   }
}

StopLightPanel.java

public class StopLightPanel extends JPanel {

  StopLightDrawing light = new StopLightDrawing();

  public StopLightPanel() {
    JButton changebutton = new JButton("Switch");
    light.setPreferredSize(new Dimension(160, 260));

    buttonListener l = new buttonListener();
    changebutton.addActionlistener(l);

    add(light);
    add(changebutton);
  }
  public class buttonListener implements Actionlistener {

    @Override
    public void actionPerformed(ActionEvent e) {
      // TODO Auto-generated method stub
      light.changeColor();
    }
  }

StopLightDrawing.java

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JComponent;

public class StopLightDrawing extends JComponent {
  Color go = Color.gray;
  Color slow = Color.gray;
  Color stop = Color.red;

  String activelight = "red";

  public void paintComponent(Graphics g) {
    g.setColor(Color.yellow);
    g.fillRect(0, 0, 150, 250);

    g.setColor(Color.black);
    g.drawRect(0, 0, 150, 250);

    g.setColor(stop);
  }
}

After running the main file I experience quite a bit of errors:

./StopLightPanel.java:22: error: reached end of file while parsing
} Λ
./StopLightPanel.java:1: error: cannot find symbol public class StopLightPanel extends JPanel{
symbol: class JPanel
./StopLightPanel.java:15: error: cannot find symbol public class buttonListener implements Actionlistener{ A
symbol: class Actionlistener location: class StopLightPanel
./StopLightPanel.java:18: error: cannot find symbol public void actionPerformed (ActionEvent e) {
symbol: class ActionEvent
location: class StopLightPanel.buttonListener
Main.java:8: error: incompatible types: StopLightPanel cannot be converted to JPanel JPanel panel = new StopLightPanel();
Λ
Main.java:12: error: cannot find symbol

I believe the issue lies in the conversion, and how I'm using the specified elements in the code, I tried to fix StopLightPanel.java as I think the issues lies from there, but it did not help.

答案1

得分: 0

我纠正了你的打字错误,完成了你的绘图代码,并设计了以下的GUI界面。

交通信号灯使用Java Swing:灯光未显示

按下开关按钮将会使灯光从绿色变成黄色(橙色),然后变成红色。

Oracle有一个有用的教程,使用Swing创建GUI。跳过使用NetBeans IDE学习Swing的部分。

所有的Swing应用程序都必须以对SwingUtilitiesinvokeLater方法的调用开始。这个方法确保Swing组件在事件调度线程上被创建和执行。

大部分的更改都在StopLightDrawing类中。在paintComponent的重写中,第一行总是应该是super.paintComponent(g);

以下是完整的可运行代码。我将额外的类作为内部类,这样我就可以把代码作为一个块发布。

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
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.SwingUtilities;

public class TrafficLightExample implements Runnable {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new TrafficLightExample());
    }

    @Override
    public void run() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new StopLightPanel();
        frame.add(panel);
        frame.setSize(250, 350);
        frame.setVisible(true);
    }

    public class StopLightPanel extends JPanel {

        private static final long serialVersionUID = 1L;

        StopLightDrawing light = new StopLightDrawing();

        public StopLightPanel() {
            JButton changebutton = new JButton("Switch");

            ButtonListener l = new ButtonListener();
            changebutton.addActionListener(l);

            add(light);
            add(changebutton);
        }

        public class ButtonListener implements ActionListener {
            @Override
            public void actionPerformed(ActionEvent e) {
                light.changeColor();
            }
        }
    }

    public class StopLightDrawing extends JComponent {

        private static final long serialVersionUID = 1L;

        private int activeLight = 2;
        private Color[] colors = { Color.green, Color.orange, Color.red };

        public StopLightDrawing() {
            this.setPreferredSize(new Dimension(160, 250));
        }

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);

            g.setColor(Color.black);
            g.fillRect(0, 0, 160, 250);

            g.setColor(Color.yellow);
            g.fillRect(5, 5, 150, 240);

            int radius = 20;
            int diameter = radius + radius;
            int x = 80;
            int y = 60;
            setLightColor(g, 2);
            g.fillOval(x - radius, y - radius, diameter, diameter);

            y += 60;
            setLightColor(g, 1);
            g.fillOval(x - radius, y - radius, diameter, diameter);

            y += 60;
            setLightColor(g, 0);
            g.fillOval(x - radius, y - radius, diameter, diameter);
        }

        private void setLightColor(Graphics g, int lightPosition) {
            if (activeLight == lightPosition) {
                g.setColor(colors[lightPosition]);
            } else {
                g.setColor(Color.gray);
            }
        }

        public void changeColor() {
            activeLight++;
            activeLight %= colors.length;
            repaint();
        }
    }

}
英文:

I fixed your typing errors, finished your drawing code, and came up with the following GUI.

交通信号灯使用Java Swing:灯光未显示

Pressing the switch button will change the light from green to yellow (orange), to red.

Oracle has a helpful tutorial, Creating a GUI With Swing. Skip the Learning Swing with the NetBeans IDE section.

All Swing applications must start with a call to the SwingUtilities invokeLater method. This method ensures that the Swing components are created and executed on the Event Dispatch Thread.

Most of the changes were in the StopLightDrawing class. The first line of a paintComponent override should always be super.paintComponent(g);.

Here's the complete runnable code. I made the additional classes inner classes so I could post the code as one block.

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
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.SwingUtilities;
public class TrafficLightExample implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new TrafficLightExample());
}
@Override
public void run() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new StopLightPanel();
frame.add(panel);
frame.setSize(250, 350);
frame.setVisible(true);
}
public class StopLightPanel extends JPanel {
private static final long serialVersionUID = 1L;
StopLightDrawing light = new StopLightDrawing();
public StopLightPanel() {
JButton changebutton = new JButton("Switch");
ButtonListener l = new ButtonListener();
changebutton.addActionListener(l);
add(light);
add(changebutton);
}
public class ButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
light.changeColor();
}
}
}
public class StopLightDrawing extends JComponent {
private static final long serialVersionUID = 1L;
private int activeLight = 2;
private Color[] colors = { Color.green, Color.orange, Color.red };
public StopLightDrawing() {
this.setPreferredSize(new Dimension(160, 250));
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.black);
g.fillRect(0, 0, 160, 250);
g.setColor(Color.yellow);
g.fillRect(5, 5, 150, 240);
int radius = 20;
int diameter = radius + radius;
int x = 80;
int y = 60;
setLightColor(g, 2);
g.fillOval(x - radius, y - radius, diameter, diameter);
y += 60;
setLightColor(g, 1);
g.fillOval(x - radius, y - radius, diameter, diameter);
y += 60;
setLightColor(g, 0);
g.fillOval(x - radius, y - radius, diameter, diameter);
}
private void setLightColor(Graphics g, int lightPosition) {
if (activeLight == lightPosition) {
g.setColor(colors[lightPosition]);
} else {
g.setColor(Color.gray);
}
}
public void changeColor() {
activeLight++;
activeLight %= colors.length;
repaint();
}
}
}

huangapple
  • 本文由 发表于 2020年10月2日 05:31:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/64163455.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定