JAVA GUI界面中切换表格
JAVA语言中点击不同按钮时切换到相应表格,该如何实现?要求切换时新表要替换原来的表格,而不能出现新旧表格同屏的情况既然能,请问能附上源码吗...
JAVA语言中点击不同按钮时切换到相应表格,该如何实现?要求切换时新表要替换原来的表格,而不能出现新旧表格同屏的情况
既然能,请问能附上源码吗 展开
既然能,请问能附上源码吗 展开
1个回答
2009-06-21
展开全部
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;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
public class Test {
public static void main(String args[]) {
JFrame frame = new JFrame();
final JTable table = new JTable();
frame.add(new JScrollPane(table), BorderLayout.CENTER);
String[] columnNames = {"column1", "column2"};
String[][] value1 = {{"t1-c1", "t1-c2"}};
String[][] value2 = {{"t2-c1", "t2-c2"}};
final TableModel model1 = new DefaultTableModel(value1, columnNames);
final TableModel model2 = new DefaultTableModel(value2, columnNames);
JButton button1 = new JButton("table1");
JButton button2 = new JButton("table2");
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
table.setModel(model1);
}
});
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
table.setModel(model2);
}
});
table.setModel(model1);
JPanel panel = new JPanel();
panel.add(button1);
panel.add(button2);
frame.add(panel, BorderLayout.SOUTH);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
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;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
public class Test {
public static void main(String args[]) {
JFrame frame = new JFrame();
final JTable table = new JTable();
frame.add(new JScrollPane(table), BorderLayout.CENTER);
String[] columnNames = {"column1", "column2"};
String[][] value1 = {{"t1-c1", "t1-c2"}};
String[][] value2 = {{"t2-c1", "t2-c2"}};
final TableModel model1 = new DefaultTableModel(value1, columnNames);
final TableModel model2 = new DefaultTableModel(value2, columnNames);
JButton button1 = new JButton("table1");
JButton button2 = new JButton("table2");
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
table.setModel(model1);
}
});
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
table.setModel(model2);
}
});
table.setModel(model1);
JPanel panel = new JPanel();
panel.add(button1);
panel.add(button2);
frame.add(panel, BorderLayout.SOUTH);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询