JTable定位

时间:2015-04-15 17:27:58

标签: java swing jtable layout-manager

我正在使用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);
    }

1 个答案:

答案 0 :(得分:1)

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

JPanel使用FlowLayout,因此两个组件会彼此相邻显示。

也许您可以使用BorderLayout

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