JTable定位

JTable positioning

我正在使用 Swing/Java 和 MVC 模式创建一个日历应用程序。我正在尝试将 JTable 定位到类似于下图的位置,但是 This.setSize 和 table.setPreferedSize 似乎无法完成这项工作。任何反馈表示赞赏。

当前 GUI:http://gyazo.com/f1d4a3e8b08e40440af5e1c514727be8 预期的 GUI:http://gyazo.com/8352843f58eb116a7334f2b01c40c1a4

    public class CalenderView extends JFrame {

//Eclipse freaks out if this isnt here.
private static final long serialVersionUID = 1L;
//JPanel houses the JFrame
JPanel CalenderPanel = new JPanel();
//Table takes in cell values
JTable table = new JTable(5,7);

//This is a Label which has a getter to the current date
JLabel date = new JLabel("Today is : " + getdate());

    public CalenderView(){
        //Close the application when X is pressed
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Default size of application
        this.setName("Calendar");
        this.getAlignmentX();
        this.getAlignmentY();
        this.setSize(850, 550);
        this.setResizable(false);

        //add GUI to JPanel
        CalenderPanel.add(table);
        CalenderPanel.add(date);

        //add the JPanel to the JFrame
        this.add(CalenderPanel);
        //centers the application native to the users res
        setLocationRelativeTo(null);
    }

变量名不应以大写字符开头。 "table" 和 "date" 是正确的,但 "CalenderPanel" 不是。保持一致!

JPanel 使用 FlowLayout,因此两个组件并排显示。

也许你可以使用 BorderLayout:

calenderPanel.setLayout( new BorderLayout() );
calenderPanel.add(table, BorderLayout.CENTER);
calenderPanel.add(date, BorderLayout.PAGE_START);