3 回答

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

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 是要計算的表達式(您從組合框中選擇的選項)。
希望這再次有所幫助!

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();
}
}
添加回答
舉報