我正在尝试使用几个文本框创建小型 GUI。最短的代码在这里:
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class MortgageCalculator implements ActionListener {
JTextField loanAmount, loanTerm;
JFrame jf;
public static void main(String[] args) {
// TODO Auto-generated method stub
mygui mg = new mygui();
mg.initUIPanel();
}
public void initUIPanel() {
jf = new JFrame();
jf.setTitle("my gui");
jf.setLocation(300, 400);
jf.setSize(400, 500);
jf.setVisible(true);
jf.setResizable(false);
jf.show();
JPanel panel = new JPanel();
panel.setLayout(null);
loanAmount = new JTextField(15);
loanAmount.setBounds(170, 20, 125, 20);
loanTerm = new JTextField(15);
loanTerm.setBounds(170, 60, 125, 20);
panel.add(loanAmount);
panel.add(loanTerm);
jf.add(panel, "Center");
}
}
问题是它只显示框架而不显示 txtboxes。只有在最大化框架一次后,我才能看到这些框。如果再次最小化它,那么我仍然可以看到它们,但一开始看不到。我究竟做错了什么 ?