我很难让我的标题在它自己的行上。标题运行到一周中的几天,而不是显示标题,然后在底部显示日历中的天数。这个是缺面板吗?我已经尝试过多次更改,但我真的说不出来
package Exercise15_5;
import java.awt.*;
import javax.swing.*;
import java.util.*;
public class Exercise15_5 extends JFrame {
public Exercise15_5(){
//Create panel with gridlayout
JPanel calendar = new JPanel(new BorderLayout());
calendar.setLayout(new GridLayout(5,7));
//Add headers
String[] headers = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
for(int i = 0; i <7; i++){
calendar.add(new JLabel("" + headers[i]));
}
//Add days to calendar
for(int i = 1; i <31; i++){
calendar.add(new JLabel("" + i));
}
JPanel monthHeader = new JPanel(new BorderLayout());
monthHeader.add(new JTextField("\t\t\t04/2014"), BorderLayout.NORTH);
monthHeader.add(calendar, BorderLayout.CENTER);
add(monthHeader);
}
public static void main(String[] args) {
Exercise15_5 frame = new Exercise15_5();
frame.setTitle("Exercise 15_5");
frame.setSize(600,300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
最佳答案
尝试将面板的布局设置为 new GridLayout(0,7)
。
GridLayout API陈述如下:
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.
关于java - 日历网格布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23038917/