java关于swing中的JTable的问题 50
我现在想在一个窗口中加入一个JTable为两行四列第一行的四列分别是列名而第二行的每列都空着用来输入的,怎么实现呢?刚开始我是想把第二行的每列都加进去一个JTextFie...
我现在想在一个窗口中加入一个JTable 为两行四列 第一行的四列分别是列名 而第二行的每列都空着用来输入的,怎么实现呢?刚开始我是想把第二行的每列都加进去一个JTextField,结果不行,还有什么思路?
展开
2个回答
展开全部
你给范例吧!!
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();
}// </editor-fold>
/**
* @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);
}
//</editor-fold>
/* 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;
}
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();
}// </editor-fold>
/**
* @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);
}
//</editor-fold>
/* 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;
}
展开全部
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();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询