我有10个组合框和一个按钮。是否有一种简单的方法可以设置每个组合框的所有值,然后按下此按钮,它会存储组合框的所有值?
答案 0 :(得分:0)
您首先必须遍历所有JComboBox
元素并获取其选定状态(即:getSelectedItem()
或getSelectedIndex()
)。确定选择后,您可以根据需要将选择保存在以后要阅读的许多地方:
您可以将状态保存到以后可以读取的文件中
您可以通过Java Preferences API
如果是网络应用程序,您可以将信息保存在SQL数据库中
答案 1 :(得分:0)
当然!我建议给自己写一个有两件事的新课:
class comboTrack{
private JComboBox box;
private Object value;
public comboTrack(JComboBox box){
this.box = box;
this.value = box.getSelectedItem();
}
public void update(){
this.value = box.getSelectedItem();
}
public Object getValue(){
return value;
}
}
然后,使用这些而不是普通的Boxes。像这样:
//List of boxes (class variable):
ArrayList<comboTrack> boxes;
//Inside the button click method:
for(comboTrack box : boxes){
box.update();
}
答案 2 :(得分:0)
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;
public class ComboBoxValues extends JFrame {
private JPanel contentPane;
JComboBox comboBox;
JComboBox comboBox_1;
JComboBox comboBox_2;
JComboBox comboBox_3;
JComboBox comboBox_4;
JComboBox comboBox_5;
JComboBox comboBox_6;
JComboBox comboBox_7;
JComboBox comboBox_8;
JComboBox comboBox_9;
private JTextArea textArea;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ComboBoxValues frame = new ComboBoxValues();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public ComboBoxValues() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
GridBagLayout gbl_contentPane = new GridBagLayout();
gbl_contentPane.columnWidths = new int[]{0, 0, 0};
gbl_contentPane.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
gbl_contentPane.columnWeights = new double[]{1.0, 0.0, Double.MIN_VALUE};
gbl_contentPane.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, Double.MIN_VALUE};
contentPane.setLayout(gbl_contentPane);
comboBox = new JComboBox();
GridBagConstraints gbc_comboBox = new GridBagConstraints();
gbc_comboBox.insets = new Insets(0, 0, 5, 5);
gbc_comboBox.fill = GridBagConstraints.HORIZONTAL;
gbc_comboBox.gridx = 0;
gbc_comboBox.gridy = 0;
contentPane.add(comboBox, gbc_comboBox);
comboBox.addItem("comboBox_0 val 1");
comboBox.addItem("comboBox_0 val 2");
comboBox_1 = new JComboBox();
GridBagConstraints gbc_comboBox_1 = new GridBagConstraints();
gbc_comboBox_1.insets = new Insets(0, 0, 5, 0);
gbc_comboBox_1.fill = GridBagConstraints.HORIZONTAL;
gbc_comboBox_1.gridx = 1;
gbc_comboBox_1.gridy = 0;
contentPane.add(comboBox_1, gbc_comboBox_1);
comboBox_1.addItem("comboBox_1 val 1");
comboBox_1.addItem("comboBox_1 val 2");
comboBox_2 = new JComboBox();
GridBagConstraints gbc_comboBox_2 = new GridBagConstraints();
gbc_comboBox_2.insets = new Insets(0, 0, 5, 5);
gbc_comboBox_2.fill = GridBagConstraints.HORIZONTAL;
gbc_comboBox_2.gridx = 0;
gbc_comboBox_2.gridy = 1;
contentPane.add(comboBox_2, gbc_comboBox_2);
comboBox_2.addItem("comboBox_2 val 1");
comboBox_2.addItem("comboBox_2 val 2");
comboBox_3 = new JComboBox();
GridBagConstraints gbc_comboBox_3 = new GridBagConstraints();
gbc_comboBox_3.insets = new Insets(0, 0, 5, 0);
gbc_comboBox_3.fill = GridBagConstraints.HORIZONTAL;
gbc_comboBox_3.gridx = 1;
gbc_comboBox_3.gridy = 1;
contentPane.add(comboBox_3, gbc_comboBox_3);
comboBox_3.addItem("comboBox_3 val 1");
comboBox_3.addItem("comboBox_3 val 2");
comboBox_4 = new JComboBox();
GridBagConstraints gbc_comboBox_4 = new GridBagConstraints();
gbc_comboBox_4.insets = new Insets(0, 0, 5, 5);
gbc_comboBox_4.fill = GridBagConstraints.HORIZONTAL;
gbc_comboBox_4.gridx = 0;
gbc_comboBox_4.gridy = 2;
contentPane.add(comboBox_4, gbc_comboBox_4);
comboBox_4.addItem("comboBox_4 val 1");
comboBox_4.addItem("comboBox_4 val 2");
comboBox_5 = new JComboBox();
GridBagConstraints gbc_comboBox_5 = new GridBagConstraints();
gbc_comboBox_5.insets = new Insets(0, 0, 5, 0);
gbc_comboBox_5.fill = GridBagConstraints.HORIZONTAL;
gbc_comboBox_5.gridx = 1;
gbc_comboBox_5.gridy = 2;
contentPane.add(comboBox_5, gbc_comboBox_5);
comboBox_5.addItem("comboBox_5 val 1");
comboBox_5.addItem("comboBox_5 val 2");
comboBox_6 = new JComboBox();
GridBagConstraints gbc_comboBox_6 = new GridBagConstraints();
gbc_comboBox_6.insets = new Insets(0, 0, 5, 5);
gbc_comboBox_6.fill = GridBagConstraints.HORIZONTAL;
gbc_comboBox_6.gridx = 0;
gbc_comboBox_6.gridy = 3;
contentPane.add(comboBox_6, gbc_comboBox_6);
comboBox_6.addItem("comboBox_6 val 1");
comboBox_6.addItem("comboBox_6 val 2");
comboBox_7 = new JComboBox();
GridBagConstraints gbc_comboBox_7 = new GridBagConstraints();
gbc_comboBox_7.insets = new Insets(0, 0, 5, 0);
gbc_comboBox_7.fill = GridBagConstraints.HORIZONTAL;
gbc_comboBox_7.gridx = 1;
gbc_comboBox_7.gridy = 3;
contentPane.add(comboBox_7, gbc_comboBox_7);
comboBox_7.addItem("comboBox_7 val 1");
comboBox_7.addItem("comboBox_7 val 2");
comboBox_8 = new JComboBox();
GridBagConstraints gbc_comboBox_8 = new GridBagConstraints();
gbc_comboBox_8.insets = new Insets(0, 0, 5, 5);
gbc_comboBox_8.fill = GridBagConstraints.HORIZONTAL;
gbc_comboBox_8.gridx = 0;
gbc_comboBox_8.gridy = 4;
contentPane.add(comboBox_8, gbc_comboBox_8);
comboBox_8.addItem("comboBox_8 val 1");
comboBox_8.addItem("comboBox_8 val 2");
comboBox_9 = new JComboBox();
GridBagConstraints gbc_comboBox_9 = new GridBagConstraints();
gbc_comboBox_9.insets = new Insets(0, 0, 5, 0);
gbc_comboBox_9.fill = GridBagConstraints.HORIZONTAL;
gbc_comboBox_9.gridx = 1;
gbc_comboBox_9.gridy = 4;
contentPane.add(comboBox_9, gbc_comboBox_9);
comboBox_9.addItem("comboBox_9 val 1");
comboBox_9.addItem("comboBox_9 val 2");
JButton btnGetValues = new JButton("Get Values");
GridBagConstraints gbc_btnGetValues = new GridBagConstraints();
gbc_btnGetValues.insets = new Insets(0, 0, 5, 0);
gbc_btnGetValues.gridx = 1;
gbc_btnGetValues.gridy = 5;
contentPane.add(btnGetValues, gbc_btnGetValues);
textArea = new JTextArea();
GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
gbc_lblNewLabel.gridheight = 2;
gbc_lblNewLabel.gridwidth = 2;
gbc_lblNewLabel.insets = new Insets(0, 0, 5, 0);
gbc_lblNewLabel.gridx = 0;
gbc_lblNewLabel.gridy = 6;
contentPane.add(textArea, gbc_lblNewLabel);
textArea.setColumns(30);
textArea.setRows(5);
pack();
btnGetValues.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textArea.setText(comboBox.getSelectedItem().toString()+","+
comboBox_1.getSelectedItem().toString()+","+
comboBox_2.getSelectedItem().toString()+"\n"+
comboBox_3.getSelectedItem().toString()+","+
comboBox_4.getSelectedItem().toString()+","+
comboBox_5.getSelectedItem().toString()+"\n"+
comboBox_6.getSelectedItem().toString()+","+
comboBox_7.getSelectedItem().toString()+","+
comboBox_8.getSelectedItem().toString()+"\n"+
comboBox_9.getSelectedItem().toString());
}
});
}
}