java中的下拉列表
请帮我看一下,下面这个程序,我想实现,在第一个列表中选择吉林时,在第二个列表中显示相应的市!!!选择辽宁时,也显示相应的市!!下面是这个程序!importjava.awt...
请帮我看一下,下面这个程序,我想实现,在第一个列表中选择吉林时,在第二个列表中显示相应的市!!!选择辽宁时,也显示相应的市!!
下面是这个程序!
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Demo10 extends JFrame implements ActionListener {
String s [] = {"吉林","辽宁","黑龙江"};
String ss [][] ={{"吉林市","长春市","四平市"},{"沈阳市","大连市","锦州市"},{"佳木斯","哈尔滨","齐齐哈尔"}};
JComboBox jb1 = new JComboBox(s);
JComboBox jb2 = new JComboBox(ss);
JPanel panel = new JPanel();
public Demo10() {
panel.add(jb1);
panel.add(jb2);
this.getContentPane().add(panel);
this.setBounds(100,100,300,300);
this.setDefaultCloseOperation(3);
this.setVisible(true);
}
public static void main(String[] args) {
new Demo10();
}
public void actionPerformed(ActionEvent e) {
}
} 展开
下面是这个程序!
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Demo10 extends JFrame implements ActionListener {
String s [] = {"吉林","辽宁","黑龙江"};
String ss [][] ={{"吉林市","长春市","四平市"},{"沈阳市","大连市","锦州市"},{"佳木斯","哈尔滨","齐齐哈尔"}};
JComboBox jb1 = new JComboBox(s);
JComboBox jb2 = new JComboBox(ss);
JPanel panel = new JPanel();
public Demo10() {
panel.add(jb1);
panel.add(jb2);
this.getContentPane().add(panel);
this.setBounds(100,100,300,300);
this.setDefaultCloseOperation(3);
this.setVisible(true);
}
public static void main(String[] args) {
new Demo10();
}
public void actionPerformed(ActionEvent e) {
}
} 展开
4个回答
展开全部
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Demo10 extends JFrame implements ActionListener {
String s[] = { "吉林", "辽宁", "黑龙江" };
String ss[][] = { { "吉林市", "长春市", "四平市" },
{ "沈阳市", "大连市", "锦州市" },
{ "佳木斯", "哈尔滨", "齐齐哈尔" } };
JComboBox jb1 = new JComboBox(s);
JComboBox jb2 = new JComboBox();
JPanel panel = new JPanel();
public Demo10() {
panel.add(jb1);
panel.add(jb2);
jb1.addActionListener(this);
this.getContentPane().add(panel);
this.setBounds(100, 100, 300, 300);
this.setDefaultCloseOperation(3);
this.setVisible(true);
}
public static void main(String[] args) {
new Demo10();
}
public void actionPerformed(ActionEvent e) {
JComboBox temp = (JComboBox) e.getSource();
String name = (String) temp.getSelectedItem();
if(jb2.getItemCount()!=0){
jb2.removeAllItems();
jb2.updateUI();
jb2.setSelectedItem("");
}
if(name.equals(s[0])){
for(int i=0;i<3;i++){
jb2.addItem(ss[0][i]);
}
}
else if(name.equals(s[1])){
for(int i=0;i<3;i++){
jb2.addItem(ss[1][i]);
}
}
else if(name.equals(s[2])){
for(int i=0;i<3;i++){
jb2.addItem(ss[2][i]);
}
}
}
}
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Demo10 extends JFrame implements ActionListener {
String s[] = { "吉林", "辽宁", "黑龙江" };
String ss[][] = { { "吉林市", "长春市", "四平市" },
{ "沈阳市", "大连市", "锦州市" },
{ "佳木斯", "哈尔滨", "齐齐哈尔" } };
JComboBox jb1 = new JComboBox(s);
JComboBox jb2 = new JComboBox();
JPanel panel = new JPanel();
public Demo10() {
panel.add(jb1);
panel.add(jb2);
jb1.addActionListener(this);
this.getContentPane().add(panel);
this.setBounds(100, 100, 300, 300);
this.setDefaultCloseOperation(3);
this.setVisible(true);
}
public static void main(String[] args) {
new Demo10();
}
public void actionPerformed(ActionEvent e) {
JComboBox temp = (JComboBox) e.getSource();
String name = (String) temp.getSelectedItem();
if(jb2.getItemCount()!=0){
jb2.removeAllItems();
jb2.updateUI();
jb2.setSelectedItem("");
}
if(name.equals(s[0])){
for(int i=0;i<3;i++){
jb2.addItem(ss[0][i]);
}
}
else if(name.equals(s[1])){
for(int i=0;i<3;i++){
jb2.addItem(ss[1][i]);
}
}
else if(name.equals(s[2])){
for(int i=0;i<3;i++){
jb2.addItem(ss[2][i]);
}
}
}
}
展开全部
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class ItemEventTest {
private JFrame jf;
private JLabel label1,label2;
private JComboBox jcb1,jcb2;
private String[] pArr;
private String[][] cArr;
public ItemEventTest(String[] pArr,String[][] cArr){
this.pArr=pArr;
this.cArr=cArr;
jf=new JFrame("Item Event Test");
label1=new JLabel("省份:");
label2=new JLabel("城市:");
jcb1=new JComboBox(pArr);
jcb2=new JComboBox(cArr[0]);
jf.setLayout(new FlowLayout());
jf.add(label1);
jf.add(jcb1);
jf.add(label2);
jf.add(jcb2);
addEventHandler();
}
private void addEventHandler(){
jcb1.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent arg0) {
int index=jcb1.getSelectedIndex();
jcb2.removeAllItems();
for(int i=0;i<cArr[index].length;i++){
jcb2.addItem(cArr[index][i]);
}
}
});
}
public void showMe(){
jf.setSize(300,200);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
/**
* @param args
*/
public static void main(String[] args) {
String s [] = {"吉林","辽宁","黑龙江"};
String ss [][] ={{"吉林市","长春市","四平市"},{"沈阳市","大连市","锦州市"},{"佳木斯","哈尔滨","齐齐哈尔"}};
new ItemEventTest(s,ss).showMe();
}
}
import javax.swing.*;
import java.awt.event.*;
public class ItemEventTest {
private JFrame jf;
private JLabel label1,label2;
private JComboBox jcb1,jcb2;
private String[] pArr;
private String[][] cArr;
public ItemEventTest(String[] pArr,String[][] cArr){
this.pArr=pArr;
this.cArr=cArr;
jf=new JFrame("Item Event Test");
label1=new JLabel("省份:");
label2=new JLabel("城市:");
jcb1=new JComboBox(pArr);
jcb2=new JComboBox(cArr[0]);
jf.setLayout(new FlowLayout());
jf.add(label1);
jf.add(jcb1);
jf.add(label2);
jf.add(jcb2);
addEventHandler();
}
private void addEventHandler(){
jcb1.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent arg0) {
int index=jcb1.getSelectedIndex();
jcb2.removeAllItems();
for(int i=0;i<cArr[index].length;i++){
jcb2.addItem(cArr[index][i]);
}
}
});
}
public void showMe(){
jf.setSize(300,200);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
/**
* @param args
*/
public static void main(String[] args) {
String s [] = {"吉林","辽宁","黑龙江"};
String ss [][] ={{"吉林市","长春市","四平市"},{"沈阳市","大连市","锦州市"},{"佳木斯","哈尔滨","齐齐哈尔"}};
new ItemEventTest(s,ss).showMe();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
重写toString方法啊,
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
程序呢?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询