我的程序有一个国家和州组合框,无论您选择什么,它都只会从州组合框中获得第一个值
My program has a Country and State combobox, and from the state combobox it only gets the first value no matter what you choose
所以我为我的作业创建了一个程序,它创建了一个包含学生数据的顺序文件。我制作了一个国家和州的组合框,根据国家的不同会有相应的州,但是在顺序文件(这是一个 .dat 文件)中,无论我选择什么州,在顺序文件中,州总是第一个 one.For 示例如果选择澳大利亚和塔斯马尼亚州,顺序文件中的州将是新南威尔士州。这是我的代码提前致谢
public assignment1st()
{
super("create student file");
try{
output=new DataOutputStream(new FileOutputStream("studentRec.dat"));
}
catch ( IOException e ) {
System.err.println( "File won't open properly/n" +
e.toString( ) );
System.exit( 1 );
}
initialize();
//*******HERE STARTS THE COUNTRY/STATE COMBOBOX BUILD**************************************
String[] countries = {"-CHOOSE","Australia","Belgium","Brazil","Canada","Georgia","Greece",
"India","Lithuania","Macedonia"};
comboBox_1 = new JComboBox<Object>(countries);
comboBox_1.addActionListener(this);
comboBox_1.setBounds(278, 142, 92, 20);
frame.getContentPane().add(comboBox_1);
// Create sub combo box with multiple models
//State Combobox
comboBox_2 = new JComboBox<String>();
comboBox_2.addItem("-CHOOSE-");
comboBox_2.setBounds(452, 142, 109, 20);
frame.getContentPane().add(comboBox_2);
comboBox_2.setPrototypeDisplayValue("XXXXXXXXXX");
String[] Australia = { "New South Wales", "Tasmania", "Queensland" ,"Victoria"};
states.put(countries[1], Australia);
String[] Belgium = { "Louxembourg", "Hainaut", "Flemish" };
states.put(countries[2], Belgium);
String[] Brazil = { "Amazonas", "Mato Grosso" };
states.put(countries[3], Brazil);
String[] Canada = { "Vancouver", "Quebec" };
states.put(countries[4], Canada);
String[] Georgia = {"Tbilisi", "S.Ossetia" };
states.put(countries[5], Georgia);
String[] Greece = { "Pelloponisos", "Chalchidikis", "Thesprotias" };
states.put(countries[6], Greece);
String[] India = { "Jalpur", "Kolkata", "New Delhi" };
states.put(countries[7], India);
String[] Lithuania = { "Akmene", "Kretinga", "Varena" };
states.put(countries[8],Lithuania);
String[] Macedonia = { "Bitola", "Struga", "Veles" };
states.put(countries[9], Macedonia);
}
导入数据的代码
if ( studentID > 0 ) {
//PLACE FOR COMBOBOXEZ
String sex=(String) comboBox.getSelectedItem();
output.writeUTF(sex);
String country=(String) comboBox_1.getSelectedItem();
output.writeUTF(country);
String state=(String) comboBox_2.getSelectedItem();
output.writeUTF(state);
String month=(String) comboBox_3.getSelectedItem();
output.writeUTF(month);
String day=(String) comboBox_4.getSelectedItem();
output.writeUTF(day);
String year=(String) comboBox_5.getSelectedItem();
output.writeUTF(year);
output.writeInt(maths);
output.writeInt(buisness);
output.writeInt(programming);
output.writeInt(accounting);
output.writeInt(art);
output.writeInt(music);
最后是针对国家/地区组合框执行的操作
public void actionPerformed( ActionEvent e ) {
//*************FOR STATE AND COUNTRY COMBOBOXEZ*********************
String country = (String)comboBox_1.getSelectedItem();
Object o = states.get( country );
if (o == null)
{
comboBox_2.setModel( new DefaultComboBoxModel<String>() );
}
else
{
comboBox_2.setModel( new DefaultComboBoxModel<String>( (String[])o ) );
}
//**********DONE WITH THE STATE AND COUNTRY COMBOBOXEZ**********
这是因为你的这部分代码:
if (o == null)
{
comboBoxstate.setModel( new DefaultComboBoxModel<String>() );
}
else
{
comboBoxstate.setModel( new DefaultComboBoxModel<String>( (String[])o ) );
}
在此之前:
if (e.getSource()==btnEnter){
addRecord( ) ;
}
因此,您的组合框会先重新排列,然后再添加记录。
如果将第二部分放在第一部分之前,效果会很好。就是这样。
但是,我建议您检查是哪个组件导致了该操作,然后执行您想要执行的操作,否则对某些组件的操作可能会导致您希望其他组件执行的操作。如果需要,可以尝试 e.getActionCommand()
所以我为我的作业创建了一个程序,它创建了一个包含学生数据的顺序文件。我制作了一个国家和州的组合框,根据国家的不同会有相应的州,但是在顺序文件(这是一个 .dat 文件)中,无论我选择什么州,在顺序文件中,州总是第一个 one.For 示例如果选择澳大利亚和塔斯马尼亚州,顺序文件中的州将是新南威尔士州。这是我的代码提前致谢
public assignment1st()
{
super("create student file");
try{
output=new DataOutputStream(new FileOutputStream("studentRec.dat"));
}
catch ( IOException e ) {
System.err.println( "File won't open properly/n" +
e.toString( ) );
System.exit( 1 );
}
initialize();
//*******HERE STARTS THE COUNTRY/STATE COMBOBOX BUILD**************************************
String[] countries = {"-CHOOSE","Australia","Belgium","Brazil","Canada","Georgia","Greece",
"India","Lithuania","Macedonia"};
comboBox_1 = new JComboBox<Object>(countries);
comboBox_1.addActionListener(this);
comboBox_1.setBounds(278, 142, 92, 20);
frame.getContentPane().add(comboBox_1);
// Create sub combo box with multiple models
//State Combobox
comboBox_2 = new JComboBox<String>();
comboBox_2.addItem("-CHOOSE-");
comboBox_2.setBounds(452, 142, 109, 20);
frame.getContentPane().add(comboBox_2);
comboBox_2.setPrototypeDisplayValue("XXXXXXXXXX");
String[] Australia = { "New South Wales", "Tasmania", "Queensland" ,"Victoria"};
states.put(countries[1], Australia);
String[] Belgium = { "Louxembourg", "Hainaut", "Flemish" };
states.put(countries[2], Belgium);
String[] Brazil = { "Amazonas", "Mato Grosso" };
states.put(countries[3], Brazil);
String[] Canada = { "Vancouver", "Quebec" };
states.put(countries[4], Canada);
String[] Georgia = {"Tbilisi", "S.Ossetia" };
states.put(countries[5], Georgia);
String[] Greece = { "Pelloponisos", "Chalchidikis", "Thesprotias" };
states.put(countries[6], Greece);
String[] India = { "Jalpur", "Kolkata", "New Delhi" };
states.put(countries[7], India);
String[] Lithuania = { "Akmene", "Kretinga", "Varena" };
states.put(countries[8],Lithuania);
String[] Macedonia = { "Bitola", "Struga", "Veles" };
states.put(countries[9], Macedonia);
}
导入数据的代码
if ( studentID > 0 ) {
//PLACE FOR COMBOBOXEZ
String sex=(String) comboBox.getSelectedItem();
output.writeUTF(sex);
String country=(String) comboBox_1.getSelectedItem();
output.writeUTF(country);
String state=(String) comboBox_2.getSelectedItem();
output.writeUTF(state);
String month=(String) comboBox_3.getSelectedItem();
output.writeUTF(month);
String day=(String) comboBox_4.getSelectedItem();
output.writeUTF(day);
String year=(String) comboBox_5.getSelectedItem();
output.writeUTF(year);
output.writeInt(maths);
output.writeInt(buisness);
output.writeInt(programming);
output.writeInt(accounting);
output.writeInt(art);
output.writeInt(music);
最后是针对国家/地区组合框执行的操作
public void actionPerformed( ActionEvent e ) {
//*************FOR STATE AND COUNTRY COMBOBOXEZ*********************
String country = (String)comboBox_1.getSelectedItem();
Object o = states.get( country );
if (o == null)
{
comboBox_2.setModel( new DefaultComboBoxModel<String>() );
}
else
{
comboBox_2.setModel( new DefaultComboBoxModel<String>( (String[])o ) );
}
//**********DONE WITH THE STATE AND COUNTRY COMBOBOXEZ**********
这是因为你的这部分代码:
if (o == null)
{
comboBoxstate.setModel( new DefaultComboBoxModel<String>() );
}
else
{
comboBoxstate.setModel( new DefaultComboBoxModel<String>( (String[])o ) );
}
在此之前:
if (e.getSource()==btnEnter){
addRecord( ) ;
}
因此,您的组合框会先重新排列,然后再添加记录。
如果将第二部分放在第一部分之前,效果会很好。就是这样。
但是,我建议您检查是哪个组件导致了该操作,然后执行您想要执行的操作,否则对某些组件的操作可能会导致您希望其他组件执行的操作。如果需要,可以尝试 e.getActionCommand()