java - 日历网格布局

标签 java swing user-interface layout calendar

我很难让我的标题在它自己的行上。标题运行到一周中的几天,而不是显示标题,然后在底部显示日历中的天数。这个是缺面板吗?我已经尝试过多次更改,但我真的说不出来

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/

相关文章:

java - 如何在关闭窗口时自动保存 TextArea 中的内容?

java - 与 Java 进程对话的简单方法

java - Mockito 在本地模拟 final类,但在 Jenkins 中失败

java - Java 中的节省时间 (int) 从它停止的地方开始?

Android - 调用 GONE 然后 VISIBLE 使 View 显示在错误的位置

java - 查找某个单词或短语在文档中出现的次数

java - 结束球的路径

具有过期条目的 Java 集合

java - Swing 按钮和 FillRect

java - 该 JFrame 的布局是什么?