gpt4 book ai didi

java - 如何创建具有不同 react 的 JcomboBox?

转载 作者:行者123 更新时间:2023-12-01 15:58:23 25 4
gpt4 key购买 nike

我的问题是 SyntaxPanel 中的每个 JComboBox 都是相同!我以为我可以迭代组合框的名称,但这不会改变任何东西。也许我应该将它添加到 Jlist/List 但该列表不适用于 JComboBox 类型..

为了进一步使用,我想迭代 SyntaxPanel 中的任何 JComboBox:

  • 这样我就可以从中获取值(value)
  • 使用 JComboBox cbSyntax 的值设置默认值
  • 非常感谢您的帮助=)

    包读取器;

    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.GridBagLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.LinkedList;
    import java.util.List;

    import javax.swing.DefaultComboBoxModel;
    import javax.swing.JButton;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;

    @SuppressWarnings("serial")
    public class Rename extends JFrame {
    private Integer i=0;

    private List<JComboBox> llSyntax = new LinkedList<JComboBox>();

    private JPanel jpOption;
    private JComboBox cbOption;
    private JLabel lOption;

    private JPanel jpControl;
    private JButton jbRemove;
    private JButton jbAdd;
    private JButton jbUmbenennen;

    private JPanel jpSyntax;
    private JComboBox cbSyntax;
    private DefaultComboBoxModel cbmSyntax = new DefaultComboBoxModel(new String[] {"artist","year","album"});
    //private List<JComboBox> lJComboBox = new List<JComboBox>();

    private DefaultComboBoxModel cbmOption = new DefaultComboBoxModel(new String[] { "artist", "album", "track" });

    Rename() {
    this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    this.setSize(500, 200);
    this.setLocation(500, 500);
    this.setPreferredSize(new Dimension(500, 500));
    this.setLayout(new BorderLayout());

    OptionPanel();
    SyntaxPanel();
    ControlPanel();

    this.setVisible(true);
    }

    public void OptionPanel() {
    jpOption = new JPanel();
    this.add(jpOption, BorderLayout.PAGE_START);

    lOption = new JLabel();
    lOption.setText("Welcher Background soll aktiv sein?");
    jpOption.add(lOption);

    cbOption = new JComboBox(cbmOption);
    cbOption.addActionListener(new alOption());
    jpOption.add(cbOption);
    }

    public void SyntaxPanel() {
    //JPanel für Syntax Buttons
    jpSyntax = new JPanel(new GridBagLayout());
    this.add(jpSyntax);

    for (i=0; i < 2; i++) {
    cbSyntax = new JComboBox(cbmSyntax);
    cbSyntax.setName("combobox"+i.toString());
    jpSyntax.add(cbSyntax);

    //Wie befüllt man comboBox mit arraylist
    // for (int x = 0; x < alSyntax.size(); x++) {
    // cbSyntax.addItem(alSyntax.get(x));
    // {llSyntax.add(cbSyntax);}
    // }
    }

    //System.out.println(cbSyntax.getSelectedItem());

    }

    public void ControlPanel() {
    jpControl = new JPanel();
    this.add(jpControl, BorderLayout.PAGE_END);

    jbAdd = new JButton();
    jbAdd.setText("Add");
    jbAdd.addActionListener(new alAdd());
    jpControl.add(jbAdd);

    jbRemove = new JButton();
    jbRemove.setText("Remove");
    jbRemove.addActionListener(new alRemove());
    jpControl.add(jbRemove);

    jbUmbenennen= new JButton();
    jbUmbenennen.setText("Umbenennen");
    jbUmbenennen.addActionListener(new alUmbenennen());
    jpControl.add(jbUmbenennen);

    }

    private class alOption implements ActionListener {
    @Override //setzt die Umgebung
    public void actionPerformed(ActionEvent e) {

    int i = cbOption.getSelectedIndex();
    //Set the Background for the ComboBoxes of the Syntax Panel
    }
    }

    private class alAdd implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
    cbSyntax = new JComboBox(cbmSyntax);
    jpSyntax.add(cbSyntax);

    jpSyntax.revalidate();
    jpSyntax.repaint();

    // add(buttonList.get(1)); // am schluss einfügen.
    }
    }

    private class alRemove implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {

    System.out.println("remove");
    }
    }

    private class alUmbenennen implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
    // for (JComboBox cb : llSyntax)
    // ArrayList<String> test.add(JComboBox.getSelectedItem());
    //

    // add(buttonList.get(1)); // am schluss einfügen.
    }
    }
    public static void main(String[] args) {
    @SuppressWarnings("unused")
    Rename ren = new Rename();

    }

    }

    最佳答案

    在你的循环中,

       for (i=0; i < 2; i++) {
    cbSyntax = new JComboBox(cbmSyntax);
    cbSyntax.setName("combobox"+i.toString());
    jpSyntax.add(cbSyntax);

    }

    声明一个局部变量并将其添加到您的 jpSyntax 中,如下所示:

       for (i=0; i < 2; i++) {
    JComboBox box = new JComboBox(cbmSyntax);
    box.setName("combobox"+i.toString());
    jpSyntax.add(box);
    }

    此外,

    cbSyntax = new JComboBox(cbmSyntax);
    jpSyntax.add(cbSyntax);

    actionlistener 中需要相应地更改为新的局部变量。

    关于java - 如何创建具有不同 react 的 JcomboBox?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4556680/

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