2011-03-25 85 views
1

如何changue這個代碼組合框按鈕,文本框的Java

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 

public class ComboBox { 
    private static final ActionListener Event = null; 
    JComboBox combo1; 
    JComboBox combo2; 
    JComboBox combo3; 
    JTextField txt; 
    Button boton; 

    public static void main(String[] args) { 
     ComboBox b = new ComboBox(); 
    } 

    public ComboBox() { 
     String course1[] = { "India", "Germany", "America", "Russia" }; 
     String course2[] = { "India", "Germany", "America", "Russia" }; 
     String course3[] = { "India", "Germany", "America", "Russia" }; 
     JFrame frame = new JFrame("Creating a JComboBox Component"); 
     JPanel panel = new JPanel(); 
     combo1 = new JComboBox(course1); 
     combo2 = new JComboBox(course2); 
     combo3 = new JComboBox(course3); 
     txt = new JTextField(30); 
     boton = new Button("Boton"); 
     panel.add(combo1); 
     panel.add(combo2); 
     panel.add(combo3); 
     panel.add(txt); 
     panel.add(boton); 
     frame.add(panel); 

     boton.addActionListener(Event); 
     combo1.addItemListener(new ItemListener() { 
      public void itemStateChanged(ItemEvent ie) { 
       String str1 = (String) combo1.getSelectedItem(); 
       String str2 = (String) combo2.getSelectedItem(); 
       String str3 = (String) combo3.getSelectedItem(); 
       txt.setText(str1+str2+str3); 
      } 
     }); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setSize(400, 400); 
     frame.setVisible(true); 
    } 
} 

因此,在一個文本框顯示每個組合的值的串聯的作用是通過按鈕來完成?

+1

你的問題不明確 – developer 2011-03-25 06:39:34

+0

嘛,所以當按鈕被按下時打印每個列表 – cMinor 2011-03-25 06:41:05

+0

更好的字符串中的所選項目的動作偵聽器的代碼,發佈完整的代碼,已經嘗試或嘗試解釋簡單而直接的事情.. – 2011-03-25 06:46:11

回答

1

我從你的問題得到的是,當你點擊按鈕,你需要顯示在JTextField中的三個組合框中選擇的值。

import java.util.ArrayList; 
import java.util.HashSet; 
import java.util.List; 
import java.util.Set; 
import java.util.SortedSet; 
import java.util.TreeSet; 

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 

public class ComboBox { 
    private static final ActionListener Event = null; 
    JComboBox combo1; 
    JComboBox combo2; 
    JComboBox combo3; 
    JTextField txt; 
    Button boton; 

    public static void main(String[] args) { 
     ComboBox b = new ComboBox(); 
    } 

    public ComboBox() { 
     String course1[] = { "India", "Germany", "America", "Russia" }; 
     String course2[] = { "India", "Germany", "America", "Russia" }; 
     String course3[] = { "India", "Germany", "America", "Russia" }; 
     JFrame frame = new JFrame("Creating a JComboBox Component"); 
     JPanel panel = new JPanel(); 
     combo1 = new JComboBox(course1); 
     combo2 = new JComboBox(course2); 
     combo3 = new JComboBox(course3); 
     txt = new JTextField(30); 
     boton = new Button("Boton"); 
     panel.add(combo1); 
     panel.add(combo2); 
     panel.add(combo3); 
     panel.add(txt); 
     panel.add(boton); 
     frame.add(panel); 

     boton.addActionListener(new ActionListener() { 

      public void actionPerformed(ActionEvent e) { 
        String str1 = (String) combo1.getSelectedItem(); 
        String str2 = (String) combo2.getSelectedItem(); 
        String str3 = (String) combo3.getSelectedItem(); 
        txt.setText(str1+str2+str3); 

      } 
     }); 
     combo1.addItemListener(new ItemListener() { 
      public void itemStateChanged(ItemEvent ie) { 

      } 
     }); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setSize(400, 400); 
     frame.setVisible(true); 
    } 
} 

這裏是sceenshot ​​