0

我有两个组合框,它们必须具有相同的值。我已将它们设置为具有 2 个不同的 ComboBoxModel,但是当我在第一个组合框中选择某个选项时,第二个组合框中会选择相同的选项。

public class AddLineFrame extends JFrame {
    private ComboBoxModel model1;
    private ComboBoxModel model2;

    import java.awt.BorderLayout;


public class AddLineFrame extends JFrame {

    private JComboBox<String> comboBox1;
    private JComboBox<String> comboBox2;
    private ComboBoxModel model1;
    private ComboBoxModel model2;

    public AddLineFrame() {
        model1 = main.bPanel.getList();
        model2 = main.bPanel.getList();

        comboBox1 = new JComboBox();
        comboBox1.setModel(model1);
        comboBox1.setFont(new Font("Candara", Font.PLAIN, 14));
        comboBox1.setBounds(114, 33, 90, 25);
        panel_1.add(comboBox1);

        comboBox2 = new JComboBox(model2);
        comboBox1.setModel(model2);
        comboBox2.setFont(new Font("Candara", Font.PLAIN, 14));
        comboBox2.setBounds(114, 63, 90, 25);
        panel_1.add(comboBox2);
    }
}

我的模型是这样制作的

公共类 BusPanel 扩展 JPanel 实现 Runnable {

ArrayList <Bus> busList = new ArrayList<Bus> ();
DefaultComboBoxModel<String> model = new DefaultComboBoxModel<String>();


public BusPanel() {
    model.addElement(" ");
    setBorder(BorderFactory.createLineBorder(Color.black, 2));
    setPreferredSize(new Dimension(400,300));
}

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D)g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                        RenderingHints.VALUE_ANTIALIAS_ON);       
    for (Bus bus : busList) {
        bus.paintComponent(g);
    }
}

public void addBus(int id, double voltage, String title) { 
    Bus bus = new Bus(id, voltage, title);
    busList.add(bus);
    model.addElement(bus.title);
    revalidate();
    repaint();

}

public DefaultComboBoxModel<String> getList(){
    return model;
}

}

4

0 回答 0