我希望JTextField使用borderlayout出现在面板的中心但是它没有出现,按钮出现但是JTextField没有...
import java.awt.*;
import javax.swing.*;
public class cal {
public static void main(String[] args) {
JFrame frame= new JFrame();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
frame.setLayout(new FlowLayout());
JPanel panel= new JPanel();
panel.setLayout(new BorderLayout());
JTextField answer= new JTextField("0");
panel.add(answer, BorderLayout.CENTER);
frame.add(panel);
}
}
答案 0 :(得分:0)
添加组件后,必须在帧上调用setVisible(true)。
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class cal {
public static void main(final String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
frame.getContentPane().setLayout(new FlowLayout());
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
JTextField answer = new JTextField("0");
panel.add(answer, BorderLayout.CENTER);
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}