日期:2014-05-20 浏览次数:20762 次
package 测试JTable对界面的影响;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class test
{
public static void main(String[] args)
{
new f();
}
}
class f extends JFrame implements ActionListener
{
JButton east, west, north, south;
JPanel peast, pwest, pnorth, psouth;
JScrollPane jsp;
JTable table;
Object a[][];
Object name[] = {"地方", "dsf", "few", "fsfse"};
f()
{
east = new JButton("窗口变大");
east.addActionListener(this);
west = new JButton("窗口变小");
west.addActionListener(this);
north = new JButton("移除其他组件");
north.addActionListener(this);
south = new JButton("显示所有组件");
south.addActionListener(this);
a = new Object[20][4];
for(int i=0; i<20; i++)
{
a[i][0] = i;
a[i][1] = i;
a[i][2] = i;
a[i][3] = i;
}
table = new JTable(a, name);
peast = new JPanel();
pwest = new JPanel();
pnorth = new JPanel();
psouth = new JPanel();
jsp = new JScrollPane();
peast.add(east);
pwest.add(west);
pnorth.add(north);
psouth.add(south);
//north.setSize(100, 100); //这两条语句是不是有问题呢?
//pnorth.setSize(100, 100); //为什么按钮并没有改变大小?
add(peast, BorderLayout.EAST);
add(pwest, BorderLayout.WEST);
add(pnorth, BorderLayout.NORTH);
add(psouth, BorderLayout.SOUTH);
add(jsp, BorderLayout.CENTER);
setBounds(100, 150, 350, 350);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == east)
{
jsp = new JScrollPane(table);
this.getContentPane().remove(jsp);
this.add(jsp, BorderLayout.CENTER);
this.setBounds(100, 150, 500, 500);
this.repaint();
}
else if(e.getSource() == west)
{
jsp = new JScrollPane(table);
this.getContentPane().remove(jsp);
this.add(jsp, BorderLayout.CENTER);
this.setBounds(100, 150, 300, 300);
this.repaint();
}
else if(e.getSource() == north)
{
this.getContentPane().removeAll();
this.repaint();
this.add(pnorth, BorderLayout.NORTH);
this.add(psouth, BorderLayout.SOUTH);
}
else if(e.getSource() == south)
{
jsp = new JScrollPane(table);
this.getContentPane().removeAll();
this.add(peast, BorderLayout.EAST);
this.add(pwest, BorderLayout.WEST);
this.add(pnorth, BorderLayout.NORTH);
this.add(psouth, BorderLayout.SOUTH);
this.add(jsp, BorderLayout.CENTER);