gpt4 book ai didi

java - 更改类之间的 JLabels

转载 作者:行者123 更新时间:2023-12-01 15:41:11 25 4
gpt4 key购买 nike

我的第一篇文章,所以请原谅任何不正确的礼仪。我目前正在为学校做年终项目,我需要一些帮助。我正在 Netbeans 中制作一个 GUI java 应用程序。我有两个类。一个是控制计时器的类,另一个是记分板屏幕的类。我需要用timerClass 中倒计时的时间来更新记分板timerLabel。它非常困惑,因为 Timer 类中有另一个计时器标签它会更新。我的问题是我无法更新 MatchScreen() 中的timerLabel。这是我的代码:

定时器类

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

public class TimerClass extends JFrame {

Timer timer;
JLabel promptLabel, timerLabel;
int counter;
JTextField tf;
JButton button;
MatchScreen call = null;

public TimerClass() {
call = new MatchScreen();
setLayout(new GridLayout(4, 4, 7, 7));
promptLabel = new JLabel(""
+ "Enter number of seconds for the timer",
SwingConstants.CENTER);
add(promptLabel);
tf = new JTextField(5);
add(tf);
button = new JButton("Start");
add(button);
timerLabel = new JLabel("waiting...",
SwingConstants.CENTER);
add(timerLabel);

event e = new event();
button.addActionListener(e);
System.out.println("Button pressed");
}

public class event implements ActionListener {

public void actionPerformed(ActionEvent e) {
System.out.println("Action performed");
int count = (int) (Double.parseDouble(tf.getText()));
timerLabel.setText("Time left: " + count);
call.setTimerLabel(count);
System.out.println("Passed count to tc");
TimeClass tc = new TimeClass(count);
timer = new Timer(1000, tc);
System.out.println("Timer.start");
timer.start();
//throw new UnsupportedOperationException("Not supported yet.");
}

/*public void actionPerformed(ActionEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}*/
}

public class TimeClass implements ActionListener {

int counter;

public TimeClass(int counter) {
this.counter = counter;
}

public void actionPerformed(ActionEvent e) {
counter--;
if (counter >= 1) {
call.setTimerLabel(counter);
} else {
timerLabel.setText("END");
timer.stop();
Toolkit.getDefaultToolkit().beep();
}
}
}

public static void main(String args[]) {
TimerClass gui = new TimerClass();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setSize(250, 150);
gui.setTitle("Time Setup");
gui.setVisible(true);
}
}

现在是记分板屏幕

public class MatchScreen extends javax.swing.JFrame {

int redScore = 0, blueScore = 0, blueCat1 = 0,
blueCat2 = 0, redCat1 = 0, redCat2 = 0, winner = 0;

public MatchScreen() {
initComponents();
}

//Determine Winner of the match
public int getWinner() {
if (redScore > blueScore) {
winner = 1;
} else {
winner = 2;
}
return winner;
}

public void setTimerLabel(int a) {
int time = a;
while (time >= 1) {
timerLabel.setText("" + time);
}
if (time < 1) {
timerLabel.setText("End");
}
}

private void jButton13ActionPerformed(java.awt.event.ActionEvent evt) {
//Creates an object of the timerClass
TimerClass gui = new TimerClass();
gui.setSize(300, 175);
gui.setTitle("Time Setup");
gui.setVisible(true);
}
}

MatchScreen() 中省略了一些我认为不相关的代码。

非常感谢

最佳答案

设法解决一般问题。我将所有代码放入一个类中。不理想,但它有效:/无论如何,最后期限迫在眉睫。

真诚的感谢。

关于java - 更改类之间的 JLabels,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8014629/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com