我想返回 count 的新值,以便下面的 while 语句可以更改标签的文本。除了这个以外,其他一切都有效。
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
public class beepBoop extends JApplet {
private JButton play = new JButton("Play");
private JButton loop = new JButton("Loop");
private JButton stop = new JButton("Stop");
JLabel songTitle = new JLabel("Test");
JLabel amountClicks = new JLabel();
JLabel warning = new JLabel("KEEP ON LISTENING!!!");
private AudioClip audioClip;
int count = 0;
public beepBoop() {
audioClip = Applet.newAudioClip(getClass().getResource("song1.wav"));
JPanel pane = new JPanel(new FlowLayout());
JPanel panel = new JPanel();
panel.add(play);
panel.add(loop);
panel.add(stop);
panel.add(songTitle);
panel.add(amountClicks);
panel.add(warning);
stop.setEnabled(false);
add(panel, BorderLayout.CENTER);
play.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
start();
songTitle.setText("LOVE THE SONG");
count++;
amountClicks.setText("You have pressed button " + count + " times.");
}
});
loop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
loop();
songTitle.setText("YOU REALLY LOVE THIS SONG");
}
});
stop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
stop();
songTitle.setText("WOW YOU ALREADY QUIT IT!!!");
}
});
int countTotal = count;
while (countTotal >= 5) {
if (countTotal >= 5) {
warning.setText("MAYBE YOU'VE HAD TOO MUCH!!!");
} else if (countTotal >= 10) {
warning.setText("YOU HAVE HAD WAYYY TO MUCH!!!");
} else {
warning.setText("WOW!!!");
}
}
}
public void start() {
audioClip.play();
play.setEnabled(false);
stop.setEnabled(true);
loop.setEnabled(true);
}
public void loop() {
stop();
audioClip.loop();
loop.setEnabled(false);
play.setEnabled(true);
stop.setEnabled(true);
}
public void stop() {
audioClip.stop();
stop.setEnabled(false);
loop.setEnabled(true);
play.setEnabled(true);
}
public static void main(String[] args) {
beepBoop player = new beepBoop();
JFrame frame = new JFrame("beepBoop");
beepBoop applet = new beepBoop();
applet.init();
frame.getContentPane().add(applet, BorderLayout.CENTER);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(225,275);
frame.setVisible(true);
}
}
因此,回顾一下我想做的就是获取 play.addActionListener 之外的变量,以便下面的 while 语句可以在触发一定量的点击后显示正确的响应。
最佳答案
I would like to return the new value for count so that the while statement below can change the text of a label.
不,你真的不知道。严重地。这段代码:
while (countTotal >= 5) {
if (countTotal >= 5) {
warning.setText("MAYBE YOU'VE HAD TOO MUCH!!!");
} else if (countTotal >= 10) {
warning.setText("YOU HAVE HAD WAYYY TO MUCH!!!");
} else {
warning.setText("WOW!!!");
}
}
属于线性用户界面控制台程序,在该程序中,您可以准确地知道代码流何时将进入此循环,并且当它进入时,它必须保留在循环中,直到用户完成输入数据。
但这不是 Swing(或任何使用事件驱动范例的)程序的工作方式,因为使用事件驱动代码,您无法完全控制程序流程,因为用户可以自由地与任何 GUI 交互呈现给他并启用的组件。
因此,您可能希望在某个 ActionListener 中使用计数器变量,并根据计数器变量的状态更改监听器的行为。 if/else 或 switch block 可以用于此目的。
所以,也许......
play.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
start();
songTitle.setText("LOVE THE SONG");
count++;
amountClicks.setText("You have pressed button " + count + " times.");
if (countTotal >= 5) {
warning.setText("MAYBE YOU'VE HAD TOO MUCH!!!");
} else if (countTotal >= 10) {
warning.setText("YOU HAVE HAD WAYYY TO MUCH!!!");
} else {
warning.setText("WOW!!!");
}
}
});
关于java - 如何在 JFrame 中获取事件监听器外部的变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35762493/