我将在前面说过我之前没有任何Java图形等经验,所以代码可能是非常错误的...也是java的新手,所以请在解释时尽量保持简单:P
好的,所以我查看了API并找到了重绘方法,但我不完全确定如何使用它。 我的代码目前是:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
/* Class extending Jframe */
public class Train extends JFrame {
String onOffValue = "OFF";
String dOpenValue = "CLOSED";
String tMovingValue = "NO";
String tReadyValue = "NO";
String trainInfoContent = "AH HERROOOO!!";
int stationNo = 0;
/* Constuctor method for the object */
Train() {
/* setting the frame title */
super("Train Control System");
/* calling buildGUI to set up the interface */
buildGUI();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
private void buildGUI() {
JPanel station = new JPanel();
station.setLayout(new BorderLayout());
JPanel Graphic = new JPanel();
Graphic g = new Graphic(stationNo);
Graphic.add(g);
station.add(Graphic, BorderLayout.NORTH);
station.setBorder(new TitledBorder(new EtchedBorder(), " Station Information "));
add(station, BorderLayout.NORTH);
JPanel trains = new JPanel();
trains.setLayout(new BorderLayout());
JTextArea trainInfo = new JTextArea(trainInfoContent, 3, 70);
trains.add(trainInfo);
trains.setBorder(new TitledBorder(new EtchedBorder(), " Train Information "));
add(trains, BorderLayout.CENTER);
JPanel controls = new JPanel();
controls.setLayout(new BorderLayout());
JPanel buttons = new JPanel();
buttons.setLayout(new GridLayout(3, 2));
buttons.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
Button start = new Button("Start Train");
buttons.add(start);
update test = new update(trainInfo, stationNo, Graphic);
start.addActionListener(test);
Button open = new Button("Open Doors");
buttons.add(open);
Button close = new Button("Close Doors");
buttons.add(close);
Button go = new Button("Go");
buttons.add(go);
Button stop = new Button("Stop");
buttons.add(stop);
Button shutdown = new Button("Shutdown Train");
buttons.add(shutdown);
controls.add(buttons, BorderLayout.WEST);
JPanel display = new JPanel();
display.setLayout(new GridLayout(2, 4));
JTextField onOffTitle = new JTextField("Train on: ");
onOffTitle.setEditable(false);
display.add(onOffTitle);
JTextField onOff = new JTextField(onOffValue);
onOff.setEditable(false);
display.add(onOff);
JTextField dOpenTitle = new JTextField("Doors Open: ");
dOpenTitle.setEditable(false);
display.add(dOpenTitle);
JTextField dOpen = new JTextField(dOpenValue);
dOpen.setEditable(false);
display.add(dOpen);
JTextField tMovingTitle = new JTextField("Train Moving: ");
tMovingTitle.setEditable(false);
display.add(tMovingTitle);
JTextField tMoving = new JTextField(tMovingValue);
tMoving.setEditable(false);
display.add(tMoving);
JTextField tReadyTitle = new JTextField("Train Ready: ");
tReadyTitle.setEditable(false);
display.add(tReadyTitle);
JTextField tReady = new JTextField(tReadyValue);
tReady.setEditable(false);
display.add(tReady);
controls.add(display, BorderLayout.CENTER);
add(controls, BorderLayout.SOUTH);
}
/* Main method declaring a new train control system object */
public static void main(String[] args) {
Train TCS = new Train();
}
}
class Graphic extends JPanel {
int station;
Graphic(int s) {
station = s;
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.drawString("Darlington", 1, 36);
g2.drawString("North Road", 140, 36);
g2.drawString("Heighington", 280, 36);
g2.drawString("Shildon", 436, 36);
g2.drawString("Newton Aycliffe", 560, 36);
g2.drawString("Bishop Auckland", 698, 36);
g2.setStroke(new BasicStroke(5));
g2.drawLine(25, 11, 742, 11);
g2.setColor(Color.gray);
g2.drawLine(25, 11, 25+(station*143), 11);
g2.setStroke(new BasicStroke(9));
int x = 23;
for (int i = 0; i < 6; i++) {
g2.drawOval(x, 7, 10, 10);
x = x + 143;
}
g2.setColor(Color.black);
x = 23 + (station*143);
for (int i = 0; i < 6; i++) {
g2.drawOval(x, 7, 10, 10);
x = x + 143;
}
g2.setColor(Color.white);
x = 23;
for (int i = 0; i < 6; i++) {
g2.fillOval(x, 7, 10, 10);
x = x + 143;
}
g2.setColor(Color.red);
g2.fillOval(23 + (143*station), 7, 10, 10);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(800, 40);
}
}
class update implements ActionListener {
JTextArea output;
int stationNo;
JPanel graphic;
update(JTextArea out, int station, JPanel p) {
output = out;
stationNo = station;
graphic = p;
}
public void actionPerformed(ActionEvent e) {
output.append("Start Train");
stationNo = stationNo + 1;
graphic.repaint();
output.append(" " + stationNo);
}
}
如果有很多代码,我很抱歉,我不确定人们需要提供哪些帮助:/
我可以看到为什么我的代码目前不起作用,但它是我能够最接近它工作的...
忘了提前说谢谢! :) (我的举止在哪里)
答案 0 :(得分:0)
请先研究一下Java编码格式。然后开始编码 APP。 java中的每个变量都必须以小的开头 信件。只有班级将以大写字母开头。只需改变它。
JPanel Graphic = new JPanel();
Graphic g = new Graphic(stationNo);
Graphic.add(g);
station.add(Graphic, BorderLayout.NORTH);
以强>
JPanel graphic = new JPanel();
Graphic g = new Graphic(stationNo);
graphic.add(g);
station.add(graphic, BorderLayout.NORTH);
然后你可以在while循环中使用repaint()方法。当重绘() 方法被称为paintComponent(Graphics g)方法将被触发 并且将执行其中的代码。
答案 1 :(得分:0)
所以我设法让我的代码正常工作。
问题是我在我的Graphic类中需要一个方法来更新对象的StationNo,而在更新类中我应该接受一个Graphic对象而不是JPanel。
我所做的更改是:
class update implements ActionListener {
JTextArea output;
int stationNo;
Graphic graphic;
update(JTextArea out, int station, Graphic p) {
output = out;
stationNo = station;
graphic = p;
}
public void actionPerformed(ActionEvent e) {
output.append("Start Train");
stationNo = stationNo + 1;
graphic.newStation(stationNo);
graphic.repaint();
output.append(" " + stationNo);
}
}
并且newStation方法只是
public void newStation(int s) {
stationNo = s;
}
所以,我只是愚蠢的想法......