import java.awt.BorderLayout
import java.awt.event.ActionEvent
import java.awt.event.ActionListener
import javax.swing.*
import javax.swing.table.DefaultTableModel
public class TableTest implements ActionListener{
JFrame frame = new JFrame("JTable Test")
JButton button = new JButton("增加")
JTextField fname = new JTextField(20)
JTextField lname = new JTextField(20)
JPanel north = new JPanel()
JLabel l1 = new JLabel("姓名")
JLabel l2 = new JLabel("联系方式")
DefaultTableModel dtm = new DefaultTableModel(0, 2)
JTable table = new JTable(dtm)
JScrollPane jsp = new JScrollPane(table)
String [] temp = new String[2]
public void init() {
frame.setBounds(200, 200, 640, 480)
frame.setLayout(new BorderLayout())
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
frame.setResizable(false)
north.add(l1)
north.add(fname)
north.add(l2)
north.add(lname)
button.addActionListener(this)
north.add(button)
dtm.setColumnIdentifiers(new String[] {"姓名", "联系方式"})
frame.add(north, BorderLayout.NORTH)
frame.add(jsp, BorderLayout.CENTER)
frame.setVisible(true)
}
public static void main(String[] args) {
TableTest tt = new TableTest()
tt.init()
}
public void actionPerformed(ActionEvent e) {
temp[0] = fname.getText()
temp[1] = lname.getText()
dtm.insertRow(dtm.getRowCount(), temp)
}
}
因为BorderLayout的布局原理导致这样的,四个角的组件的展示大小是它的preferSize,很不幸Table默认的preferSize是0,所以你放上去是看不到的。你可以设置下Table的PreferSize你就可以看见,不过建议你还是把Table放在Center这个位置。还有添加JTable的时候别忘了把JTable放在JScrollPane中。
package com.bonc.mp.excplmeimport javax.swing.JPanel
import javax.swing.JFrame
import javax.swing.JTable
import java.awt.Rectangle
public class Rusult extends JFrame {
private static final long serialVersionUID = 1L
private JPanel jContentPane = null
private JTable jTable = null
public Rusult() {
super()
initialize()
}
private void initialize() {
this.setSize(300, 200)
this.setContentPane(getJContentPane())
this.setTitle("JFrame")
}
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel()
jContentPane.setLayout(null)
jContentPane.add(getJTable(), null)
}
return jContentPane
}
private JTable getJTable() {
if (jTable == null) {
jTable = new JTable()
jTable.setBounds(new Rectangle(3, 18, 172, 91))
}
return jTable
}
}
给分吧!! 呵呵 ~~
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)