OnJava8-Examples/gui/MessageBoxes.java

68 lines
1.9 KiB
Java
Raw Normal View History

2015-04-20 15:36:01 -07:00
//: gui/MessageBoxes.java
// Demonstrates JOptionPane.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import static net.mindview.util.SwingConsole.*;
public class MessageBoxes extends JFrame {
private JButton[] b = {
new JButton("Alert"), new JButton("Yes/No"),
new JButton("Color"), new JButton("Input"),
new JButton("3 Vals")
};
private JTextField txt = new JTextField(15);
2015-05-06 12:09:38 -07:00
private ActionListener al = e -> {
2015-05-05 11:20:13 -07:00
String id = ((JButton)e.getSource()).getText();
switch (id) {
case "Alert":
2015-04-20 15:36:01 -07:00
JOptionPane.showMessageDialog(null,
"There's a bug on you!", "Hey!",
JOptionPane.ERROR_MESSAGE);
2015-05-05 11:20:13 -07:00
break;
case "Yes/No":
2015-04-20 15:36:01 -07:00
JOptionPane.showConfirmDialog(null,
"or no", "choose yes",
JOptionPane.YES_NO_OPTION);
2015-05-05 11:20:13 -07:00
break;
case "Color":
2015-04-20 15:36:01 -07:00
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]);
2015-05-05 11:20:13 -07:00
break;
case "Input": {
2015-04-20 15:36:01 -07:00
String val = JOptionPane.showInputDialog(
2015-05-05 11:20:13 -07:00
"How many fingers do you see?");
2015-04-20 15:36:01 -07:00
txt.setText(val);
2015-05-05 11:20:13 -07:00
break;
}
case "3 Vals": {
2015-04-20 15:36:01 -07:00
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());
2015-05-05 11:20:13 -07:00
break;
2015-04-20 15:36:01 -07:00
}
}
};
public MessageBoxes() {
setLayout(new FlowLayout());
2015-05-18 23:05:20 -07:00
for(JButton b1 : b) {
2015-05-05 14:05:39 -07:00
b1.addActionListener(al);
add(b1);
2015-04-20 15:36:01 -07:00
}
add(txt);
}
public static void main(String[] args) {
run(new MessageBoxes(), 200, 200);
}
} ///:~