我是java語言的新手,我正在嘗試創建一個具有JComboBox的GUI,以允許用戶選擇其中一個選項,并根據所選選項通過添加更多文本欄位來更新面板。目前我面臨一個問題,如果我選擇(選擇 1)面板不會更新,它應該出現 5 個帶有標簽的文本欄位。
我的問題是:如何根據用戶從 JComboBox 中選擇的選項更新面板以添加更多文本欄位?
另外,我面臨另一個問題,我找不到設定 JFrame 大小的方法
這是我的代碼:
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);
}
});
}
}
uj5u.com熱心網友回復:
將以下行添加到 RTSS 建構式的末尾:
public RTSS() {
//your code
//this lines
pack();
setLocationRelativeTo(null);
cb.addActionListener(this);
}
這 3 行將 1.設定布局,2.居中框架和 3.激活 ActionListener。如果您現在在 ComboBox 中進行選擇,您的代碼將在 ActionListener 中執行。
在 ComboBox 中選擇更改布局后,再次呼叫 pack()。
@Override
public void actionPerformed(ActionEvent e) {
String Choice = cb.getSelectedItem().toString();
if ("Choice 1".equals(Choice)) {
// your code
pack();
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/438413.html
上一篇:從文本欄位獲取文本不起作用
下一篇:JFrameJava。視窗不顯示
