按钮的 ActionListener,似乎 JOptionPane 干扰
ActionListener to buttons , seems like JOptionPane interfere
我有这个样本。这是一个简单的程序。
按我按钮调用 JOptionPane。
退出按钮调用 System.exit
。
问题是 JOptionPane
的按钮 OK 似乎调用 "else"(与 buttonExt 一起)并退出程序。
如何操作 JOptionPane
按钮?
package javatests;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
/**
*
* @author fotis
*/
public class AreaCalculation extends JFrame
implements ActionListener {
private JButton button;
private JButton exitBtn;
public static void main(String[] args){
AreaCalculation frame=new AreaCalculation();
frame.setSize(400, 300);
frame.createGUI();
frame.setVisible(true);
}
public void createGUI(){
FlowLayout flow=new FlowLayout();
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container window=getContentPane();
window.setLayout(flow);
button=new JButton("Press me");
window.add(button);
button.addActionListener(this);
exitBtn=new JButton("Exit");
window.add(exitBtn);
exitBtn.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
int area,length,width;
length = 20;
width =10;
area= length*width;
if(e.getSource()==button){
JOptionPane.showMessageDialog(null, "Area is : " + area);
}
else if(e.getSource()==exitBtn);
{System.exit(0);}
}
}
else if(e.getSource()==exitBtn);
那个训练;是你的 else
,{System.exit(0);}
是它自己的一个块。
格式正确这将等于:
else if(e.getSource()==exitBtn) {
;
}
{
System.exit(0);}
}
正确代码:
else if(e.getSource()==exitBtn) {
System.exit(0);
}
我有这个样本。这是一个简单的程序。
按我按钮调用 JOptionPane。
退出按钮调用 System.exit
。
问题是 JOptionPane
的按钮 OK 似乎调用 "else"(与 buttonExt 一起)并退出程序。
如何操作 JOptionPane
按钮?
package javatests;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
/**
*
* @author fotis
*/
public class AreaCalculation extends JFrame
implements ActionListener {
private JButton button;
private JButton exitBtn;
public static void main(String[] args){
AreaCalculation frame=new AreaCalculation();
frame.setSize(400, 300);
frame.createGUI();
frame.setVisible(true);
}
public void createGUI(){
FlowLayout flow=new FlowLayout();
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container window=getContentPane();
window.setLayout(flow);
button=new JButton("Press me");
window.add(button);
button.addActionListener(this);
exitBtn=new JButton("Exit");
window.add(exitBtn);
exitBtn.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
int area,length,width;
length = 20;
width =10;
area= length*width;
if(e.getSource()==button){
JOptionPane.showMessageDialog(null, "Area is : " + area);
}
else if(e.getSource()==exitBtn);
{System.exit(0);}
}
}
else if(e.getSource()==exitBtn);
那个训练;是你的 else
,{System.exit(0);}
是它自己的一个块。
格式正确这将等于:
else if(e.getSource()==exitBtn) {
;
}
{
System.exit(0);}
}
正确代码:
else if(e.getSource()==exitBtn) {
System.exit(0);
}