3 回答

TA貢獻(xiàn)2036條經(jīng)驗(yàn) 獲得超8個(gè)贊
這是一個(gè)完整的工作示例:
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聽取執(zhí)行的動(dòng)作。如果是這樣,他們會(huì)在JTextField.
但如果你通過(guò)JRadioButton和一個(gè)ButtonGroup. 有了這個(gè)就不能有多項(xiàng)選擇了。

TA貢獻(xiàn)1780條經(jīng)驗(yàn) 獲得超4個(gè)贊
您可以將 switch() 用于您的組合框。我寫了一個(gè)代碼,它的名稱定義為 combobox 作為 cb1。getSelectedItem() 方法用于 cb1。您可以為每種情況定義相應(yīng)的命令(從索引 0 開始)。
String a = (String)cb1.getSelectedItem();
int i = 0;
switch (i){
case 0:
break;
}
確保以break結(jié)束每個(gè)case;否則你的代碼將重復(fù)執(zhí)行。現(xiàn)在,如果您使用的文本字段是 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是一個(gè)選擇語(yǔ)句,它根據(jù)整數(shù)或字符常量列表連續(xù)測(cè)試表達(dá)式的值。當(dāng)找到匹配項(xiàng)時(shí),將執(zhí)行與該常量關(guān)聯(lián)的語(yǔ)句。這里,變量 i 是要計(jì)算的表達(dá)式(您從組合框中選擇的選項(xiàng))。
希望這再次有所幫助!

TA貢獻(xiàn)1796條經(jīng)驗(yàn) 獲得超10個(gè)贊
你的問(wèn)題缺乏細(xì)節(jié)和示例,你應(yīng)該發(fā)布你已經(jīng)編寫的代碼的重要部分,例如我現(xiàn)在不知道你使用什么[GUI] API(例如 或 ),所以我強(qiáng)烈swing建議AWT你編輯您的問(wèn)題并提供更多詳細(xì)信息,但無(wú)論哪種方式,我都會(huì)給您一個(gè)簡(jiǎn)單的示例。
我假設(shè)您使用的是swingapi,但如果您使用另一個(gè) GUI api(如 ),應(yīng)該沒(méi)有什么不同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();
}
}
添加回答
舉報(bào)