因此,我已经为我的一个类开发了一个程序,在单击按钮时使用ActionListener来处理事件。我有程序工作,然而,当点击任何按钮,你必须点击输入按钮多次的答案,以登记和移动到程序的下一个层次。
example1:警报按钮打开2,有时是3“嘿,你身上有个bug!”消息对话框。
example2:是/否提示你3-4次向txt返回“是/否”。
example3:在返回输入到txt之前,颜色需要点击3/4。
(我想你明白了.)
为了我的生命,我不知道为什么它不会只需要一个输入就能继续前进.
我的程序代码供你复习..。谢谢你的进阶。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MessageBoxes{
private JButton alert = new JButton("Alert");
private JButton yesNo = new JButton("Yes/No");
private JButton color = new JButton("Color");
private JButton vals = new JButton("3 Vals");
private JButton input = new JButton("Input");
private JTextField txt = new JTextField(15);
private JFrame frame;
private ActionListener al = new ActionListener(){
public void actionPerformed(ActionEvent e){
alert.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
//Execute when button is pressed
JOptionPane.showMessageDialog(null,
"There's a bug on you!", "Hey!",
JOptionPane.ERROR_MESSAGE);
}
});
yesNo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
//Execute when button is pressed
int val = JOptionPane.showConfirmDialog(null,
"Choose yes or no", "Your Call...",
JOptionPane.YES_NO_OPTION);
if(val != JOptionPane.CLOSED_OPTION){
if(val == 0){
txt.setText("Yes");
}
else{
txt.setText("No");
}
}
}
});
color.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
//Execute when button is pressed
Object[] options = {"Red", "Green"};
int sel = JOptionPane.showOptionDialog(null,
"Choose a Color!", "Warning",
JOptionPane.DEFAULT_OPTION,
JOptionPane.WARNING_MESSAGE, null,
options, options[0]);
if(sel != JOptionPane.CLOSED_OPTION){
txt.setText("Color Selected: " + options[sel]);
}
}
});
vals.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
//Execute when button is pressed
Object[] selections = {"First", "Second", "Third"};
Object val = JOptionPane.showInputDialog(null,
"Choose one", "Input",
JOptionPane.INFORMATION_MESSAGE,
null, selections, selections[0]);
if(val != null){
txt.setText(val.toString());
}
}
});
input.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
//Execute when button is pressed
String val = JOptionPane.showInputDialog("How mant fingers do you see?");
txt.setText(val);
}
});
}
};
public MessageBoxes(){
frame = new JFrame("Title");
frame.setSize(250, 250);
frame.getContentPane().setLayout(new FlowLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(alert);
frame.getContentPane().add(yesNo);
frame.getContentPane().add(color);
frame.getContentPane().add(vals);
frame.getContentPane().add(input);
frame.getContentPane().add(txt);
frame.setVisible(true);
}
public void launchJFrame(){
alert.addActionListener(al);
yesNo.addActionListener(al);
color.addActionListener(al);
vals.addActionListener(al);
input.addActionListener(al);
}
public static void main(String [] args){
MessageBoxes messageBoxes = new MessageBoxes();
messageBoxes.launchJFrame();
}
}发布于 2014-08-18 02:47:43
问题是,每次在您的actionPerformed中调用ActionListener al时,它都会使用您的按钮注册新的ActionListener。
private ActionListener al = new ActionListener() {
public void actionPerformed(ActionEvent e) {
alert.addActionListener(new ActionListener() {
//...
});
yesNo.addActionListener(new ActionListener() {
//...
});
color.addActionListener(new ActionListener() {
//...
});
vals.addActionListener(new ActionListener() {
//...
});
input.addActionListener(new ActionListener() {
//...
});
}
};因此,每次调用actionPerformed方法时,每个按钮都会注册一个新的actionListener。
相反,您可以使用if-else语句来确定事件的源,例如.
Object source = e.getSource();
if (source == alert) {
JOptionPane.showMessageDialog(null,
"There's a bug on you!", "Hey!",
JOptionPane.ERROR_MESSAGE);
} else if (...或者您可以完全摆脱ActionListener al,简单地将单个ActionListener注册到launchJFrame方法中的按钮.
public void launchJFrame() {
alert.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//Execute when button is pressed
}
});
yesNo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//Execute when button is pressed
int val = JOptionPane.showConfirmDialog(null,
"Choose yes or no", "Your Call...",
JOptionPane.YES_NO_OPTION);
if (val != JOptionPane.CLOSED_OPTION) {
if (val == 0) {
txt.setText("Yes");
} else {
txt.setText("No");
}
}
}
});
color.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//Execute when button is pressed
Object[] options = {"Red", "Green"};
int sel = JOptionPane.showOptionDialog(null,
"Choose a Color!", "Warning",
JOptionPane.DEFAULT_OPTION,
JOptionPane.WARNING_MESSAGE, null,
options, options[0]);
if (sel != JOptionPane.CLOSED_OPTION) {
txt.setText("Color Selected: " + options[sel]);
}
}
});
vals.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//Execute when button is pressed
Object[] selections = {"First", "Second", "Third"};
Object val = JOptionPane.showInputDialog(null,
"Choose one", "Input",
JOptionPane.INFORMATION_MESSAGE,
null, selections, selections[0]);
if (val != null) {
txt.setText(val.toString());
}
}
});
input.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//Execute when button is pressed
String val = JOptionPane.showInputDialog("How mant fingers do you see?");
txt.setText(val);
}
});
}发布于 2014-08-18 02:47:41
这是因为您的actionlistener构造/添加嵌套在另一个actionlistener中,al。因此,您所关心的actionlisteners (弹出消息的那些)实际上不会添加到按钮中,直到您单击它们一次。在launchJFrame()中将actionlisteners添加到按钮中可以解决这个问题。下面是您的代码的固定版本:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MessageBoxes{
private JButton alert = new JButton("Alert");
private JButton yesNo = new JButton("Yes/No");
private JButton color = new JButton("Color");
private JButton vals = new JButton("3 Vals");
private JButton input = new JButton("Input");
private JTextField txt = new JTextField(15);
private JFrame frame;
public MessageBoxes(){
frame = new JFrame("Title");
frame.setSize(250, 250);
frame.getContentPane().setLayout(new FlowLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(alert);
frame.getContentPane().add(yesNo);
frame.getContentPane().add(color);
frame.getContentPane().add(vals);
frame.getContentPane().add(input);
frame.getContentPane().add(txt);
frame.setVisible(true);
}
public void launchJFrame(){
alert.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
//Execute when button is pressed
JOptionPane.showMessageDialog(null,
"There's a bug on you!", "Hey!",
JOptionPane.ERROR_MESSAGE);
}
});
yesNo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
//Execute when button is pressed
int val = JOptionPane.showConfirmDialog(null,
"Choose yes or no", "Your Call...",
JOptionPane.YES_NO_OPTION);
if(val != JOptionPane.CLOSED_OPTION){
if(val == 0){
txt.setText("Yes");
}
else{
txt.setText("No");
}
}
}
});
color.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
//Execute when button is pressed
Object[] options = {"Red", "Green"};
int sel = JOptionPane.showOptionDialog(null,
"Choose a Color!", "Warning",
JOptionPane.DEFAULT_OPTION,
JOptionPane.WARNING_MESSAGE, null,
options, options[0]);
if(sel != JOptionPane.CLOSED_OPTION){
txt.setText("Color Selected: " + options[sel]);
}
}
});
vals.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
//Execute when button is pressed
Object[] selections = {"First", "Second", "Third"};
Object val = JOptionPane.showInputDialog(null,
"Choose one", "Input",
JOptionPane.INFORMATION_MESSAGE,
null, selections, selections[0]);
if(val != null){
txt.setText(val.toString());
}
}
});
input.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
//Execute when button is pressed
String val = JOptionPane.showInputDialog("How mant fingers do you see?");
txt.setText(val);
}
});
}
public static void main(String [] args){
MessageBoxes messageBoxes = new MessageBoxes();
messageBoxes.launchJFrame();
}
}发布于 2014-08-18 03:00:47
以下是我发现的最适合我的解决方案:
private ActionListener alertAction = new ActionListener(){
public void actionPerformed(ActionEvent e){
//Execute when button is pressed
JOptionPane.showMessageDialog(null,
"There's a bug on you!", "Hey!",
JOptionPane.ERROR_MESSAGE);
}
};
private ActionListener yesNoAction = new ActionListener(){
public void actionPerformed(ActionEvent e){
//Execute when button is pressed
//Execute when button is pressed
int val = JOptionPane.showConfirmDialog(null,
"Choose yes or no", "Your Call...",
JOptionPane.YES_NO_OPTION);
if(val != JOptionPane.CLOSED_OPTION){
if(val == 0){
txt.setText("Yes");
}
else{
txt.setText("No");
}
}
}
};
private ActionListener colorAction = new ActionListener(){
public void actionPerformed(ActionEvent e){
//Execute when button is pressed
Object[] options = {"Red", "Green"};
int sel = JOptionPane.showOptionDialog(null,
"Choose a Color!", "Warning",
JOptionPane.DEFAULT_OPTION,
JOptionPane.WARNING_MESSAGE, null,
options, options[0]);
if(sel != JOptionPane.CLOSED_OPTION){
txt.setText("Color Selected: " + options[sel]);
}
}
};
private ActionListener valsAction = new ActionListener(){
public void actionPerformed(ActionEvent e){
//Execute when button is pressed
Object[] selections = {"First", "Second", "Third"};
Object val = JOptionPane.showInputDialog(null,
"Choose one", "Input",
JOptionPane.INFORMATION_MESSAGE,
null, selections, selections[0]);
if(val != null){
txt.setText(val.toString());
}
}
};
private ActionListener inputAction = new ActionListener(){
public void actionPerformed(ActionEvent e){
//Execute when button is pressed
String val = JOptionPane.showInputDialog("How mant fingers do you see?");
txt.setText(val);
}
};
public MessageBoxes(){
frame = new JFrame("Title");
frame.setSize(250, 250);
frame.getContentPane().setLayout(new FlowLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(alert);
frame.getContentPane().add(yesNo);
frame.getContentPane().add(color);
frame.getContentPane().add(vals);
frame.getContentPane().add(input);
frame.getContentPane().add(txt);
frame.setVisible(true);
}
public void launchJFrame(){
alert.addActionListener(alertAction);
yesNo.addActionListener(yesNoAction);
color.addActionListener(colorAction);
vals.addActionListener(valsAction);
input.addActionListener(inputAction);
}https://stackoverflow.com/questions/25355587
复制相似问题