gpt4 book ai didi

java - 如何修复 JFrame 程序的布局

转载 作者:行者123 更新时间:2023-11-30 03:37:23 25 4
gpt4 key购买 nike

我只是想知道是否有人知道如何修复我的代码,以便所有代码都出现在一列中,而不是它一直给我的两列,我希望它只显示一列,并且有一个漂亮而紧凑的GUI,但每次我编译和运行时,该按钮都会从其余文本框和标签中伸出!任何额外的帮助也非常感谢。谢谢!

代码:

package time;

import java.awt.BorderLayout;


public class Display extends JPanel{

private static final long serialVersionUID = 1L;

Basic out = new Basic();

public static int hrsIn;
public static int daysIn;
public static int monthsIn;
public static int yearsIn;
public static int timeAdd;
public static JButton enter;
public static JLabel currentTime, nextTime, hoursUntil;
public static JTextField dayCurrent, hourCurrent, monthCurrent, yearCurrent,
dayNext, hourNext, monthNext, yearNext;
public static JFrame frame;
public static JPanel newTimePanel;

public Display(){
super(new GridLayout(3,1));

enter = new JButton("Okay");
dayCurrent = new JTextField("Day", 2);
hourCurrent = new JTextField("Hour", 2);
monthCurrent = new JTextField("Month", 2);
yearCurrent = new JTextField("Year", 4);
dayNext = new JTextField("Day", 2);
hourNext = new JTextField("Hour", 2);
monthNext = new JTextField("Month", 2);
yearNext = new JTextField("Year", 4);
currentTime = new JLabel("Current Time:", JLabel.RIGHT);
nextTime = new JLabel("Next Time:", JLabel.RIGHT);
hoursUntil = new JLabel("xxxx", JLabel.RIGHT);

JPanel button = new JPanel(new GridLayout(0,1));
button.add(enter);

JPanel currentPane = new JPanel(new GridLayout(1,0));
currentPane.add(dayCurrent);
currentPane.add(monthCurrent);
currentPane.add(yearCurrent);
currentPane.add(hourCurrent);

JPanel nextPane = new JPanel(new GridLayout(1,0));
nextPane.add(dayNext);
nextPane.add(monthNext);
nextPane.add(yearNext);
nextPane.add(hourNext);

JPanel newTimePanel = new JPanel(new FlowLayout());
newTimePanel.add(hoursUntil);

JPanel labelsCurrent = new JPanel(new GridLayout(1,0));
labelsCurrent.add(currentTime);
labelsCurrent.add(currentPane);

JPanel labelsNext = new JPanel(new GridLayout(1,0));
labelsNext.add(nextTime);
labelsNext.add(nextPane);

setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
add(labelsCurrent, BorderLayout.CENTER);
add(labelsNext, BorderLayout.CENTER);
add(button, BorderLayout.AFTER_LAST_LINE);
}

private static void createAndShowGUI(){
frame = new JFrame("Clock Adder");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
frame.add(new Display());
frame.pack();
frame.setVisible(true);

}
public static void main(String[] args) {

javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run(){
createAndShowGUI();
}
}
);
}
}

最佳答案

不要使用 GridLayout,它的作用完全符合其设计目的,相反,请尝试使用 GridBagLayout,例如...

替换

super(new GridLayout(3,1));

与...

super(new GridBagLayout());

然后替换...

add(labelsCurrent, BorderLayout.CENTER);
add(labelsNext, BorderLayout.CENTER);
add(button, BorderLayout.AFTER_LAST_LINE);

与...

GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
add(labelsCurrent, gbc);
add(labelsNext, gbc);
add(button, gbc);

Layout

看看How to Use GridBagLayout了解更多详情...

您可能还想查看How to Use Spinners

已更新

使用GridBagLayout,没有理由在这里尝试使用复合布局...

enter image description here

        setLayout(new GridBagLayout());

dayCurrent = new JTextField(3);
hourCurrent = new JTextField(3);
monthCurrent = new JTextField(3);
yearCurrent = new JTextField(3);
dayNext = new JTextField(3);
hourNext = new JTextField(3);
monthNext = new JTextField(3);
yearNext = new JTextField(3);
enter = new JButton("Ok");

GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 1;
gbc.gridy = 0;
gbc.insets = new Insets(1, 1, 1, 1);

add(new JLabel("Day"), gbc);
gbc.gridx++;
add(new JLabel("Month"), gbc);
gbc.gridx++;
add(new JLabel("Year"), gbc);
gbc.gridx++;
add(new JLabel("Hour"), gbc);

gbc.gridx = 0;
gbc.gridy = 1;

add(new JLabel("Current Time:"), gbc);
gbc.gridx++;
add(dayCurrent, gbc);
gbc.gridx++;
add(monthCurrent, gbc);
gbc.gridx++;
add(yearCurrent, gbc);
gbc.gridx++;
add(hourCurrent, gbc);

gbc.gridx = 0;
gbc.gridy = 2;

add(new JLabel("Next Time:"), gbc);
gbc.gridx++;
add(dayNext, gbc);
gbc.gridx++;
add(monthNext, gbc);
gbc.gridx++;
add(yearNext, gbc);
gbc.gridx++;
add(hourNext, gbc);

gbc.gridy++;
gbc.gridx = 0;
gbc.gridwidth = GridBagConstraints.REMAINDER;
add(enter, gbc);

关于java - 如何修复 JFrame 程序的布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27537083/

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