问题:我正在尝试在我的框架中添加第二个 Jpanel,但是当我添加后者时,它会覆盖前一个。目的是在同一个框架中有 2 个组件(Jpanels),但它似乎只接受一个而不是两个。出现顺序应为一列两行:1:输入名称:2:TextField
import javax.swing.*;
import java.awt.*;
public class Money2 extends JFrame {
public Money2() {
// setLayout(new GridLayout(2,2));
JPanel p1 = new JPanel();
p1.setLayout(new FlowLayout(FlowLayout.CENTER));
p1.add(new JLabel("Enter name:"));
// -------------------------------------------------------------------------
// p2.setLayout(new FlowLayout(FlowLayout.CENTER));
JPanel p2 = new JPanel(new FlowLayout());
p2.add(new JTextField(8));
add(p1); // add to Jframe
add(p2);
}
/** Main method */
public static void main(String[] args) {
Money2 frame = new Money2();
frame.setTitle("Money Converter App");
frame.setLocationRelativeTo(null); // Center the frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(350, 400);
frame.setVisible(true);
}
}