私はJava GUIを書いています。いくつかのプリセットがあり、それらを互いに区別できるようにするために、クラスを拡張し、それらを互いに区別するのに役立つ変数をJComboBoxes
追加したいと考えています。enum
2 つの標準の MCVE は次のJComboBoxes
とおりです。
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class XComboBoxMCVE extends JPanel {
private JComboBox tfComboBox;
private JComboBox ynComboBox;
private JComponent[] components;
public XComboBoxMCVE() {
setLayout(new BorderLayout());
JPanel comboPanel = new JPanel(new GridLayout(0, 1, 5, 5));
Boolean[] trueFalse = { true, false };
DefaultComboBoxModel tfModel = new DefaultComboBoxModel(trueFalse);
tfComboBox = new JComboBox(tfModel);
String[] yesNo = { "Yes", "No" };
DefaultComboBoxModel ynModel = new DefaultComboBoxModel(yesNo);
ynComboBox = new JComboBox(ynModel);
components = new JComponent[] { tfComboBox, ynComboBox };
JButton printSelection = new JButton("Print Type");
printSelection.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
for (JComponent component : components) {
// I also have other components in component in program,
// therefore this usage..
if (component instanceof JComboBox) {
JComboBox temp = (JComboBox) component;
System.out.println("Printing selection: " + temp.getSelectedItem().toString());
// if (temp.getBoxType() == BoxType.Company){
// System.out.println("Companies say: " +
// temp.getSelectedItem().toString());
// } else if(temp.getBoxType() == BoxType.Device){
// System.out.println("Devices are: " +
// temp.getSelectedItem().toString());
// }
}
}
}
});
JPanel buttonPane = new JPanel(new GridLayout(0, 1, 5, 5));
buttonPane.add(printSelection);
comboPanel.add(tfComboBox);
comboPanel.add(ynComboBox);
add(comboPanel, BorderLayout.CENTER);
add(buttonPane, BorderLayout.PAGE_END);
}
public static void createAndShowGUI(){
JFrame frame = new JFrame("MCVE");
frame.setLayout(new BorderLayout());
XComboBoxMCVE pane = new XComboBoxMCVE();
frame.add(pane, BorderLayout.CENTER);
frame.setResizable(false);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
createAndShowGUI();
}
});
}
}
class XComboBox extends JComboBox {
BoxType type;
public XComboBox(BoxType type) {
this.type = type;
}
public void setBoxType(BoxType type) {
this.type = type;
}
public BoxType getBoxType() {
return this.type;
}
public enum BoxType {
Model, Company, Device
}
}
上記の例が示すようJComboBoxes
に、ユーザーがボタンをクリックしたときに 2 つを区別する方法はありません。の使用例としては、BoxType
一方BoxType
が 1 つのタイプのメッセージを出力し、他方BoxType
が別のメッセージを出力するというものがあります。例えば:
if(temp.getBoxType() == BoxType.Device){
System.out.println("The devices are: " + temp.getSelectedItem().toString());
} else if(temp.getBoxType() == BoxType.Company){
System.out.println("The companies say: " + temp.getSelectedItem().toString());
}
しかし、クラスでまだ実装していないJComboBox
と入力 aで行ったのと同様に使用しようとするコンストラクターで問題に遭遇しました。DefaultComboBoxModel
XComboBox
質問
要素を構築XComboBox
するときにクラスを指定できるように、クラスを変更するにはどうすればよいですか?DefaultComboBoxModel
私はこれを行うことができるようにしたい:
ynComboBox = new XComboBox(tfModel);
ynComboBox.setBoxType(BoxType.Device);
助けや指導をありがとう!