JAVA课设笔记-GUI-表格部分 随笔:
-
setSize(),setLocation(),setBounds()
的区别:
setSize(int width, int height)
:定义控件的大小,有两个参数,分别对应宽度和高度;setLocation(int x, int y)
:设置组件的在窗口的位置,用x 和 y 参数来指定新位置的左上角setBounds(int x, int y, int width, int height)
:四个参数,既定义组件的位置,也定义控件的大小; 相当于上面两个函数的功能的组合。 -
JScrollPane
组件:
JScrollPane
是一个滚动面板,里面只能添加一个组件,用以当前页面难以显示完全所有的组件,又因为其中只能添加一个组件,所以常常将一个JPanel
添加到里面,而在JPanel
里面可以加很多其他的组件。在构造JScrollPane
的时候,可以直接调用他的另一个构造器JScrollPane(zujian)
从而直接把组件加入进去。 -
repaint()
方法:
在表格中,如果更新了表格里面的数据,那么需要调用repaint()
方法让表格得以更新。 -
ActionListener
接口
需要在实现该接口的类中实现actionPerformed
方法,用getSource()
方法可以获取鼠标所点击的组件
例子:
public void actionPerformed(ActionEvent e){
//在此处编写具体的事项
//如果有多个事件处理,可以用多个if来分别实现
if (e.getSource() == inputRowsNumber){
int initRows = Integer.parseInt(inputRowsNumber.getText());
}
}
- 在变换窗口的显示内容时,用
removeAll()
方法以清除之前的各种组件布局,用getContentPane()
获取当前的面板
例子:
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == inputRowsNumber)
{
count = 0;
initRows = Integer.parseInt(inputRowsNumber.getText());
a = new Object[initRows][4];
for (int i = 0; i < initRows; i++)
{
for (int j = 0; j < 4; j++)
a[i][j] = "0";
}
table = new JTable(a, name);
table.setRowHeight(20);
getContentPane().removeAll();
add(new JScrollPane(table), BorderLayout.CENTER);
add(pSouth, BorderLayout.SOUTH);
add(pNorth, BorderLayout.NORTH);
validate();
}