我正在尝试将JPanel p4添加到JFrame中,但它似乎不起作用。
import java.awt.*;
import javax.swing.*;
public class Class4 {
public static final long serialVersionUID = 1L;
public JFrame f = new JFrame("Love Test");
public void mainMethod(int event){
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(500,200);
f.setLayout(new GridBagLayout());
if(event == 0){
GridBagConstraints gbc = new GridBagConstraints();
JPanel p = new JPanel();
p.setLayout(new BoxLayout(p,BoxLayout.Y_AXIS));
p.setBounds(150, 0, 500, 75);
p.setPreferredSize(new Dimension(350,75));
JTextField boy = new JTextField();
boy.setMaximumSize(new Dimension(200,40));
JTextField girl = new JTextField();
girl.setMaximumSize(new Dimension(200,40));
p.add(boy);
p.add(girl);
gbc.insets = new Insets(-90,310,0,0);
gbc.gridx = 0;
gbc.gridy = 0;
f.add(p,gbc);
JPanel p3 = new JPanel(new BorderLayout());
p3.setBounds(0, 0, 150, 75);
p.setPreferredSize(new Dimension(150,75));
Class5 c5o = new Class5();
c5o.setPreferredSize(new Dimension(150,75));
p3.add(c5o);
gbc.insets = new Insets(0,0,90,330);
gbc.gridx = 0;
gbc.gridy = 0;
f.add(p3,gbc);
JPanel p2 = new JPanel(new FlowLayout());
Class7 c7o = new Class7();
p2.add(c7o);
p2.setPreferredSize(new Dimension(300,40));
gbc.insets = new Insets(0,0,-20,0);
gbc.gridx = 0;
gbc.gridy = 0;
f.add(p2,gbc);
f.setVisible(true);
}
if(event == 5){
addLove();
}
}
public void addLove(){
GridBagConstraints gbc = new GridBagConstraints();
JPanel p4 = new JPanel();
p4.setBounds(0,120,500,55);
Class2 c2o = new Class2();
Dimension d2 = new Dimension(500,55);
c2o.setPreferredSize(d2);
p4.setPreferredSize(d2);
p4.add(c2o);
gbc.insets = new Insets(0,0,-130,0);
gbc.gridx = 0;
gbc.gridy = 0;
f.add(p4,gbc);
f.invalidate();
f.validate();
f.repaint();
JOptionPane.showMessageDialog(null, "Test");
}
}
但是,如果我将代码扭曲到下面显示的内容,JPanel p4将被添加到JFrame中。
import java.awt.*;
import javax.swing.*;
public class Class4 {
public static final long serialVersionUID = 1L;
public JFrame f = new JFrame("Love Test");
public void mainMethod(int event){
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(500,200);
f.setLayout(new GridBagLayout());
if(event == 0){
GridBagConstraints gbc = new GridBagConstraints();
JPanel p = new JPanel();
p.setLayout(new BoxLayout(p,BoxLayout.Y_AXIS));
p.setBounds(150, 0, 500, 75);
p.setPreferredSize(new Dimension(350,75));
JTextField boy = new JTextField();
boy.setMaximumSize(new Dimension(200,40));
JTextField girl = new JTextField();
girl.setMaximumSize(new Dimension(200,40));
p.add(boy);
p.add(girl);
gbc.insets = new Insets(-90,310,0,0);
gbc.gridx = 0;
gbc.gridy = 0;
f.add(p,gbc);
JPanel p3 = new JPanel(new BorderLayout());
p3.setBounds(0, 0, 150, 75);
p.setPreferredSize(new Dimension(150,75));
Class5 c5o = new Class5();
c5o.setPreferredSize(new Dimension(150,75));
p3.add(c5o);
gbc.insets = new Insets(0,0,90,330);
gbc.gridx = 0;
gbc.gridy = 0;
f.add(p3,gbc);
JPanel p2 = new JPanel(new FlowLayout());
Class7 c7o = new Class7();
p2.add(c7o);
p2.setPreferredSize(new Dimension(300,40));
gbc.insets = new Insets(0,0,-20,0);
gbc.gridx = 0;
gbc.gridy = 0;
f.add(p2,gbc);
f.setVisible(true);
JPanel p4 = new JPanel();
p4.setBounds(0,120,500,55);
Class2 c2o = new Class2();
Dimension d2 = new Dimension(500,55);
c2o.setPreferredSize(d2);
p4.setPreferredSize(d2);
p4.add(c2o);
gbc.insets = new Insets(0,0,-130,0);
gbc.gridx = 0;
gbc.gridy = 0;
f.add(p4,gbc);
f.invalidate();
f.validate();
f.repaint();
}
if(event == 5){
addLove();
}
}
public void addLove(){
JOptionPane.showMessageDialog(null, "Test");
}
}
但是一旦我运行代码,JPanel就会立即显示出来。我只希望JPanel在事件发生后出现(按下按钮)。所有帮助将不胜感激。感谢。