JOptionPane轻便应用,不需要另外写Jframe
代码案例:(抛砖引玉)
package com.kancy.wintools;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeModel;
import java.awt.*;
/**
* JOptionPane 高级扩展应用
* @author huangchengkang
* @date 2018-09-03 9:37
*/
public class JOptionPaneAdvancedExt {
public static void main(String[] args) {
Dimension jScrollPaneDimension = new Dimension(1300, 600);
JPasswordField pwd = new JPasswordField();
JTextArea textArea = new JTextArea();
textArea.setRows(5);
textArea.setColumns(40);
textArea.setLineWrap(true);
JScrollPane jScrollPane = new JScrollPane(textArea);
jScrollPane.setPreferredSize(new Dimension(450, 100));
JButton button = new JButton("提交");
JButton button1 = new JButton("注册");
JButton button2 = new JButton("登录");
JButton button3 = new JButton("提出");
JPanel bPanel = new JPanel();
bPanel.add(button1);
bPanel.add(button2);
bPanel.add(button3);
bPanel.setBorder(new TitledBorder("功能按钮"));
bPanel.setLayout(new GridLayout(0,3));
JList jList = new JList();
jList.setListData(new Object[]{"A","B","C","D"});
jList.setSelectedIndex(0);
final JMenuBar jMenuBar = new JMenuBar();
JMenu menu = new JMenu("菜单");
JMenuItem item1 = menu.add("辅助");
menu.addSeparator();
JMenuItem item2 = menu.add("法师");
jMenuBar.add(menu);
JTree jTree = new JTree();
DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("中国");
// 创建二级节点
DefaultMutableTreeNode gdNode = new DefaultMutableTreeNode("广东");
DefaultMutableTreeNode fjNode = new DefaultMutableTreeNode("福建");
DefaultMutableTreeNode shNode = new DefaultMutableTreeNode("上海");
DefaultMutableTreeNode twNode = new DefaultMutableTreeNode("台湾");
// 把二级节点作为子节点添加到根节点
rootNode.add(gdNode);
rootNode.add(fjNode);
rootNode.add(shNode);
rootNode.add(twNode);
TreeModel treeModel = new DefaultTreeModel(rootNode);
jTree.setModel(treeModel);
JScrollPane jScrollPane_jTree = new JScrollPane(jTree){
@Override
public Dimension getPreferredSize() {
return new Dimension(450, 120);
}
};
JPopupMenu.Separator separator = new JPopupMenu.Separator();
JRadioButton jRadioButton1 = new JRadioButton("男");
JRadioButton jRadioButton2 = new JRadioButton("女");
JRadioButton jRadioButton3 = new JRadioButton("未知");
// 单选
ButtonGroup group = new ButtonGroup();
group.add(jRadioButton1);
group.add(jRadioButton2);
group.add(jRadioButton3);
JPanel jPanel = new JPanel();
jPanel.add(jRadioButton1);
jPanel.add(jRadioButton2);
jPanel.add(jRadioButton3);
jPanel.setBorder(new TitledBorder("选择性别"));
jPanel.setLayout(new GridLayout(0,6));
JComboBox jComboBox = new JComboBox();
jComboBox.addItem("JAVA");
jComboBox.addItem("C++");
jComboBox.addItem("PHP");
jComboBox.setEnabled(true);
jComboBox.setEditable(true);
JProgressBar jProgressBar = new JProgressBar();
jProgressBar.setMaximum(100);
jProgressBar.setMinimum(0);
jProgressBar.setValue(1);
startJProgressBar(jProgressBar);
String[] columnNames ={ "姓名", "学号", "性别"};
Object[][] obj = new Object[][]{
{"张三","1","男"},
{"李四","2","女"}
};
JTable table = new JTable(obj, columnNames);
JScrollPane jScrollPane_table = new JScrollPane(table){
@Override
public Dimension getPreferredSize() {
return new Dimension(500, 100);
}
};
// 创建选项卡面板
final JTabbedPane tabbedPane = new JTabbedPane();
// 创建第 1 个选项卡(选项卡只包含 标题)
tabbedPane.addTab("Tab01", createTextPanel("TAB 01"));
// 创建第 2 个选项卡(选项卡包含 标题 和 图标)
tabbedPane.addTab("Tab02", new ImageIcon("bb.jpg"), createTextPanel("TAB 02"));
// 创建第 3 个选项卡(选项卡包含 标题、图标 和 tip提示)
tabbedPane.addTab("Tab03", new ImageIcon("bb.jpg"), createTextPanel("TAB 03"), "This is a tab.");
// 添加选项卡选中状态改变的监听器
tabbedPane.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
System.out.println("当前选中的选项卡: " + tabbedPane.getSelectedIndex());
}
});
// 设置默认选中的选项卡
tabbedPane.setSelectedIndex(1);
Object[] message = {jMenuBar,"姓名:", "黄成康",
separator,
"输入密码:",pwd,
jPanel,
jComboBox,
jScrollPane_jTree,
"选择答案:", jList,
"请输入备注:",jScrollPane,
jScrollPane_table,
tabbedPane,
jProgressBar,
bPanel,
button
};
int i = JOptionPane.showOptionDialog(null, message, "测试JOptionPane参数", JOptionPane.DEFAULT_OPTION,
-1, null, new Object[]{}, null);
System.out.println(jList.getSelectedValue());
System.out.println(jComboBox.getSelectedItem());
System.out.println(i);
System.exit(0);
}
/**
* 创建一个面板,面板中心显示一个标签,用于表示某个选项卡需要显示的内容
*/
private static JComponent createTextPanel(String text) {
// 创建面板, 使用一个 1 行 1 列的网格布局(为了让标签的宽高自动撑满面板)
JPanel panel = new JPanel(new GridLayout(1, 1));
// 创建标签
JLabel label = new JLabel(text);
label.setFont(new Font(null, Font.PLAIN, 50));
label.setHorizontalAlignment(SwingConstants.CENTER);
// 添加标签到面板
panel.add(label);
return panel;
}
private static void startJProgressBar(final JProgressBar jProgressBar) {
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i <= 101; i++) {
if(i == 101){
i = 0;
}
jProgressBar.setValue(i);
jProgressBar.setString(i + "%");
jProgressBar.setStringPainted(true);
sleep(100);
}
}
private void sleep(long time) {
try {
Thread.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
}
运行效果图: