📄 joptionpanetest.java
字号:
import java.awt.FlowLayout;
import javax.swing.*;
import java.awt.event.*;
public class JOptionPaneTest extends JFrame implements ActionListener
{
private int MESSAGE_TYPE[] =
{
JOptionPane.ERROR_MESSAGE,
JOptionPane.INFORMATION_MESSAGE,
JOptionPane.WARNING_MESSAGE,
JOptionPane.QUESTION_MESSAGE,
JOptionPane.PLAIN_MESSAGE
};
private int OPTION_TYPE[] =
{
JOptionPane.DEFAULT_OPTION,
JOptionPane.YES_NO_OPTION,
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.OK_CANCEL_OPTION
};
private String MESSAGE_STRING[] =
{
"ERROR_MESSAGE",
"INFORMATION_MESSAGE",
"WARNING_MESSAGE",
"QUESION_MESSAGE",
"PLAIN_MESSAGE"
};
private String OPTION_STRING[] =
{
"DEFAULT_OPTION",
"YES_NO_OPTION",
"YES_NO_CANCEL_OPTION",
"OK_CANCEL_OPTION"
};
private JButton message = new JButton("Message");
private JButton input = new JButton("Input");
private JButton confirm = new JButton("Confirm");
private JComboBox messageType = new JComboBox(MESSAGE_STRING);
private JComboBox optionType = new JComboBox(OPTION_STRING);
public JOptionPaneTest()
{
super("Standard Dialog Test");
JPanelBox buttons = new JPanelBox(new FlowLayout(), "Types");
buttons.add(message);
message.addActionListener(this);
buttons.add(input);
input.addActionListener(this);
buttons.add(confirm);
confirm.addActionListener(this);
JPanelBox options = new JPanelBox(new FlowLayout(), "Options");
options.add(new JLabel("Message Types: "));
options.add(messageType);
options.add(new JLabel("Option Types"));
options.add(optionType);
getContentPane().add("North", buttons);
getContentPane().add("South", options);
addWindowListener(new WindowCloser());
validate();
pack();
setLocationRelativeTo(this);
setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
if (ae.getSource() == message)
JOptionPane.showMessageDialog(this, "Test Message","Message",
MESSAGE_TYPE[messageType.getSelectedIndex()]);
else if (ae.getSource() == input)
JOptionPane.showMessageDialog(this, "Test Input","Input",
MESSAGE_TYPE[messageType.getSelectedIndex()]);
else if (ae.getSource() == confirm)
JOptionPane.showConfirmDialog(this, "Test Confirm","Confirm",
OPTION_TYPE[optionType.getSelectedIndex()],
MESSAGE_TYPE[messageType.getSelectedIndex()]);
}
public static void styleCtrl(String style)
{
try
{
String lf_wind = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
String lf_unix = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
String lf_java = "javax.swing.plaf.metal.MetalLookAndFeel";
if (style.equals("java"))
UIManager.setLookAndFeel(lf_java);
else if (style.equals("unix"))
UIManager.setLookAndFeel(lf_unix);
else if (style.equals("windows"))
UIManager.setLookAndFeel(lf_wind);
}
catch (Exception e)
{
System.err.println("Exception: " + e);
}
}
private class WindowCloser extends WindowAdapter
{
public void windowClosing(WindowEvent we)
{ System.exit(0); }
}
public static void main(String args[])
{
styleCtrl("java");
JOptionPaneTest jopt = new JOptionPaneTest();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -