java swing 用swing怎样写一个对话框,里面有一个列表,选择列表中的一个,点击确定,返回选定的内容,之后对话框消失。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 给你贴一个简单的实例,你参考一下吧package swing.JComboBox;import javax.swing.*;import java.awt.*;import java.awt.event.*;public class CityDialog extends JDialog{// private String [] cities={// "北京","上海","南京","深圳","济南","沈阳","苏州","杭州","常州"// }; private JFrame parent; private City[] cities=null; private JComboBox comboBox=new JComboBox(); private JButton jBtn=new JButton("确认"); public CityDialog(JFrame parent){ this.parent=parent; //添加几个数据 cities=new City[3]; cities[0]=new City(1,"北京"); cities[1]=new City(2,"上海"); cities[2]=new City(3,"广州"); for(int i=0;i<cities.length;i++){ comboBox.addItem(cities[i]); } comboBox.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub City selectCity=(City)comboBox.getSelectedItem(); System.out.println(selectCity.getId()+selectCity.getName()); } }); comboBox.setEditable(true); Container contentPane=this.getContentPane(); contentPane.setLayout(new FlowLayout()); contentPane.add(comboBox); jBtn.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { City selectCity=(City)comboBox.getSelectedItem(); JOptionPane.showMessageDialog(null,"所选城市为"+selectCity.getName()); dispose(); } }); contentPane.add(jBtn); this.pack(); } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub JFrame frame=new JFrame("Demo"); JButton btn=new JButton("选择城市"); final CityDialog cityDialog=new CityDialog(frame); btn.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { //To change body of implemented methods use File | Settings | File Templates. cityDialog.setVisible(true); } }); frame.getContentPane().add(btn); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); new CityDialog(frame); }}class City{ int id; String name; public City(int id ,String name){ this.id=id; this.name=name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String toString(){ return this.name; } } 这个要一个子窗体实现 如果是子窗体相信你就会写了 至于父子窗体的写法参考一下从别处复制过来的代码import javax.swing.*; import java.awt.*; import javax.swing.event.*; import java.awt.event.*; public class move extends JFrame{ Container con=getContentPane(); JDesktopPane p=new JDesktopPane(); move(){ setTitle("主窗体"); childframe f=new childframe("子窗体",true,true,true); con.add(p); p.add(f); setSize(300,300); setVisible(true); } public static void main(String[] args){ move moveframe=new move(); } } class childframe extends JInternalFrame implements Runnable{ private static int x=0,y=-200; Thread t; childframe(String title, boolean resizable, boolean closable, boolean maximizable){ t=new Thread(this); this.resizable=resizable; this.closable=closable; this.maximizable=maximizable; setSize(200,200); setTitle(title); setBackground(Color.BLUE); show(); t.start(); } public void run(){ try{ while(y<=0){ setLocation(x,y); y+=4; t.sleep(30); } } catch(InterruptedException IE){ System.out.println("线程被中断!!!"); } } } JOptionPane 就可以JOptionPane.showInputDialog(null, "Please choose a name", "Example 1", JOptionPane.QUESTION_MESSAGE, null, new Object[] { "Amanda", "Colin", "Don", "Fred", "Gordon", "Janet", "Jay", "Joe", "Judie", "Kerstin", "Lotus", "Maciek", "Mark", "Mike", "Mulhern", "Oliver", "Peter", "Quaxo", "Rita", "Sandro", "Tim", "Will" }, "Joe"); 也可以选择 http://code.google.com/p/oxbow/提供的 TaskDialog 新手请教一个异常抛出程序 exe4j打包后运行提示ClassNotFoundException Jdeveloper的资料 jfreechart 画图 java socket 简单问题 scjp拖放题,高手来帮帮忙啊~ java如何从字符串中提取相应位置的字符?? 各位大哥,救救小弟,给小弟我一点信心吧!我真的快要失去学习JAVA的信心了! jfreechart 线性图的问题: 猜拳游戏求加一个while大循环,一直执行它! java简单调用 简单数组问题
import java.awt.event.*;public class CityDialog extends JDialog{
// private String [] cities={
// "北京","上海","南京","深圳","济南","沈阳","苏州","杭州","常州"
// };
private JFrame parent;
private City[] cities=null;
private JComboBox comboBox=new JComboBox();
private JButton jBtn=new JButton("确认"); public CityDialog(JFrame parent){
this.parent=parent;
//添加几个数据
cities=new City[3];
cities[0]=new City(1,"北京");
cities[1]=new City(2,"上海");
cities[2]=new City(3,"广州"); for(int i=0;i<cities.length;i++){
comboBox.addItem(cities[i]);
} comboBox.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub City selectCity=(City)comboBox.getSelectedItem();
System.out.println(selectCity.getId()+selectCity.getName());
} }); comboBox.setEditable(true); Container contentPane=this.getContentPane();
contentPane.setLayout(new FlowLayout());
contentPane.add(comboBox); jBtn.addActionListener(new ActionListener(){ @Override
public void actionPerformed(ActionEvent e) {
City selectCity=(City)comboBox.getSelectedItem();
JOptionPane.showMessageDialog(null,"所选城市为"+selectCity.getName());
dispose();
}
});
contentPane.add(jBtn);
this.pack(); } /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame frame=new JFrame("Demo");
JButton btn=new JButton("选择城市");
final CityDialog cityDialog=new CityDialog(frame);
btn.addActionListener(new ActionListener(){ @Override
public void actionPerformed(ActionEvent e) {
//To change body of implemented methods use File | Settings | File Templates.
cityDialog.setVisible(true);
}
});
frame.getContentPane().add(btn);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
new CityDialog(frame);
}
}
class City{
int id;
String name;
public City(int id ,String name){
this.id=id;
this.name=name;
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
}
public String toString(){
return this.name;
}
}
import java.awt.*;
import javax.swing.event.*;
import java.awt.event.*; public class move extends JFrame{
Container con=getContentPane();
JDesktopPane p=new JDesktopPane();
move(){
setTitle("主窗体");
childframe f=new childframe("子窗体",true,true,true);
con.add(p);
p.add(f);
setSize(300,300);
setVisible(true);
}
public static void main(String[] args){
move moveframe=new move();
}
}
class childframe extends JInternalFrame implements Runnable{
private static int x=0,y=-200;
Thread t;
childframe(String title, boolean resizable, boolean closable, boolean maximizable){
t=new Thread(this);
this.resizable=resizable;
this.closable=closable;
this.maximizable=maximizable;
setSize(200,200);
setTitle(title);
setBackground(Color.BLUE);
show();
t.start();
}
public void run(){
try{
while(y<=0){
setLocation(x,y);
y+=4;
t.sleep(30);
}
}
catch(InterruptedException IE){
System.out.println("线程被中断!!!");
}
}
}
JOptionPane.QUESTION_MESSAGE, null, new Object[] { "Amanda",
"Colin", "Don", "Fred", "Gordon", "Janet", "Jay",
"Joe", "Judie", "Kerstin", "Lotus", "Maciek", "Mark",
"Mike", "Mulhern", "Oliver", "Peter", "Quaxo", "Rita",
"Sandro", "Tim", "Will" }, "Joe");