我正在尝试创建一个带有一些标签和 txtAreas 的小型 GUI。我通过使用绝对定位来做到这一点,但我想继续使用 gridbag 布局。我从过去 3 天开始尝试,但无法将这些标签作为所需位置。要么卡在中间,要么卡在边界附近。请帮助他们担任这些职位。
公共无效initUIPanel(){
jf = new JFrame();
jf.setTitle("Mortgage Calculator");
jf.setLocation(100,200);
jf.setSize(400,500);
jf.setVisible (true);
//jf.setResizable(false);
JPanel panel = new JPanel();
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.NORTHWEST;
loanAmount = new JTextField(15);
gbc.gridx = 0;
gbc.gridy = 0;
gbl.setConstraints(loanAmount, gbc);
panel.add(loanAmount);
loanTerm = new JTextField(15);
gbc.gridx = 0;
gbc.gridy = 0;
gbl.setConstraints(loanTerm, gbc);
panel.add(loanTerm);
amount = new JLabel("Loan Amount");
gbc.gridx = 0;
gbc.gridy = 0;
gbl.setConstraints(amount, gbc);
panel.add(amount);
term= new JLabel("Loan Term");
gbc.gridx = 0;
gbc.gridy = 0;
gbl.setConstraints(term, gbc);
panel.add(term);
currency = new JLabel ("AUD");
gbc.gridx = 0;
gbc.gridy = 0;
gbl.setConstraints(currency, gbc);
panel.add(currency);
numOfYear = new JLabel ("Year");
gbc.gridx = 0;
gbc.gridy = 0;
gbl.setConstraints(numOfYear, gbc);
panel.add(numOfYear);
JPanel middlePanel = new JPanel ();
middlePanel.setBorder ( new TitledBorder ( new EtchedBorder (), "Display Area" ) );
txtResult = new JTextArea();
gbc.gridx = 0;
gbc.gridy = 0;
gbl.setConstraints(txtResult, gbc);
panel.add(txtResult);
jf.add(panel,"Center");
//panel.setBounds(200,200,200,20);
jf.setVisible(true);
}
它在 2 行中提供所有内容。都搞砸了。我想要的是
First Line: Loan Amount: ............(txt Area).... "AUD"
Second LIne: Loan Term: .............(Txt Area......Years
然后文本框
谢谢