Java 两个Jpanel和按钮

Java 两个Jpanel和按钮,java,Java,我有一个JFrame,其中包含2个jpanel。第一个包含:JTextField和一个JButton“Add”。此JButton应获取JTextField中写入的内容 下一个JPanel包含一个JComboBox,它显示通过按钮提供的JTextField中的数据 所有代码都运行良好 但是,当我通过按钮进行插入时,数据会插入数据库OK,但是JComboBox不包含数据 我当前必须关闭并打开jframe,然后在组合框中找到显示的数据 我不知道当我在JPanel1的JTextField中添加一个新名称

我有一个
JFrame
,其中包含2个
jpanel
。第一个包含:
JTextField
和一个
JButton
“Add”。此
JButton
应获取
JTextField
中写入的内容

下一个
JPanel
包含一个
JComboBox
,它显示通过按钮提供的
JTextField
中的数据

所有代码都运行良好

但是,当我通过按钮进行插入时,数据会插入数据库OK,但是
JComboBox
不包含数据

我当前必须关闭并打开jframe,然后在组合框中找到显示的数据


我不知道当我在
JPanel1

JTextField
中添加一个新名称时,如何在
JComboBox
中立即显示数据

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JComboBox;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class Wut {

    private JFrame frame;
    private JTextField textField;
    private JPanel panel_1;
    private JComboBox comboBox;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Wut window = new Wut();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public Wut() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        comboBox = new JComboBox();
        JPanel panel = new JPanel();
        frame.getContentPane().add(panel, BorderLayout.WEST);

        textField = new JTextField();
        panel.add(textField);
        textField.setColumns(10);

        JButton btnAdd = new JButton("Add");
        btnAdd.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                            if (!textField.getText().toString().equals("")) {
                    comboBox.addItem(textField.getText().toString());
                                comboBOx.setSelectedItem(textField.getText().toString());
                                textField.setText("");
                            }
            }
        });
        panel.add(btnAdd);

        panel_1 = new JPanel();
        frame.getContentPane().add(panel_1, BorderLayout.EAST);

        panel_1.add(comboBox);
    }

}

当您单击“添加”按钮时,它会将文本字段中的任何内容添加到组合框中。

除非看到代码,否则我们无法帮助您。话虽如此,我怀疑您需要重新验证/重新绘制。请发布您的代码,如Grammin所说。你也可以看到,为了语法和清晰,你的文章被编辑了很多次。请验证这些编辑是否准确反映了您的问题,并尝试进一步澄清。是的,但组合框会自动显示新的插入。我必须关闭界面并再次打开,然后我发现itone中的新数据包含jtextfield和buttonadd@cisco.nat是的,我的代码就是这样。如果您看到
panel.add(textField)
panel.add(btnAdd)
——它们将进入一个名为
panel
JPanel
<代码>面板_1包含
组合框
。但你是说你想让组合框将它的值设置为最近的条目吗?看,我在框架中有两个面板