java你如何显示选择的项_使用showOptionDialog显示多项选择框

  • Post author:
  • Post category:java


packagecom.siwuxie095.showdialog;

importjava.awt.BorderLayout;

importjava.awt.EventQueue;

importjavax.swing.JFrame;

importjavax.swing.JOptionPane;

importjavax.swing.JPanel;

importjavax.swing.UIManager;

importjavax.swing.UnsupportedLookAndFeelException;

importjavax.swing.border.EmptyBorder;

importcom.sun.java.swing.plaf.windows.WindowsLookAndFeel;

importcom.sun.org.apache.xerces.internal.impl.xs.SchemaSymbols;

importjavax.swing.JButton;

importjava.awt.event.MouseAdapter;

importjava.awt.event.MouseEvent;

public classTestOptionDialog extendsJFrame {

privateJPanel contentPane;

/**

* Launch the application.

*/

public staticvoidmain(String[] args) {

EventQueue.invokeLater(newRunnable() {

publicvoidrun() {

try{

TestOptionDialog frame = newTestOptionDialog();

frame.setVisible(true);

} catch(Exception e) {

e.printStackTrace();

}

}

});

}

/**

* Create the frame.

*/

publicTestOptionDialog() {

try{

UIManager.setLookAndFeel(newWindowsLookAndFeel());

} catch(UnsupportedLookAndFeelException e) {

e.printStackTrace();

}

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setBounds(100, 100, 450, 300);

contentPane = newJPanel();

contentPane.setBorder(newEmptyBorder(5, 5, 5, 5));

contentPane.setLayout(newBorderLayout(0, 0));

setContentPane(contentPane);

JButton btnshowoptiondialog = newJButton(“显示多项选择框(showOptionDialog)”);

//为按钮添加鼠标点击事件

btnshowoptiondialog.addMouseListener(newMouseAdapter() {

@Override

publicvoidmouseClicked(MouseEvent e) {

/**

*直接通过静态方法调用

*需要指定父级窗体,信息,标题,选项类型,

*信息类型,图标,可选值(数组),初始值(默认输入值)

*返回值是int类型,创建以接收返回值

*如果返回0,对应”方案1″ …

*如果返回CLOSED_OPTION,则该窗口关闭,没有选择

*没有关闭多项选择框时,后面的主窗体是完全无法操作的(即阻塞)

*/

String options[]={“方案1″,”方案2″,”方案3”};

intvalue=JOptionPane.showOptionDialog(TestOptionDialog.this, “选择一个方案:”,

“方案”, JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null,

options, “方案1”);

if(value!=JOptionPane.CLOSED_OPTION) {

switch(value) {

case0:System.out.println(“你选择了方案1”);break;

case1:System.out.println(“你选择了方案2”);break;

case2:System.out.println(“你选择了方案3”);break;

default:

break;

}

}

}

});

btnshowoptiondialog.setFocusable(false);

contentPane.add(btnshowoptiondialog, BorderLayout.NORTH);

}

}



版权声明:本文为weixin_29083649原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。