private static final long serialVersionUID = 1L
private JPanel jPanel = null
private JTextField jTextField = null
private JTextField jTextField1 = null
private JButton jButton = null
private JButton jButton1 = null
private JButton jButton2 = null
private JTable jTable = null
private DefaultTableModel tableModel = null
private JPanel getJPanel() {
if (jPanel == null) {
JLabel jLabel1 = new JLabel()
jLabel1.setBounds(new Rectangle(30, 50, 80, 25))
jLabel1.setText("请输入ISBN:")
JLabel jLabel = new JLabel()
jLabel.setBounds(new Rectangle(30, 20, 80, 25))
jLabel.setText("请输入书名:")
jPanel = new JPanel()
jPanel.setLayout(null)
jPanel.setPreferredSize(new Dimension(1, 120))
jPanel.add(jLabel)
jPanel.add(jLabel1)
jPanel.add(getJTextField())
jPanel.add(getJTextField1())
jPanel.add(getJButton())
jPanel.add(getJButton1())
jPanel.add(getJButton2())
}
return jPanel
}
private JTextField getJTextField() {
if (jTextField == null) {
jTextField = new JTextField()
jTextField.setBounds(new Rectangle(120, 20, 140, 25))
}
return jTextField
}
private JTextField getJTextField1() {
if (jTextField1 == null) {
jTextField1 = new JTextField()
jTextField1.setBounds(new Rectangle(120, 50, 140, 25))
}
return jTextField1
}
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton()
jButton.setBounds(new Rectangle(270, 20, 60, 25))
jButton.setText("显示")
jButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
/* 修改处 */
}
})
}
return jButton
}
private JButton getJButton1() {
if (jButton1 == null) {
jButton1 = new JButton()
jButton1.setBounds(new Rectangle(270, 50, 60, 25))
jButton1.setText("显示")
jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
/* 修改处 */
}
})
}
return jButton1
}
private JButton getJButton2() {
if (jButton2 == null) {
jButton2 = new JButton()
jButton2.setBounds(new Rectangle(240, 80, 90, 30))
jButton2.setText("显示全部")
jButton2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
/*
* 修改处此处只要改下面一行,换成从数据库读就行
* 应该在DAO层加一个查询全部的方法
* public List<Titles>getAll()具体实现自己写。
* 把114行的代码改成:List<Titles>lists = new TitleDaoImpl().getAll()
* */
List<Titles>lists = Titles.list()
// 将表行数设为0行,防止异常
tableModel.setRowCount(0)
for (int i = 0i <lists.size()i++) { // 当每有一个Titles对象就添加一行
tableModel.addRow(new Object[] {})
}
Titles instance = null
Object obj = null
Object name = null
for (int i = 0i <tableModel.getRowCount()i++) { // 行循环
instance = lists.get(i) // 为当前Titles对象赋值
for (int j = 0j <tableModel.getColumnCount()j++) {
name = tableModel.getColumnName(j)
if (name.equals("ISBN")) {
obj = instance.getISBN()
} else if (name.equals("title")) {
obj = instance.getTitle()
} else if (name.equals("publisher")) {
obj = instance.getPublisher()
} else if (name.equals("date")) {
obj = instance.getDate()
} else if (name.equals("price")) {
obj = instance.getPrice()
} else {
obj = ""
}
tableModel.setValueAt(obj, i, j)
}
}
}
})
}
return jButton2
}
private JPanel getJPanel1() {
JScrollPane jScrollPane = new JScrollPane()
jScrollPane.setViewportView(getJTable())
JPanel panel = new JPanel(new BorderLayout())
panel.add(jScrollPane, BorderLayout.CENTER)
return panel
}
private JTable getJTable() {
if (jTable == null) {
// 获得Titles类里的字段,一个字段在JTable中应该表现为一列
Field[] fields = Titles.class.getDeclaredFields()
// 将字段名保存为列名数组为DefaultTableModel所用
Object[] columnNames = new Object[fields.length]
for (int i = 0i <fields.lengthi++) {
columnNames[i] = fields[i].getName()
}
tableModel = new DefaultTableModel(columnNames, 0)
jTable = new JTable(tableModel)
}
return jTable
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
T thisClass = new T()
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
thisClass.setVisible(true)
}
})
}
public T() {
super()
setSize(580, 475)
getContentPane().add(getJPanel(), BorderLayout.SOUTH)
getContentPane().add(getJPanel1(), BorderLayout.CENTER)
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)