0

我正在尝试创建一个带有一些标签和 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

然后文本框

谢谢

4

1 回答 1

0

您还没有设置面板的布局:

panel.setLayout(gbl);

而且,正如评论中已经说过的,所有组件都有相同的gridx和gridy,这显然是不对的。

这是一个完整的示例,如果我理解正确,它可以满足您的要求:

import javax.swing.*;
import java.awt.*;

public class GblTest extends JFrame {

    public GblTest() {
        add(createPanel(), BorderLayout.NORTH);
    }

    private JPanel createPanel() {
        JPanel p = new JPanel();
        p.setLayout(new GridBagLayout());

        GridBagConstraints c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 0;
        c.anchor = GridBagConstraints.BASELINE_LEADING;
        p.add(new JLabel("Loan amount"), c);

        c.gridx++;
        p.add(new JTextField(15), c);

        c.gridx++;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 1.0;
        p.add(new JLabel("AUD"), c);

        c.gridx = 0;
        c.gridy++;
        c.fill = GridBagConstraints.NONE;
        c.weightx = 0.0;
        p.add(new JLabel("Loan term"), c);

        c.gridx++;
        p.add(new JTextField(15), c);

        c.gridx++;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 1.0;
        p.add(new JLabel("Years"), c);

        return p;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                GblTest test = new GblTest();
                test.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                test.pack();
                test.setVisible(true);
            }
        });
    }
}
于 2013-06-01T16:12:43.760 回答