我只是想知道是否有人知道如何修复我的代码,以便所有这些都出现在一个列中,而不是它一直给我的两个,我希望它只显示一个列并且具有良好和紧凑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();
}
}
);
}
}
答案 0 :(得分:2)
不要使用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);
请查看How to Use GridBagLayout了解更多详情......
您可能还想查看How to Use Spinners
<强>更新强>
使用GridBagLayout
,没有理由在这里尝试使用复合布局......
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);