gpt4 book ai didi

java - 如何扩展 JComboBox 类?

转载 作者:行者123 更新时间:2023-11-30 08:35:10 25 4
gpt4 key购买 nike

我正在编写 Java GUI。我有一些预设的 JComboBoxes 并且能够将它们彼此区分开来,我想扩展类并添加一个 enum 变量来帮助我将它们彼此区分开来其他。

这是两个标准 JComboBoxes 的 MCVE:

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 所做的操作并输入 DefaultComboBoxModel,但我没有这样做尚未在 XComboBox 类中实现。

问题

如何修改 XComboBox 类,以便在构造元素时可以给它一个 DefaultComboBoxModel

我希望能够做到这一点:

ynComboBox = new XComboBox(tfModel);
ynComboBox.setBoxType(BoxType.Device);

感谢您的帮助和/或指导!

最佳答案

您需要将 JComboBox 与 BoxType 关联,关联两个对象的最佳方法之一是使用 Map,这里是 Map<BoxType, JComboBox> .完成此操作后,您可以使用组合框轻松提取具有哪些选择。同样,我会避免扩展 JCombo。例如,我的 MCVE:

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;

import javax.swing.BorderFactory;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class XComboBoxMCVE extends JPanel {
private static final long serialVersionUID = 1L;
private Map<BoxType, JComboBox<String>> comboMap = new EnumMap<>(BoxType.class);

public XComboBoxMCVE() {
setLayout(new BorderLayout());
JPanel comboPanel = new JPanel(new GridLayout(0, 1, 5, 5));

// just for example
String[] modelItemsArray = {"Model Item A", "Model Item B", "Model Item C", "Model Item D"};
BoxItems modelItems = new BoxItems(BoxType.MODEL, modelItemsArray);
String[] CompanyItemsArray = {"Company Item A", "Company Item B", "Company Item C", "Company Item D"};
BoxItems companyItems = new BoxItems(BoxType.COMPANY, CompanyItemsArray);
String[] deviceItemsArray = {"Device Item A", "Device Item B", "Device Item C", "Device Item D"};
BoxItems deviceItems = new BoxItems(BoxType.DEVICE, deviceItemsArray);

createAndPlaceComboBox(BoxType.MODEL, modelItems, comboPanel);
createAndPlaceComboBox(BoxType.COMPANY, companyItems, comboPanel);
createAndPlaceComboBox(BoxType.DEVICE, deviceItems, comboPanel);

JButton printSelection = new JButton("Print Type");
printSelection.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
for (BoxType boxType : BoxType.values()) {
// use the Map to get the appropriate JComboBox
JComboBox<String> combo = comboMap.get(boxType);
String selection = (String) combo.getSelectedItem();
if (selection == null) {
selection = "NONE";
}
String text = String.format("Selection for box type %s: %s: ",
boxType.getText(), selection);
System.out.println(text);
}
}
});

JPanel buttonPane = new JPanel(new GridLayout(0, 1, 5, 5));
buttonPane.add(printSelection);

add(comboPanel, BorderLayout.CENTER);
add(buttonPane, BorderLayout.PAGE_END);
}

private void createAndPlaceComboBox(BoxType boxType, BoxItems boxItems, JPanel panel) {
String[] items = boxItems.getItems().toArray(new String[] {});
DefaultComboBoxModel<String> comboModel = new DefaultComboBoxModel<>(items); // create model
JComboBox<String> combo = new JComboBox<>(comboModel); // crteate combo
comboMap.put(boxType, combo); // put combo into Map

JPanel wrapPanel = new JPanel(new BorderLayout()); // wrapper panel that has a title border
wrapPanel.add(combo);
wrapPanel.setBorder(BorderFactory.createTitledBorder(boxType.getText() + " Items"));
panel.add(wrapPanel);
}

public static void createAndShowGUI() {
JFrame frame = new JFrame("MCVE");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
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();
}
});
}
}

// A better enum, one not inside of an unnecessary class
enum BoxType {
MODEL("Model"), COMPANY("Company"), DEVICE("Device");
private String text;

private BoxType(String text) {
this.text = text;
}

public String getText() {
return text;
}
}

// class to associate BoxType with possible items associated with it
class BoxItems {
private BoxType boxType;
private List<String> items = new ArrayList<>();

public BoxItems(BoxType boxType) {
this.boxType = boxType;
}

public BoxItems(BoxType boxType, String[] itemsArray) {
this(boxType);
for (String item : itemsArray) {
items.add(item);
}
}

public void addItem(String item) {
items.add(item);
}

public BoxType getBoxType() {
return boxType;
}

public List<String> getItems() {
// unmodifiable so that can't be changed outside of this class
return Collections.unmodifiableList(items);
}
}

关于java - 如何扩展 JComboBox 类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38398449/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com