swing实现对话框 我想在点击JMenu的一个子项JMemuItem后,弹出一个框(可以是面板pane或对话框),框里有两个文本text,用来输入时间,还有一个JComboBox(下拉菜单),应该怎么写。请帮忙解疑,谢谢。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 JDialog w =new JDialog(your_MainWindow);JPanel p=new JPanel(new GridBagLayout);//GridBagLayout布局放你的文本框和组合框p.add();//添加文本框组合框...···w.setVisible(true);//点击JMenuItem的时候 JOptionPane.showConfirmDialog(null,component,"",YES_NO_OPTION);component可以用自定义的组件,比如定义个JPanel,把很多东西加到Panel上,再把Panel显示在这个ConfirmDialog上不就可以了吗. package test;import java.awt.BorderLayout;import java.awt.Dimension;import java.awt.FlowLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JComboBox;import javax.swing.JDialog;import javax.swing.JFrame;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JMenuItem;import javax.swing.JPanel;import javax.swing.JTextField;public class TestJFrame extends JFrame implements ActionListener { private static final long serialVersionUID = -362233428598274611L; private JMenuItem mi = null; public static void main(String[] args) { new TestJFrame(); } public TestJFrame() { init(); } private void init() { JMenuBar menuBar = new JMenuBar(); JMenu menu = new JMenu("Menu"); menuBar.add(menu); mi = new JMenuItem("test"); mi.addActionListener(this); menu.add(mi); this.setJMenuBar(menuBar); this.getContentPane().setLayout(new BorderLayout()); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(new Dimension(300, 300)); this.setVisible(true); } public void actionPerformed(ActionEvent e) { if (e.getSource().equals(mi)) { new MyDialog(); } }}class MyDialog extends JDialog { private static final long serialVersionUID = 4463681153478016484L; public MyDialog() { super(); this.setModal(true); init(); } private void init() { this.getContentPane().setLayout(new BorderLayout()); this.getContentPane().add(getCenterPanel(), BorderLayout.CENTER); this.setSize(new Dimension(200, 200)); this.setVisible(true); } private JPanel getCenterPanel() { JPanel panel = new JPanel(); panel.setLayout(new FlowLayout(FlowLayout.CENTER)); JTextField field1 = new JTextField(5); JTextField field2 = new JTextField(5); JComboBox cb = new JComboBox(); cb.addItem("cb1"); cb.addItem("cb2"); cb.addItem("cb3"); panel.add(field1); panel.add(field2); panel.add(cb); return panel; }} java调用ocx java跟硬件结合, 如何找到Java注释 请问这个class变量是怎么来的 帮忙看看哪里错了 如何将ACCESS的表转换为一个数组 一些很有意思的基本概念,大家进来讨论讨论! 动态编译问题 100分求文件目录搜索原代码 我开发的MasterMoney重新写了一遍,升级到v1.4.0,欢迎测试 如何用JAVA实现随机字符串和随机数的生成. 如何通过JAVA 实现屏蔽键盘快捷键? 求教 事件处理 问题
JPanel p=new JPanel(new GridBagLayout);//GridBagLayout布局放你的文本框和组合框
p.add();//添加文本框组合框
...
···
w.setVisible(true);//点击JMenuItem的时候
component可以用自定义的组件,比如定义个JPanel,把很多东西加到Panel上,再把Panel显示在这个ConfirmDialog上不就可以了吗.
package test;import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextField;public class TestJFrame extends JFrame implements ActionListener { private static final long serialVersionUID = -362233428598274611L; private JMenuItem mi = null; public static void main(String[] args) {
new TestJFrame();
} public TestJFrame() {
init();
} private void init() {
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("Menu");
menuBar.add(menu);
mi = new JMenuItem("test");
mi.addActionListener(this);
menu.add(mi);
this.setJMenuBar(menuBar);
this.getContentPane().setLayout(new BorderLayout());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(new Dimension(300, 300));
this.setVisible(true);
} public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(mi)) {
new MyDialog();
}
}}class MyDialog extends JDialog { private static final long serialVersionUID = 4463681153478016484L; public MyDialog() {
super();
this.setModal(true);
init();
} private void init() {
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(getCenterPanel(), BorderLayout.CENTER);
this.setSize(new Dimension(200, 200));
this.setVisible(true);
} private JPanel getCenterPanel() {
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout(FlowLayout.CENTER));
JTextField field1 = new JTextField(5);
JTextField field2 = new JTextField(5);
JComboBox cb = new JComboBox();
cb.addItem("cb1");
cb.addItem("cb2");
cb.addItem("cb3");
panel.add(field1);
panel.add(field2);
panel.add(cb);
return panel;
}
}