我将如何修改此程序?该程序用于模拟交通灯,点击按钮即可更改灯光。我已经使程序在赋值的参数下工作得很好。该任务需要4个类,一个JFrame和两个嵌套的JPanel(一个用于trafficcontrol,一个用于trafficlight面板)。我似乎无法回答关于如何将动作侦听器的内部类别分离到其自己的separete实体的最后一个问题,并且仍然可以让程序工作。
内部类用于促进两个类之间的通信 有亲密的关系。使内部类(ChangeListener)成为常规类 从TrafficControlPanel中取出并观察所涉及的并发症 让他们沟通和两个阶级的亲密关系。
Package traffic.light;
import javax.swing.JFrame;
public class TrafficLight extends JFrame
{
public static void main (String[] args)
{
JFrame frame = new JFrame("Traffic Light");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(150, 230);
frame.add(new TrafficControlPanel());
frame.pack();
frame.setVisible(true);
}
}
package traffic.light;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class TrafficLightPanel extends JPanel
{
private int currentState = 0;
private final int NUM_LIGHTS = 3; // the number of lights
private final int X = 50, Y = 10, WIDTH = 50, HEIGHT = 130; //box size
private final int DIAMETER = 30; // light diameter
private final int X_OFFSET = 10, Y_OFFSET = 10; // offsets to position the lights in the box
private final int PANEL_WIDTH = 150, PANEL_HEIGHT = 230; // the size of a traffic control panel
//-----------------------------------------------------------------
// Creates the traffic light panel
//-----------------------------------------------------------------
public void TrafficLightPanel()
{
setPreferredSize(new Dimension(150, 230));
setLayout(new BorderLayout());
}
public void paintComponent(Graphics page)
{
super.paintComponent(page);
int lightOn = currentState % NUM_LIGHTS;
setBackground(Color.white);
page.setColor(Color.lightGray);
page.fillRect(X, Y, WIDTH, HEIGHT);
switch (lightOn)
{
case 0:
page.setColor(Color.red);
page.fillOval((X+X_OFFSET),(Y+Y_OFFSET), 30, 30);
page.setColor(Color.darkGray);
page.fillOval((X+X_OFFSET),60 , 30, 30);
page.setColor(Color.darkGray);
page.fillOval((X+X_OFFSET),100, 30, 30);
break;
case 1:
page.setColor(Color.darkGray);
page.fillOval((X+X_OFFSET),20, 30, 30);
page.setColor(Color.yellow);
page.fillOval((X+X_OFFSET),60 , 30, 30);
page.setColor(Color.darkGray);
page.fillOval((X+X_OFFSET),100, 30, 30);
break;
case 2 :
page.setColor(Color.darkGray);
page.fillOval((X+X_OFFSET),(Y+Y_OFFSET), 30, 30);
page.setColor(Color.darkGray);
page.fillOval((X+X_OFFSET),60 , 30, 30);
page.setColor(Color.green);
page.fillOval((X+X_OFFSET),100, 30, 30);
break;
}
}
public void change(int clickcount )
{
currentState = clickcount ;
repaint();
}
}
package traffic.light;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class TrafficControlPanel extends JPanel {
private JButton push;
private TrafficLightPanel LightPanel;
int click = 0;
public TrafficControlPanel() {
setLayout(new BorderLayout());
LightPanel = new TrafficLightPanel();
push = new JButton("Change Light");
push.addActionListener(new TrafficControlPanel.ChangeListener());
setPreferredSize(new Dimension(150, 230));
setBackground(Color.white);
add(LightPanel, BorderLayout.CENTER);
add(push, BorderLayout.SOUTH);
}
// this needs to be deleted and put into the ChangeListener class below
private class ChangeListener implements ActionListener {
int click = 0;
public void actionPerformed(ActionEvent event) {
click++;
LightPanel.change(click);
}
}
}
package traffic.light;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ChangeListener implements ActionListener{
public void actionPerformed(ActionEvent event) {
// what goes here?
}
}
答案 0 :(得分:0)
问题中提出了挑战:
...观察使他们进行沟通的复杂性 这两个班级的亲密关系。
当ChangeListener
无法访问内部变量时,任务的重点是让你看到通信变得更加复杂。
为您提供一些提示:您需要能够在LightPanel.Change
内拨打ChangeListener
。如何将LightPanel
的引用发送给ChangeListener
?