我正在使用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);
}
最佳答案
变量名称不应以大写字母开头。 “表格”和“日期”正确,但“ CalenderPanel”则不正确。始终如一!
JPanel使用FlowLayout,因此两个组件彼此并排显示。
也许您可以使用BorderLayout
:
calenderPanel.setLayout( new BorderLayout() );
calenderPanel.add(table, BorderLayout.CENTER);
calenderPanel.add(date, BorderLayout.PAGE_START);