有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我有一个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);

我该怎么做


共 (1) 个答案

  1. # 1 楼答案

    下面是一个快速而肮脏的实现

    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();
                }
            });
        }
    
    }