gpt4 book ai didi

Java GUI 对齐

转载 作者:行者123 更新时间:2023-12-02 09:06:18 32 4
gpt4 key购买 nike

所以我已经添加了标签和所有内容,但我仍然在格式化和对齐所有内容时遇到问题。计算按钮显然应该居中。我知道使用 gridbag 将框架分割成坐标系,当一列大于其他列时,它会调整其他列并将其抛弃(对吗?)。但我什至不明白如何解决这个问题。并且仍然无法将所有内容向左对齐,这样它就不会向中心开始。

编译后的窗口如下

Have

这就是我想要的,我还没有添加所有内容。

Want

 JPanel p = new JPanel(new GridBagLayout());


GridBagConstraints gc = new GridBagConstraints();

//0 Column
gc.gridx = 0;
gc.gridy = 0;
gc.anchor = GridBagConstraints.EAST;
p.add(new JLabel("Mortgage amount:"),gc);


gc.gridx = 0;
gc.gridy =1;
p.add(new JLabel("Mortgage term:"),gc);



gc.gridx = 0;
gc.gridy = 2;
p.add(new JLabel("Interest rate:"),gc);

gc.gridx = 0;
gc.gridy = 3;
p.add(new JLabel("Mortgage start date:"),gc);

gc.gridx = 0;
gc.gridy= 4;
p.add(new JLabel("Monthly Payments:"),gc);

gc.gridx = 0;
gc.gridy = 7;
p.add(new JLabel("Extra payments"),gc);

gc.gridx = 0;
gc.gridy = 8;
p.add(new JLabel("Adding: "),gc);

gc.gridx = 0;
gc.gridy = 9;
p.add(new JLabel("Adding: "),gc);

gc.gridx = 0;
gc.gridy = 10;
p.add(new JLabel("Adding: "),gc);

gc.gridx = 0;
gc.gridy = 11;
p.add(new JLabel("Changes paid off date to:"),gc);


//1 Column
gc.gridx = 1;
gc.gridy = 0;
p.add(new JLabel("$"),gc);

gc.gridx = 1;
gc.gridy = 4;
p.add(new JLabel("$"),gc);

gc.gridx = 1;
gc.gridy = 8;
p.add(new JLabel("$"),gc);

gc.gridx = 1;
gc.gridy = 9;
p.add(new JLabel("$"),gc);

gc.gridx = 1;
gc.gridy = 10;
p.add(new JLabel("$"),gc);

//2 column
gc.gridx = 2;
gc.gridy = 0;
p.add(new JTextField(8),gc);

gc.gridx = 2;
gc.gridy =1;
p.add(new JTextField(8),gc);

gc.gridx = 2;
gc.gridy = 2;
p.add(new JTextField(8),gc);

gc.gridx = 2;
gc.gridy = 3;
p.add(new JComboBox(month),gc);

gc.gridx= 2;
gc.gridy = 4;
p.add(new JTextField(8),gc);

gc.gridx = 2;
gc.gridy = 8;
p.add(new JTextField(8),gc);

gc.gridx = 2;
gc.gridy = 9;
p.add(new JTextField(8),gc);

gc.gridx = 2;
gc.gridy = 10;
p.add(new JTextField(8),gc);

//3 column
gc.gridx = 3;
gc.gridy = 1;
p.add(new JLabel(" years or "),gc);

gc.gridx = 3;
gc.gridy = 2;
p.add(new JLabel(" % per year"),gc);

gc.gridx = 3;
gc.gridy = 3;
p.add(new JComboBox(days),gc);

gc.gridx = 3;
gc.gridy = 8;
p.add(new JLabel("to your monthly mortgage payment"),gc);

gc.gridx = 3;
gc.gridy = 9;
p.add(new JLabel("as an extra yearly mortgage payment every "),gc);

gc.gridx = 3;
gc.gridy = 10;
p.add(new JLabel("as a one-time payment in "),gc);


//4 column
gc.gridx = 4;
gc.gridy = 1;
p.add(new JTextField(8),gc);

gc.gridx = 4;
gc.gridy=3;
p.add(new JComboBox(years),gc);



//5 column
gc.gridx = 5;
gc.gridy = 1;
p.add(new JLabel(" months"),gc);

gc.gridy=5;
gc.anchor = GridBagConstraints.CENTER;
p.add(new JButton("Calculate"),gc);


add(p, BorderLayout.NORTH);





}



}

最佳答案

由于您没有指定调整大小行为,因此可以使用许多布局管理器来获得非常相似的结果。正如 @MadProgrammer 所提到的,2 个面板可能是一个很好的方法,对于顶部的面板我使用 GridBagLayout出于教育目的,我将底部留空供您自己实现,尽管该项目不会从 GridBagLayout 中受益。并且可以使用更简单的布局管理器(例如 BoxLayout )。

public class Mort extends JFrame {

public static void main(String[] args) {

new Mort();
}

Mort() {

JPanel topPanel = new JPanel(new GridBagLayout());
GridBagConstraints gc = new GridBagConstraints();
JPanel tempPanel;

gc.anchor = GridBagConstraints.LINE_START;

// Top
// 0 Column
gc.gridx = 0;

gc.gridy = 0;
topPanel.add(new JLabel("Mortgage amount:"), gc);

gc.gridy = 1;
topPanel.add(new JLabel("Mortgage term:"), gc);

gc.gridy = 2;
topPanel.add(new JLabel("Interest rate:"), gc);

gc.gridy = 3;
topPanel.add(new JLabel("Mortgage start date:"), gc);

gc.gridy = 4;
topPanel.add(new JLabel("Monthly Payments:"), gc);

gc.gridy = 5;
gc.gridwidth = GridBagConstraints.REMAINDER;
gc.anchor = GridBagConstraints.CENTER;
topPanel.add(new JButton("Calculate"), gc);

gc.gridwidth = 1;
gc.anchor = GridBagConstraints.LINE_START;

// 1 Column
gc.gridx = 1;

gc.gridy = 0;
tempPanel = new JPanel();
tempPanel.add(new JLabel("$"));
tempPanel.add(new JTextField(8));
topPanel.add(tempPanel, gc);

gc.gridy = 1;
tempPanel = new JPanel();
tempPanel.add(new JTextField(8));
tempPanel.add(new JLabel("years of"));
tempPanel.add(new JTextField(8));
tempPanel.add(new JLabel("months"));
topPanel.add(tempPanel, gc);

gc.gridy = 2;
tempPanel = new JPanel();
tempPanel.add(new JTextField(8));
tempPanel.add(new JLabel("% per year"));
topPanel.add(tempPanel, gc);

gc.gridy = 3;
tempPanel = new JPanel();
tempPanel.add(new JComboBox());
tempPanel.add(new JComboBox());
tempPanel.add(new JComboBox());
topPanel.add(tempPanel, gc);

gc.gridy = 4;
tempPanel = new JPanel();
tempPanel.add(new JLabel("$"));
tempPanel.add(new JTextField(8));
topPanel.add(tempPanel, gc);

// Bottom
JPanel bottomPanel = new JPanel();
bottomPanel.add(new JLabel("BottomPanel"));

// Main
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
mainPanel.add(new JLabel("Mortgage Calculator"));
mainPanel.add(new JSeparator());
mainPanel.add(topPanel);
mainPanel.add(new JSeparator());
mainPanel.add(bottomPanel);

getContentPane().add(mainPanel);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setVisible(true);
}
}

enter image description here

回答您的具体疑虑:

  • 您错过了线路 gc.anchor = GridBagConstraints.LINE_START;它将所有内容按照您想要的方式向左对齐。

  • 超过 2 列就没有意义,因为只有 2 处需要垂直对齐。

  • “计算”按钮位于列对齐方式之外(它在两列中居中)。专线gc.gridwidth = GridBagConstraints.REMAINDER;意味着它应该跨越(填充)右侧的其余列和 gc.anchor = GridBagConstraints.CENTER; 行将其与中心对齐。

我将所有视觉效果(颜色、字体、填充、插图等)留给您。

关于Java GUI 对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29759943/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com