gpt4 book ai didi

java - GridLayout .add 错误

转载 作者:太空宇宙 更新时间:2023-11-04 13:54:37 24 4
gpt4 key购买 nike

我的程序中似乎存在一个我一生都无法修复的错误。

这是代码:

import javax.swing.*;
import javax.swing.text.*;

import java.awt.*;
import java.util.*;

public class CalendarGUI extends JPanel {
ProgramCalendar currentCalendar;
GregorianCalendar sideCalendar;
String[] month = {"January", "February", "March", "April", "May", "June", "July", "August",
"September", "October", "November", "December"};

public CalendarGUI(ProgramCalendar pc){
currentCalendar = pc;
sideCalendar = new GregorianCalendar();
JPanel calendarSidePanel = new JPanel();
calendarSidePanel.setSize(400,400);
calendarSidePanel.setLayout(new BorderLayout());
//sets month
JTextPane monthText = new JTextPane();
monthText.setText(month[sideCalendar.get(Calendar.MONTH)]);
calendarSidePanel.add(monthText, BorderLayout.NORTH);

//main part of error
JPanel sideCalendarMain = new JPanel(new GridLayout(6, 7, 5, 5));

JTextPane sun = new JTextPane();
sun.setText("Sun");
sideCalendarMain.add(sun);

JTextPane mon = new JTextPane();
mon.setText("Mon");
sideCalendarMain.add(mon);

JTextPane tues = new JTextPane();
tues.setText("Tues");
sideCalendarMain.add(tues);

JTextPane wed = new JTextPane();
wed.setText("Wed");
sideCalendarMain.add(wed);

JTextPane thurs = new JTextPane();
thurs.setText("Thur");
sideCalendarMain.add(thurs);

JTextPane fri = new JTextPane();
fri.setText("Fri");
sideCalendarMain.add(fri);

JTextPane sat = new JTextPane();
sat.setText("Sat");
sideCalendarMain.add(sat);

calendarSidePanel.add(sideCalendarMain, BorderLayout.CENTER);

add(calendarSidePanel);
setSize(1000,1000);
}

}

问题是我以为它会这样显示:

            April
Sun Mon Tues Wed Thurs Fri Sat

而不是

 April
Sun Mon
Tues Wed
Thurs Fri
Sat

当前正在显示。

我只是误用了 GridLayout 还是有其他一些我遗漏的基本错误,或者是一些随机错误。谢谢

最佳答案

在 GridLayoud 中,行优先于列。来自文档:

When both the number of rows and the number of columns have been set to non-zero values, either by a constructor or by the setRows and setColumns methods, the number of columns specified is ignored. Instead, the number of columns is determined from the specified number of rows and the total number of components in the layout. So, for example, if three rows and two columns have been specified and nine components are added to the layout, they will be displayed as three rows of three columns. Specifying the number of columns affects the layout only when the number of rows is set to zero.

因此,您指定 GridLayout 有 6 行,这就是组件的布局方式。如果您只想指定一行,请将 new GridLayout(6, 7, 5, 5) 更改为 new GridLayout(1, 7, 5, 5)

关于java - GridLayout .add 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29954118/

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