Reputation: 1031
I have this sample. It is a simple program.
Press Me button calls a JOptionPane.
Exit Button calls System.exit
.
The problem is that button OK of JOptionPane
seem to call "else" (alongside buttonExt) and exits the program.
How can I manipulate a JOptionPane
button?
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);}
}
}
Upvotes: 1
Views: 205
Reputation: 13858
else if(e.getSource()==exitBtn);
That trainling ; is your else
, {System.exit(0);}
is a block on it's own.
Correctly formatted this would equal:
else if(e.getSource()==exitBtn) {
;
}
{
System.exit(0);}
}
Correct code:
else if(e.getSource()==exitBtn) {
System.exit(0);
}
Upvotes: 2