亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使選定的組合框值顯示不同的文本?

如何使選定的組合框值顯示不同的文本?

料青山看我應如是 2023-06-21 15:50:47
我是 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");}    }   
查看完整描述

3 回答

?
慕桂英3389331

TA貢獻2036條經驗 獲得超8個贊

這是一個完整的工作示例:


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. 有了這個就不能有多項選擇了。


查看完整回答
反對 回復 2023-06-21
?
Helenr

TA貢獻1780條經驗 獲得超4個贊

您可以將 switch() 用于您的組合框。我寫了一個代碼,它的名稱定義為 combobox 作為 cb1。getSelectedItem() 方法用于 cb1。您可以為每種情況定義相應的命令(從索引 0 開始)。


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

int i = 0;

switch (i){

    case 0: 

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){

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

    // for combobox option Drake index = 0

    break;

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

   // for combobox option post_malone index = 1

    break;

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

    // for combobox option david_guetta

    break;

 }

switch是一個選擇語句,它根據整數或字符常量列表連續測試表達式的值。當找到匹配項時,將執行與該常量關聯的語句。這里,變量 i 是要計算的表達式(您從組合框中選擇的選項)。


希望這再次有所幫助!


查看完整回答
反對 回復 2023-06-21
?
白衣染霜花

TA貢獻1796條經驗 獲得超10個贊

你的問題缺乏細節和示例,你應該發布你已經編寫的代碼的重要部分,例如我現在不知道你使用什么[GUI] API(例如 或 ),所以我強烈swing建議AWT你編輯您的問題并提供更多詳細信息,但無論哪種方式,我都會給您一個簡單的示例。


我假設您使用的是swingapi,但如果您使用另一個 GUI api(如 ),應該沒有什么不同AWT。


import javax.swing.*;  

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

public class SwingExample extends JFrame{  


   public SwingExample(){

      String[] artists = {"artist1","artist2","artist3"};

      Map<String,String> albumOfArtists = new HashMap<String,String>();

      albumOfArtists.put("artist1","album1");

      albumOfArtists.put("artist2","album2");

      albumOfArtists.put("artist3","album3");


      JComboBox combo1 = new JComboBox<String>(artists);

      JTextField field1 = new JTextField();


      //You implement an action listener to define what should be done when 

      //an user performs certain operation. An action event occurs, 

      //whenever an action is performed by the user. Examples: When the user 

      //clicks a button, chooses a menu item, presses Enter in a text field.


      //add action listener to your combobox:

      combo1.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent event) {

                  String selectedString=(String)combo1.getSelectedItem(); 

                  field1.setText(albumOfArtists.get(selectedString));

                  //for example if you select artist1 then the text displayed in the text field is: album1

            }

      }


      add(combo1);

      add(field1);


   }


   private static void createAndShowGUI() {


     JFrame frame = new CreateNewJTextField();

     frame.pack();

     frame.setVisible(true);

     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


   }


   public static void main(String[] args) {

        createAndShowGUI();

   }

}


查看完整回答
反對 回復 2023-06-21
  • 3 回答
  • 0 關注
  • 188 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號