怎么控制表格让它能够显示某列和不显示某列 怎么控制表格让它能够显示某列和不显示某列 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这是完整代码:import javax.swing.*;import javax.swing.table.*;import java.awt.*;import java.awt.event.*;import java.util.*; public class Test extends JFrame {JTable table = new JTable(new Object[][] {{"Mouse", "Mighty", "M." },{"Mouse", "Polly", "A." },{"Doright", "Dudley", "L." }},new Object[] {"Last Name", "First Name", "Middle Initial" });public Test() {Container cp = getContentPane();cp.add(new JScrollPane(table), BorderLayout.CENTER);cp.add(new ControlPanel(), BorderLayout.NORTH);}class ControlPanel extends JPanel {private JCheckBox checkBox = new JCheckBox("First Name Column Showing");public ControlPanel() {final TableColumnModel tcm = table.getColumnModel();final TableColumn firstNameColumn = table.getColumn("First Name");checkBox.setSelected(true);add(checkBox);checkBox.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) {if(checkBox.isSelected()) {tcm.addColumn(firstNameColumn);tcm.moveColumn(2,1);}else {tcm.removeColumn(firstNameColumn);}table.sizeColumnsToFit(-1);}});}}public static void main(String args[]) {GJApp.launch(new Test(), "Showing/Hiding Columns",300,300,450,175);}}class GJApp extends WindowAdapter {static private JPanel statusArea = new JPanel();static private JLabel status = new JLabel(" ");static private ResourceBundle resources;public static void launch(final JFrame f, String title,final int x, final int y, final int w, int h) {launch(f,title,x,y,w,h,null); }public static void launch(final JFrame f, String title,final int x, final int y, final int w, int h,String propertiesFilename) {f.setTitle(title);f.setBounds(x,y,w,h);f.setVisible(true);statusArea.setBorder(BorderFactory.createEtchedBorder());statusArea.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));statusArea.add(status);status.setHorizontalAlignment(JLabel.LEFT);f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);if(propertiesFilename != null) {resources = ResourceBundle.getBundle(propertiesFilename, Locale.getDefault());}f.addWindowListener(new WindowAdapter() {public void windowClosed(WindowEvent e) {System.exit(0);}});}static public JPanel getStatusArea() {return statusArea;}static public void showStatus(String s) {status.setText(s);}static Object getResource(String key) {if(resources != null) {return resources.getString(key);}return null;}} jvm并不仅仅由一个公司所开发对吗? 关于抽象类?? 请教(正则表达式)用 正则表达式可以做到 查找一个字符串在另外个字符串中出现的次数吗? 如何判断一个类是抽象类 在JBuild中调用批处理出现的问题 怎么让文本框里的时间动起来 郁闷,为什么不能设置布局管理器? 怎么把Date转换成格式为“2004-03-17”的字符串? java数据库问题 JAVA的GUI事件处理 怎样用正则表达式截取截取小数点前面的值。 哪位大侠用过JTaskPane这个新组件,有问题请赐教!~
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Test extends JFrame {
JTable table = new JTable(
new Object[][] {
{"Mouse", "Mighty", "M." },
{"Mouse", "Polly", "A." },
{"Doright", "Dudley", "L." }
},
new Object[] {
"Last Name", "First Name", "Middle Initial"
}
);
public Test() {
Container cp = getContentPane();cp.add(new JScrollPane(table), BorderLayout.CENTER);
cp.add(new ControlPanel(), BorderLayout.NORTH);
}
class ControlPanel extends JPanel {
private JCheckBox checkBox = new JCheckBox(
"First Name Column Showing");
public ControlPanel() {
final TableColumnModel tcm = table.getColumnModel();
final TableColumn firstNameColumn =
table.getColumn("First Name");checkBox.setSelected(true);
add(checkBox);checkBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
if(checkBox.isSelected()) {
tcm.addColumn(firstNameColumn);
tcm.moveColumn(2,1);
}
else {
tcm.removeColumn(firstNameColumn);
}
table.sizeColumnsToFit(-1);
}
});
}
}
public static void main(String args[]) {
GJApp.launch(
new Test(), "Showing/Hiding Columns",300,300,450,175);
}
}
class GJApp extends WindowAdapter {
static private JPanel statusArea = new JPanel();
static private JLabel status = new JLabel(" ");
static private ResourceBundle resources;public static void launch(final JFrame f, String title,
final int x, final int y,
final int w, int h) {
launch(f,title,x,y,w,h,null);
}
public static void launch(final JFrame f, String title,
final int x, final int y,
final int w, int h,
String propertiesFilename) {
f.setTitle(title);
f.setBounds(x,y,w,h);
f.setVisible(true);statusArea.setBorder(BorderFactory.createEtchedBorder());
statusArea.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
statusArea.add(status);
status.setHorizontalAlignment(JLabel.LEFT);f.setDefaultCloseOperation(
WindowConstants.DISPOSE_ON_CLOSE);if(propertiesFilename != null) {
resources = ResourceBundle.getBundle(
propertiesFilename, Locale.getDefault());
}f.addWindowListener(new WindowAdapter() {
public void windowClosed(WindowEvent e) {
System.exit(0);
}
});
}
static public JPanel getStatusArea() {
return statusArea;
}
static public void showStatus(String s) {
status.setText(s);
}
static Object getResource(String key) {
if(resources != null) {
return resources.getString(key);
}
return null;
}
}