我目前正在制作一个非常简单的Java GUI应用程序,但遇到了我的变量无法更新的问题。该应用程序是一个简单的篮球记分员,分数整数不会更新,也不会显示标签的文本。没有错误,所以我不确定为什么没有更新。代码:
ScoreWindow.java
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.SpringLayout;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.FlowLayout;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
public class ScoreWindow implements ScoreListener {
private JFrame frmScorewindow;
public volatile JLabel homeScoreLabel;
public JLabel awayScoreLabel;
public volatile int homeScore, awayScore;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ScoreWindow window = new ScoreWindow();
window.frmScorewindow.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public ScoreWindow() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
// Init Scores
homeScore = 0;
awayScore = 0;
frmScorewindow = new JFrame();
frmScorewindow.setResizable(false);
frmScorewindow.setTitle("Score Keeper");
frmScorewindow.setBounds(100, 100, 551, 348);
frmScorewindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmScorewindow.getContentPane().setLayout(null);
JButton homeScore2 = new JButton("+2");
homeScore2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ScoreListener listener = new ScoreWindow();
listener.homeScore(2);
}
});
homeScore2.setBounds(110, 129, 117, 29);
frmScorewindow.getContentPane().add(homeScore2);
JButton homeScore3 = new JButton("+3");
homeScore3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ScoreListener listener = new ScoreWindow();
listener.homeScore(3);
}
});
homeScore3.setBounds(110, 156, 117, 29);
frmScorewindow.getContentPane().add(homeScore3);
JButton awayScore2 = new JButton("+2");
awayScore2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ScoreListener listener = new ScoreWindow();
listener.awayScore(2);
}
});
awayScore2.setBounds(332, 129, 117, 29);
frmScorewindow.getContentPane().add(awayScore2);
JButton awayScore3 = new JButton("+3");
awayScore3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ScoreListener listener = new ScoreWindow();
listener.awayScore(3);
}
});
awayScore3.setBounds(332, 156, 117, 29);
frmScorewindow.getContentPane().add(awayScore3);
JButton resetButton = new JButton("Reset");
resetButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ScoreListener listener = new ScoreWindow();
listener.reset();
}
});
resetButton.setBounds(225, 220, 117, 29);
frmScorewindow.getContentPane().add(resetButton);
homeScoreLabel = new JLabel("000");
homeScoreLabel.setFont(new Font("Lucida Grande", Font.PLAIN, 24));
homeScoreLabel.setHorizontalAlignment(SwingConstants.CENTER);
homeScoreLabel.setBounds(138, 88, 61, 29);
frmScorewindow.getContentPane().add(homeScoreLabel);
awayScoreLabel = new JLabel("000");
awayScoreLabel.setHorizontalAlignment(SwingConstants.CENTER);
awayScoreLabel.setFont(new Font("Lucida Grande", Font.PLAIN, 24));
awayScoreLabel.setBounds(361, 88, 61, 29);
frmScorewindow.getContentPane().add(awayScoreLabel);
JLabel lblHome = new JLabel("Home");
lblHome.setHorizontalAlignment(SwingConstants.CENTER);
lblHome.setBounds(138, 60, 61, 16);
frmScorewindow.getContentPane().add(lblHome);
JLabel lblAway = new JLabel("Away");
lblAway.setHorizontalAlignment(SwingConstants.CENTER);
lblAway.setBounds(361, 60, 61, 16);
frmScorewindow.getContentPane().add(lblAway);
JLabel title = new JLabel("Score Keeper App");
title.setHorizontalAlignment(SwingConstants.CENTER);
title.setBounds(180, 33, 200, 16);
frmScorewindow.getContentPane().add(title);
}
@Override
public void reset() {
print("reset();");
homeScore = 0;
awayScore = 0;
awayScoreLabel.setText("" + awayScore);
homeScoreLabel.setText("" + homeScore);
}
@Override
public void awayScore(int n) {
print("awayScore();");
awayScore+=n;
awayScoreLabel.setText("" + awayScore);
}
@Override
public void homeScore(int n) {
print("homeScore();");
print(homeScoreLabel.getText());
homeScore = homeScore + n;
homeScoreLabel.setText("" + homeScore);
homeScoreLabel.repaint();
homeScoreLabel.revalidate();
}
static void print(Object o) {
System.out.println(o);
}
}
ScoreListener.java
public interface ScoreListener {
public void reset();
public void awayScore(int n);
public void homeScore(int n);
}
谢谢!
答案 0 :(得分:2)
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ScoreWindow window = new ScoreWindow();
window.frmScorewindow.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
使用上面的代码创建窗口。
JButton homeScore2 = new JButton("+2");
homeScore2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ScoreListener listener = new ScoreWindow();
listener.homeScore(2);
}
});
然后你创建了一个窗口的第二个实例。
不要这样做。您只需要创建一次类的实例。所有其他代码都可以引用此实例。
您的ActionListner类在ScoreWindow类中定义,您可以直接引用“homeScore()”方法。