如何在java中的另一个选项卡中执行按钮操作时更新ComboBox

如何在java中的另一个选项卡中执行按钮操作时更新ComboBox,java,jcombobox,jtabbedpane,Java,Jcombobox,Jtabbedpane,我有一个JTabbedPane,里面有两个面板:panel1和panel2。在panel1中执行操作时,我想更新位于panel2中的JComboBox 在panel1中,我有以下代码: JPanel panel1 = new JPanel(); tabbedPane.addTab("panel1", null, panel1, null); panel1.setLayout(null); //some code JButton btnSubmit = new JButton("Submit"

我有一个JTabbedPane,里面有两个面板:panel1和panel2。在panel1中执行操作时,我想更新位于panel2中的JComboBox

在panel1中,我有以下代码:

JPanel panel1 = new JPanel();
tabbedPane.addTab("panel1", null, panel1, null);
panel1.setLayout(null);

//some code

JButton btnSubmit = new JButton("Submit");
btnSubmit.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {    

        // ?
    }
});
btnSubmit.setBounds(12, 155, 150, 25);
panel1.add(btnSubmit);
面板2中是我的JComboBox:

JPanel panel2 = new JPanel();
tabbedPane.addTab("panel2", null, panel2, null);
panel2.setLayout(null);
//some code
final JComboBox comboBox_2 = new JComboBox();
comboBox_2.setBounds(12, 240, 200, 24);
panel2.add(comboBox_2);

我该怎么做呢?

这里有一个快速而肮脏的实现

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

public class Window extends JFrame {

    JTabbedPane jtp = new JTabbedPane();
    JPanel panel1 = new JPanel();
    JPanel panel2= new JPanel();
    JComboBox<String> comboBox_2 = new JComboBox<String>();

    ActionListener buttonListener = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            comboBox_2.addItem("abc");
            comboBox_2.addItem("def");

        }
    };


    public Window() {

        initP1();
        initP2();
        initJTP();

    }

    public void initJTP(){
        jtp.add(panel1);
        jtp.add(panel2);
        this.add(jtp);
    }

    public void initP1(){
         JButton btnSubmit = new JButton("Submit");
         btnSubmit.setBounds(12, 155, 150, 25);
         btnSubmit.addActionListener(buttonListener);
         panel1.add(btnSubmit);
    }


    public void initP2(){
        comboBox_2.setBounds(12, 240, 200, 24);
        panel2.add(comboBox_2);

    }


       /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    private static void createAndShowGUI() {

        Window frame = new Window();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }

}
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入javax.swing.JButton;
导入javax.swing.JComboBox;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
导入javax.swing.JTabbedPane;
公共类窗口扩展了JFrame{
JTabbedPane jtp=新JTabbedPane();
JPanel panel1=新的JPanel();
JPanel panel2=新的JPanel();
JComboBox组合框_2=新的JComboBox();
ActionListener按钮Listener=新建ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
组合框_2.附加项(“abc”);
组合框_2.附加项(“def”);
}
};
公共窗口(){
initP1();
initP2();
initJTP();
}
公共无效initJTP(){
jtp.add(第1组);
jtp.add(第2组);
本附录(jtp);
}
公共void initP1(){
JButton btnSubmit=新JButton(“提交”);
b提交的挫折(12、155、150、25);
btnSubmit.addActionListener(按钮Listener);
小组1.添加(btnSubmit);
}
public void initP2(){
组合框_2.立根(12,240,200,24);
面板2.添加(组合框2);
}
/**
*创建GUI并显示它。为了线程安全,
*应该从
*事件调度线程。
*/
私有静态void createAndShowGUI(){
窗口框架=新窗口();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//显示窗口。
frame.pack();
frame.setVisible(true);
}
公共静态void main(字符串[]args){
//为事件调度线程计划作业:
//创建并显示此应用程序的GUI。
javax.swing.SwingUtilities.invokeLater(新的Runnable(){
公开募捐{
createAndShowGUI();
}
});
}
}

您需要对选项卡中的组合框进行引用pane@MadProgrammer我第一次提出的问题是错误的。你能再看一下吗?谢谢,这很有效,谢谢。但是现在选项卡的名称不会显示。您需要像以前一样将面板名称添加回jtp.add()调用中。jtp.add(“somename”,第1组);