English 中文(简体)
未与 com贝箱合作
原标题:windowbuilder textfield not working with combobox

我有一个问题,那就是,我有一把案文放在案文领域的行动。 该法典:

public class Main extends JFrame implements ActionListener{
   private JPanel contentPane;
   private JTextField textField;
   private JComboBox comboBox;

   //public static void main - nothing much in it except Main frame = new Main();

   public Main() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 563, 407);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);
    comboBox.addActionListener(frame);
    textField = new JTextField();
    textField.setBounds(42, 99, 445, 235);
    textField.setText("HERE");
    contentPane.add(textField);
    textField.setColumns(10);
    comboBox = new JComboBox();
    comboBox.setModel(new DefaultComboBoxModel(new String[] {"Bob", "Dan ", "Emily"}));
    comboBox.setBounds(42, 48, 140, 29);
    contentPane.add(comboBox);

    /*ONE WAY OF DOING IT: comboBox.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
        textField.setText(studentOutputString((String)comboBox.getSelectedItem()));
        textField.setText("BLAH");
    }
});*/
}
    public String studentOutputString(String student){
        String s = student + "is printed.";
        return s;
}

    public void actionPerformed(ActionEvent e) {
        comboBox = (JComboBox) e.getSource();
        String selectedStudent = (String) comboBox.getSelectedItem();
        textField.setText(studentOutputString(selectedStudent));
}

案文领域没有说明。 任何关于我做什么错的想法?

我对此进行了重新研究,并抓住了我的过去。

问题回答

你们需要就ComboBox采取行动,以便呼吁行动倾听者,并确定案文领域。 选择ComboBox的一个项目

此外,如果除改动选定项目外还有其他活动(例如,在选定项目之前产生活动),<条码>(comboBox.getS selectedItem()>可能退回无效。 在这种情况下,请您在<代码>student + “is Pesticides>/code>内打上studentOutputString (,将抛下一个无名的例外情况。





相关问题
WPF Datagrid, Setting the background of combox popup

I would like to change the color of the popup background when using a DatagridComboboxColumn in the WPF Toolkit datagrid. I ve edited the Template for a normal Combobox and it works great for selected ...

How to insert ComboBox item into ListBox? [winforms]

The question is very simple, How to insert ComboBox selected item into ListBox using c#? I have tried with this: listbox.Items.Add(combobox.SelectedItem); and some other permutations but it always ...

How do I bind a ComboBox to a one column list

I ve seen how to bind a ComboBox to a list that has columns like this: ItemsSource="{Binding Path=Entries}" DisplayMemberPath="Name" SelectedValuePath="Name" SelectedValue="{Binding Path=Entry}" But ...

Wpf Combobox Limit to List

We are using Wpf Combobox to allow the user to do the following things: 1) select items by typing in the first few characters 2) auto complete the entry by filtering the list 3) suggesting the first ...

热门标签