-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathJDialog_JavaTutorial.java
More file actions
59 lines (57 loc) · 2.45 KB
/
Copy pathJDialog_JavaTutorial.java
File metadata and controls
59 lines (57 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
/**
*
* @author Vincent
*/
public class JDialog_JavaTutorial {
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
JFrame window = new JFrame("JDialog: Java Tutorial");
final JPanel TopPanel = new JPanel();
final JButton addButton = new JButton("Add numbers");
final JTextField answerText = new JTextField(10);
Font font1 = new Font("SansSerif", Font.BOLD, 48);
answerText.setFont(font1);
answerText.setText("Answer");
answerText.setEditable(false);
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
String firstNumber = JOptionPane.showInputDialog("Enter first number");
String secondNumber = JOptionPane.showInputDialog("Enter second number");
int answer = Integer.parseInt(firstNumber) + Integer.parseInt(secondNumber);
answerText.setText(Integer.toString(answer));
JOptionPane.showMessageDialog(null, firstNumber + " + " + secondNumber + " = " + answer);
int confirm = JOptionPane.showOptionDialog(null, "You want to start again?", "Again?", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null);
/*
0 = yes
1 = no
*/
if (confirm != 1) {
addButton.doClick();
} else {
//no
}
} catch (Exception ex) {
}
}
});
TopPanel.add(addButton);
window.add(BorderLayout.NORTH, TopPanel);
window.add(BorderLayout.CENTER, answerText);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(500, 500);
window.setVisible(true);
window.setLocationRelativeTo(null);
}
}