private JButton button1 = new JButton("第一个表"); private JButton button2 = new JButton("第二个表"); private Container panel; JScrollPane scrollPane1 ; JScrollPane scrollPane2 ; static SimpleTableDemo frame; public SimpleTableDemo() { super("SimpleTableDemo"); Object[][] data1 = { {"Mary", "Campione", "Snowboarding", new Integer(5), new Boolean(false)}, {"Alison", "Huml", "Rowing", new Integer(3), new Boolean(true)}, {"Kathy", "Walrath", "Chasing toddlers", new Integer(2), new Boolean(false)}, {"Sharon", "Zakhour", "Speed reading", new Integer(20), new Boolean(true)}, {"Angela", "Lih", "Teaching high school", new Integer(4), new Boolean(false)} }; Object[][] data2 = { {"Mary2", "Campione", "Snowboarding", new Integer(5), new Boolean(false)}, {"Alison2", "Huml", "Rowing", new Integer(3), new Boolean(true)}, {"Kathy2", "Walrath", "Chasing toddlers", new Integer(2), new Boolean(false)}, {"Sharon2", "Zakhour", "Speed reading", new Integer(20), new Boolean(true)}, {"Angela2", "Lih", "Teaching high school", new Integer(4), new Boolean(false)} }; String[] columnNames1 = {"First Name", "Last Name", "Sport", "# of Years", "Vegetarian"}; String[] columnNames2 = {"First Name2", "Last Name2", "Sport2", "# of Years2", "Vegetarian2"}; final JTable table1 = new JTable(data1, columnNames1); final JTable table2 = new JTable(data2, columnNames2); //Create the scroll pane and add the table to it. scrollPane1 = new JScrollPane(table1); scrollPane2 = new JScrollPane(table2); //Add the scroll pane to this window. panel = (JPanel)this.getContentPane(); panel.setLayout(new GridLayout(3,1)); panel.add(scrollPane1); button1.setActionCommand("Button1"); button2.setActionCommand("Button2");
import java.awt.*;
import java.awt.event.*;public class SimpleTableDemo extends JFrame implements ActionListener {
private JButton button1 = new JButton("第一个表");
private JButton button2 = new JButton("第二个表");
private Container panel;
JScrollPane scrollPane1 ;
JScrollPane scrollPane2 ;
static SimpleTableDemo frame;
public SimpleTableDemo() { super("SimpleTableDemo"); Object[][] data1 = {
{"Mary", "Campione",
"Snowboarding", new Integer(5), new Boolean(false)},
{"Alison", "Huml",
"Rowing", new Integer(3), new Boolean(true)},
{"Kathy", "Walrath",
"Chasing toddlers", new Integer(2), new Boolean(false)},
{"Sharon", "Zakhour",
"Speed reading", new Integer(20), new Boolean(true)},
{"Angela", "Lih",
"Teaching high school", new Integer(4), new Boolean(false)}
};
Object[][] data2 = {
{"Mary2", "Campione",
"Snowboarding", new Integer(5), new Boolean(false)},
{"Alison2", "Huml",
"Rowing", new Integer(3), new Boolean(true)},
{"Kathy2", "Walrath",
"Chasing toddlers", new Integer(2), new Boolean(false)},
{"Sharon2", "Zakhour",
"Speed reading", new Integer(20), new Boolean(true)},
{"Angela2", "Lih",
"Teaching high school", new Integer(4), new Boolean(false)}
}; String[] columnNames1 = {"First Name",
"Last Name",
"Sport",
"# of Years",
"Vegetarian"};
String[] columnNames2 = {"First Name2",
"Last Name2",
"Sport2",
"# of Years2",
"Vegetarian2"}; final JTable table1 = new JTable(data1, columnNames1);
final JTable table2 = new JTable(data2, columnNames2); //Create the scroll pane and add the table to it.
scrollPane1 = new JScrollPane(table1);
scrollPane2 = new JScrollPane(table2); //Add the scroll pane to this window.
panel = (JPanel)this.getContentPane();
panel.setLayout(new GridLayout(3,1));
panel.add(scrollPane1);
button1.setActionCommand("Button1");
button2.setActionCommand("Button2");
panel.add(button1);
panel.add(button2);
button1.addActionListener(this);
button2.addActionListener(this);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e)
{ if ("Button1".equals(e.getActionCommand()))
{
frame.setVisible(false);
panel.remove(scrollPane2);
panel.add(scrollPane1);
frame.setVisible(true); }
else
{
frame.setVisible(false);
panel.remove(scrollPane1);
panel.add(scrollPane2);
frame.setVisible(true);
}
}
public static void main(String[] args) {
frame = new SimpleTableDemo();
frame.pack();
frame.setVisible(true);
}
}
写了一个简单的例子,你把Layout重新设定好了就可以了
呵呵
<div id="test"></div>在button1的click的时候生成table
window.test.innerHTML=showTable1();
在button2的click的时候生成table
window.test.innerHTML=showTable2();
先将数据分别写入两个table(但页面加载时可以隐藏)
<table id=tb1 style="display:none">.....</table>
<table id=tb2 style="display:none">.....</table>
再利用javascript脚本控制table的显示
显示第一个table:
tb1.style.display='block';
隐藏第一个table,显示第二个table:
tb1.style.display='none';
tb2.style.display='block';
this.value=='Hidden'?this.value='Show':this.value='Hidden'"><p>
<span id="temp">
<input type="button" value="hidden me!" name="btn"><p>
<table border="1">
<tr>
<td>hidden me!</td>
<td>hidden me!</td>
</tr>
</table>
</span>
<input type="button" name="btn">
<table id="tbl">
adsfasdfsadf
</table>
给每个table定义他们的id然后在button里面写onclick事件,对每个table的style.display属性做改变,
该变成 none ,就是不显示,这时整个table都不见,并且不占网页的位置,
变成block或者 ''就是显示.