如何将选定索引设置为来自另一个类的组合框的0

Abo*_*aad 0 java swing selectedindex jbutton jcombobox

当我按下按钮时,我尝试测试是否可以更改组合框选择的索引,但是如果我的组合框从另一个类添加到我的框架中,它对我来说什么都没有用,请你告诉我一下我想 ?

我创建组合框的类是:

package MyPackage;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;

public class AddMyBox {

    private JComboBox combobox;
    String[] array = {"Select", "1", "2", "3"};

    public JComboBox theBox() {
        combobox = new JComboBox();
        combobox.setModel(new DefaultComboBoxModel(array));
        combobox.setBounds(10, 11, 414, 20);
        return combobox;
    }

}
Run Code Online (Sandbox Code Playgroud)

以及创建框架的类和添加组件的位置:

package MyPackage;

import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class MyFrame extends JFrame {

    public MyFrame() {
        getContentPane().setLayout(null);
        setVisible(true);

        // adding the comboBox from class AddMyBox
        AddMyBox getBox = new AddMyBox();
        getContentPane().add(getBox.theBox());

        JButton btnNewButton = new JButton("New button");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                try {
                    // if selected index is 1 make it 0 when the button is pressed
                    if(getBox.theBox().getSelectedIndex() != 0) {
                        getBox.theBox().setSelectedIndex(0);
                    }
                } catch (Exception e) {
                    // TODO: handle exception
                }
            }
        });
        btnNewButton.setBounds(10, 63, 414, 23);
        getContentPane().add(btnNewButton);

        setSize(500,400);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public void MyFrame1() {
        // TODO Auto-generated method stub
    }

}
Run Code Online (Sandbox Code Playgroud)

我的主要课程是:包MyPackage;

public class MyMain {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        MyFrame getFrame = new MyFrame();
        getFrame.MyFrame1();
    }

}
Run Code Online (Sandbox Code Playgroud)

cam*_*ckr 5

public JComboBox theBox() {
    combobox = new JComboBox();
    combobox.setModel(new DefaultComboBoxModel(array));
    combobox.setBounds(10, 11, 414, 20);
    return combobox;
}
Run Code Online (Sandbox Code Playgroud)

每次调用该theBox()方法时都会创建一个新的组合框,因此ActionListener中的逻辑引用了一个在框架上不可见的组合框,因此您的可见组合框将永远不会更改.

你班级的结构是错误的.你需要:

  1. 为类创建一个构造函数,它只是创建组合框.基本上你需要将前3个语句移动到构造函数中.
  2. 更改theBox()方法以简单地返回comboBox变量.(这是删除前3个语句后留下的唯一声明.

编辑:

我从Hovercrafts的答案中复制了其他问题,因为OP无法引用它们:

其他问题:

  • combobox.setBounds(10, 11, 414, 20);不是你想做的事情 - 包括使用魔术数字,使这种方法极不灵活,并建议你使用null布局,这是你真正想要避免的.
  • getContentPane().setLayout(null);是的.不要这样做.虽然null布局和setBounds()Swing新手似乎是创建复杂GUI的最简单和最好的方法,但是你创建的Swing GUI越多,你在使用它们时会遇到更严重的困难.当GUI调整大小时,它们不会调整组件的大小,它们是增强或维护的皇室女巫,当它们放置在滚动窗格中时它们会完全失败,当它们在所有平台上观看时或者与原始平台不同的屏幕分辨率时它们看起来很糟糕.
  • catch (Exception e) { // TODO: handle exception }- 做评论建议 - 处理你的例外,永远不要忽视它们.否则,您将完成相当于闭眼驾驶汽车的编程.