猿问

如何使选定的组合框值显示不同的文本?

我是 Java 的新手,找不到我能够理解的问题的任何答案。我想在我的 ComboBox 中选择一个值来更改文本字段中显示的文本。


例如,如果用户在组合框中选择了一位艺术家,那么该艺术家的专辑就会显示在文本字段中。


任何帮助表示赞赏。谢谢!


    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {                                           

 String a = (String)jComboBox1.getSelectedItem();  

int artists = 0;

switch (artists){

    case 0: jTextField1.setText("Take Care, Nothing Was The Same, Views, More Life, Scorpion");

break;

    case 1: jTextField1.setText("Stoney, Beerbongs & Bentleys");

break;

    case 2: jTextField1.setText("One Love, Listen, Nothing But the Beat");

break;

    case 3: jTextField1.setText("Ready for the Weekend, 18 Months, Motion");

break;

    case 4: jTextField1.setText("Cole World: The Sideline Story, 2014 Forest Hills Drive, 4 Your Eyez Only");

break;

    case 5: jTextField1.setText("My Beautiful Dark Twisted Fantasy, Yeezus, The Life of Pablo, ye");

break;

    case 6: jTextField1.setText("Parachutes, a Rush of Blood to the Head, X&Y, Viva La Vida, Mylo Xyloto");

}

    }   


料青山看我应如是
浏览 127回答 3
3回答

慕桂英3389331

这是一个完整的工作示例:import java.awt.GridLayout;import javax.swing.*;public class ChangeTextViaCheckbox extends JFrame {    public ChangeTextViaCheckbox() {        setVisible(true);        setDefaultCloseOperation(EXIT_ON_CLOSE);        setLayout(new GridLayout(3, 1));        JCheckBox cb1 = new JCheckBox("Checkbox 1");        JCheckBox cb2 = new JCheckBox("Checkbox 2");        JTextField tf = new JTextField();        cb1.addActionListener(e -> tf.setText("CB 1 is active"));        cb2.addActionListener(e -> tf.setText("CB 2 is active"));        add(cb1);        add(cb2);        add(tf);    }    public static void main(String[] args) {        ChangeTextViaCheckbox frame = new ChangeTextViaCheckbox();        frame.pack();    }}两者都ActionListener听取执行的动作。如果是这样,他们会在JTextField.但如果你通过JRadioButton和一个ButtonGroup. 有了这个就不能有多项选择了。

Helenr

您可以将 switch() 用于您的组合框。我写了一个代码,它的名称定义为 combobox 作为 cb1。getSelectedItem() 方法用于 cb1。您可以为每种情况定义相应的命令(从索引 0 开始)。String a = (String)cb1.getSelectedItem();&nbsp;&nbsp;int i = 0;switch (i){&nbsp; &nbsp; case 0:&nbsp;break;}确保以break结束每个case;否则你的代码将重复执行。现在,如果您使用的文本字段是 t1,则通用以下代码,switch (i) {case 0: t1.setText(<whatever you want to display>);break;}希望这可以帮助。这是重新审视的代码:String a = (String)cb1.getSelectedItem();int i = 0;switch(i){&nbsp; &nbsp; case 0: t1.setText("Take Care, Nothing Was The Same, Views, More Life, Scorpion");&nbsp; &nbsp; // for combobox option Drake index = 0&nbsp; &nbsp; break;&nbsp; &nbsp; case 1: t1.setText("Stoney, Beerbongs & Bentleys");&nbsp; &nbsp;// for combobox option post_malone index = 1&nbsp; &nbsp; break;&nbsp; &nbsp; case 2: t1.setText("One Love, Listen, Nothing But the Beat");&nbsp; &nbsp; // for combobox option david_guetta&nbsp; &nbsp; break;&nbsp;}switch是一个选择语句,它根据整数或字符常量列表连续测试表达式的值。当找到匹配项时,将执行与该常量关联的语句。这里,变量 i 是要计算的表达式(您从组合框中选择的选项)。希望这再次有所帮助!

白衣染霜花

你的问题缺乏细节和示例,你应该发布你已经编写的代码的重要部分,例如我现在不知道你使用什么[GUI] API(例如 或 ),所以我强烈swing建议AWT你编辑您的问题并提供更多详细信息,但无论哪种方式,我都会给您一个简单的示例。我假设您使用的是swingapi,但如果您使用另一个 GUI api(如 ),应该没有什么不同AWT。import javax.swing.*;&nbsp;&nbsp;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;public class SwingExample extends JFrame{&nbsp;&nbsp;&nbsp; &nbsp;public SwingExample(){&nbsp; &nbsp; &nbsp; String[] artists = {"artist1","artist2","artist3"};&nbsp; &nbsp; &nbsp; Map<String,String> albumOfArtists = new HashMap<String,String>();&nbsp; &nbsp; &nbsp; albumOfArtists.put("artist1","album1");&nbsp; &nbsp; &nbsp; albumOfArtists.put("artist2","album2");&nbsp; &nbsp; &nbsp; albumOfArtists.put("artist3","album3");&nbsp; &nbsp; &nbsp; JComboBox combo1 = new JComboBox<String>(artists);&nbsp; &nbsp; &nbsp; JTextField field1 = new JTextField();&nbsp; &nbsp; &nbsp; //You implement an action listener to define what should be done when&nbsp;&nbsp; &nbsp; &nbsp; //an user performs certain operation. An action event occurs,&nbsp;&nbsp; &nbsp; &nbsp; //whenever an action is performed by the user. Examples: When the user&nbsp;&nbsp; &nbsp; &nbsp; //clicks a button, chooses a menu item, presses Enter in a text field.&nbsp; &nbsp; &nbsp; //add action listener to your combobox:&nbsp; &nbsp; &nbsp; combo1.addActionListener(new ActionListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void actionPerformed(ActionEvent event) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String selectedString=(String)combo1.getSelectedItem();&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; field1.setText(albumOfArtists.get(selectedString));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //for example if you select artist1 then the text displayed in the text field is: album1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; add(combo1);&nbsp; &nbsp; &nbsp; add(field1);&nbsp; &nbsp;}&nbsp; &nbsp;private static void createAndShowGUI() {&nbsp; &nbsp; &nbsp;JFrame frame = new CreateNewJTextField();&nbsp; &nbsp; &nbsp;frame.pack();&nbsp; &nbsp; &nbsp;frame.setVisible(true);&nbsp; &nbsp; &nbsp;frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);&nbsp; &nbsp;}&nbsp; &nbsp;public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; createAndShowGUI();&nbsp; &nbsp;}}
随时随地看视频慕课网APP

相关分类

Java
我要回答