私は現在、選択したアイテムをコンボボックスに別の値に設定しようとしています。この例では靴のサイズを使用していますが、靴のサイズを選択して靴のサイズを選択し、開始ボタンを押してサイズコードを読みたいとします。ComboBoxの選択項目を別の値に設定するにはどうすればよいですか?
これは私が達成しようとしているものです:
これは私が持っているものです。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ComboBoxDemo extends JFrame implements ActionListener{
//declarations
JButton btnPay = new JButton("Start");
/*ShoeSizeArray
String[] comboLabels = {"5", "5.5","6", "6.5", "7", "7.5", "8", "8.5", "9", "9.5", "10", "10.5", "11", "11.5",
"12.5", "13.5", "14", "14.5", "15.5", "16"};*/
//Sizecode Array
String[] comboLabels = {"560", "570", "580", "590", "600", "610", "620", "630", "640", "650", "660", "670", "680", "690", "700", "710",
"720", "730", "740", "750", "760", "770"};
JComboBox <String> combo = new JComboBox<String>(comboLabels);
JTextArea display = new JTextArea(5,20);
//constructor
public ComboBoxDemo(){
super("Combo box");
//panel for button and combobox
JPanel buttonPanel = new JPanel();
//add panel to frame
add(buttonPanel, BorderLayout.SOUTH);
//add button and combobox to panel
buttonPanel.add(btnPay);
buttonPanel.add(combo);
//register button with ActionListener
btnPay.addActionListener(this);
//add text area to center of frame
add(display, BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent e){
String sizecode = (String)combo.getSelectedItem();
display.append("\nYou selected the sizecode " + sizecode);
}
public static void main(String[] args){
JFrame fr = new ComboBoxDemo();
fr.setLocationRelativeTo(null);
fr.setSize(200,200);
fr.setVisible(true);
}
}
あなたはどのような問題に直面していますか? – Beniton
現時点では、選択したインデックス値をComboBoxから別の値に設定する方法がわかりません。これは私が現在抱えていることです。 http://i.imgur.com/nwn3FS0.png – Mxxm
私はまだあなたが達成しようとしているものを得ていません。開始ボタンをクリックすると、選択したコンボボックスの値が表示されます。今あなたは何を変えたいですか? – Beniton