我正在写一个Java GUI。我有一些预设JComboBoxes
并且能够将它们彼此区分开来,我想扩展类并添加一个enum
变量来帮助我将它们区分开来。
这是两个标准的 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
正如上面的示例所示,当用户单击按钮时,我无法将两者区分开来。使用的一个例子BoxType
是,一个BoxType
会打印一种类型的消息,而另一个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
and 输入 a所做的操作DefaultComboBoxModel
,但我还没有在XComboBox
课堂上实现它。
问题
如何修改XComboBox
类以便DefaultComboBoxModel
在构造元素时给它一个?
我希望能够做到这一点:
ynComboBox = new XComboBox(tfModel);
ynComboBox.setBoxType(BoxType.Device);
感谢您的任何帮助和/或指导!