java关于swing中的JTable的问题

2025-06-21 19:15:35
推荐回答(2个)
回答1:

你给范例吧!!

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;
import javax.swing.ScrollPaneConstants;
import javax.swing.table.DefaultTableModel;
import org.jfree.ui.ApplicationFrame;

public class test2 extends ApplicationFrame {
DefaultTableModel model;
public void windowClosing(WindowEvent e){
dispose();
}

/**
* Creates new form QuickVerificationShow
*/
public test2() {
super("");//设定标题
initComponents();
this.pack();
this.setVisible(true);

}

private void initComponents() {

jButton1 = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jButton1.setText("新增");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonActionPerformed(evt);
}
private void jButtonActionPerformed(ActionEvent evt) {
model.insertRow(model.getRowCount(), new Object[] {"z","x"});
}
});
jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
jScrollPane1.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
jScrollPane1.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
model = new DefaultTableModel(
new Object[][] {
{null, null},
{null, null}
},
new String[] {
"X1", "X2"

});
jTable1 = new JTable(model){
public boolean isCellEditable(int row, int column) {
return false;
}
};
jTable1.getTableHeader().setReorderingAllowed(false);
jTable1.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
jScrollPane1.setViewportView(jTable1);

org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(18, 18, 18)
.add(jButton1)
.addContainerGap(200, Short.MAX_VALUE))
.add(jScrollPane1)
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(jButton1)
.add(18, 18, 18))
.add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
this.setTitle("模组测试快速查询");
pack();
}//
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(test2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(test2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(test2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(test2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new test2();
}
});
// new QuickVerificationShow();
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JScrollPane jScrollPane1;
public static javax.swing.JTable jTable1;

}

回答2:

import java.awt.HeadlessException;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class Exam2 extends JFrame {


public Exam2() throws HeadlessException {
JTable table = new JTable(1,4){
private static final long serialVersionUID = 1L;

@Override
public boolean isCellEditable(int row, int column) {
if(row == 0){
return true;
}
return super.isCellEditable(row, column);
}
};
DefaultTableModel model = (DefaultTableModel) table.getModel();
model.setColumnIdentifiers(new String[]{"tttt","uuuu","uuuu","ppppp"});


JScrollPane scrollPanel = new JScrollPane();
scrollPanel.getViewport().add(table);

this.getContentPane().add(scrollPanel);
this.setSize(800, 500);
this.setTitle("JMemuItem");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}

/**
 * @param args
 */
public static void main(String[] args) {

new Exam2();
}


}