简体   繁体   English

如何使用 JComboBox 更新 Jpanel

[英]How to update Jpanel using JComboBox

i'm new in java language, and i'm trying to create a GUI which it has JComboBox to allow the user to select one of the choices and based on the selected option the panel is updated by adding more text fields.我是 java 语言的新手,我正在尝试创建一个具有 JComboBox 的 GUI,以允许用户选择 select 其中一个选项,并根据所选选项通过添加更多文本字段来更新面板。 currently i'm facing an issue which is the panel doesn't updated if I choosed (choice 1) it should appear 5 text fields with their labels.目前我面临一个问题,如果我选择(选项 1)面板不会更新,它应该显示 5 个带有标签的文本字段。

My question is: how can I update the panel to add more text fields based on the selected option from JComboBox by the user?我的问题是:如何更新面板以根据用户从 JComboBox 中选择的选项添加更多文本字段?

Also, I'm facing another issue which i couldn't find a way to setting the size of JFrame另外,我还面临另一个问题,我找不到设置 JFrame 大小的方法

Here is my code:这是我的代码:

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class RTSS extends JFrame implements ActionListener {

private JComboBox cb;
JPanel main;
JPanel panel3;
JPanel panel2;
JPanel panel1;
GridBagConstraints gbc;
String[] choices;
JLabel Label1;
JLabel Label2;
JLabel Label3;
JLabel Label4;
JLabel Label5;
JTextField tf1;
JTextField tf2;
JTextField tf3;
JTextField tf4;
JTextField tf5;



public RTSS() {
    Border blackline = BorderFactory.createLineBorder(Color.black);
    EmptyBorder b1 = new EmptyBorder(5, 0, 0, 0);
    main = new JPanel(new FlowLayout(FlowLayout.LEFT));

    panel3 = new JPanel(new FlowLayout(FlowLayout.LEFT));
    panel3.setAlignmentX(1);
    panel3.setPreferredSize(new Dimension(800, 35));
    panel3.setBorder(b1);
    main.add(panel3);

    panel2 = new JPanel();
    panel1 = new JPanel();
    panel1.setPreferredSize(new Dimension(700, 500));
    panel1.setBorder(blackline);
    panel1.setLayout(new GridBagLayout());
    gbc = new GridBagConstraints();
    choices = new String[]{ "","Choice 1", "Choice 2", "Choice 3 "};
    cb = new JComboBox(choices);

    //Main inputs (labels)
    Label1 = new JLabel("Label 1 ");
    Label2 = new JLabel("Label 2 ");
    Label3 = new JLabel("Label 3");
    Label4 = new JLabel("Label 4 ");
    Label5 = new JLabel("Label 5 ");

    //TextFields
    tf1 = new JTextField(10);
    tf2 = new JTextField(10);
    tf3 = new JTextField(10);
    tf4 = new JTextField(10);
    tf5 = new JTextField(10);
    gbc.anchor = GridBagConstraints.NORTHWEST;
    gbc.weightx = 0;
    gbc.weighty = 0;
    gbc.insets = new Insets(10, 3, 5, 0);
    panel3.add(cb);
    panel2.add(panel1);
    gbc.gridx = 0;
    gbc.gridy = 0;
    panel1.add(Label1, gbc);
    gbc.gridx = 0;
    gbc.gridy = 1;
    panel1.add(Label2, gbc);
    gbc.gridx = 0;
    gbc.gridy = 2;
    panel1.add(Label3, gbc);
    gbc.gridx = 1;
    gbc.gridy = 0;
    panel1.add(tf1, gbc);
    gbc.gridx = 1;
    gbc.gridy = 1;
    panel1.add(tf2, gbc);
    gbc.weightx = 0.5;
    gbc.weighty = 0.5;
    gbc.gridx = 1;
    gbc.gridy = 2;
    panel1.add(tf3, gbc);
    main.add(panel2);
    add(main);


}
@Override
public void actionPerformed(ActionEvent e) {
    String Choice = cb.getSelectedItem().toString();
    if ("Choice 1".equals(Choice)) {
        gbc.gridx = 0;
        gbc.gridy = 3;
        panel1.add(Label4, gbc);
        gbc.gridx = 1;
        gbc.gridy = 3;
        panel1.add(tf4, gbc);
        gbc.gridx = 0;
        gbc.gridy = 4;
        panel1.add(Label5, gbc);
        gbc.weightx = 6;
        gbc.weighty = 1;
        gbc.gridx = 1;
        gbc.gridy = 4;
        panel1.add(tf5, gbc);
    }
}

public static void main(String[] args) {

    try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            new RTSS().setVisible(true);
        }
    });
}
}

Add the following lines to the end of the RTSS constructor:将以下行添加到 RTSS 构造函数的末尾:

public RTSS() {
    //your code

    //this lines 
    pack();        
    setLocationRelativeTo(null);
    cb.addActionListener(this);
}

These 3 lines will 1.set the layout, 2. center the frame and 3. activate the ActionListener.这 3 行将 1. 设置布局,2. 居中框架和 3. 激活 ActionListener。 If you now make a selection in the ComboBox, your code will be executed in the ActionListener.如果您现在在 ComboBox 中进行选择,您的代码将在 ActionListener 中执行。

After a selection in the ComboBox that changes the layout, call pack() again.在更改布局的 ComboBox 中进行选择后,再次调用 pack()。

@Override
public void actionPerformed(ActionEvent e) {
    String Choice = cb.getSelectedItem().toString();
    if ("Choice 1".equals(Choice)) {
         // your code

        pack();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM