java swing编程基础(一)

/*
* ListTemperaFrame.java
*
* Created on __DATE__, __TIME__
*/

package com.ruif.view;

import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Vector;

import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

import com.ruif.dao.TemperaDAO;
import com.ruif.model.Tempera;
import com.ruif.util.DBUtil;
import com.ruif.util.StringUtil;

/**
*
* @author __USER__
*/
public class ListTemperaFrame extends javax.swing.JInternalFrame
{

/**
*
*/
private static final long serialVersionUID = 664971768900060757L;

DBUtil dbUtil = new DBUtil();
TemperaDAO temDao = new TemperaDAO();

/** Creates new form ListTemperaFrame */
public ListTemperaFrame()
{
initComponents();
this.setLocation(200, 50);
fillDesktop(null);
}

//GEN-BEGIN:initComponents
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents()
{

jScrollPane1 = new javax.swing.JScrollPane();
jTable_list = new javax.swing.JTable();
jLabel1 = new javax.swing.JLabel();
jTextField_id = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
jTextField_temp = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
jTextField_date = new javax.swing.JTextField();
jLabel4 = new javax.swing.JLabel();
jButton_query = new javax.swing.JButton();
jButton_delete = new javax.swing.JButton();
jButton_modify = new javax.swing.JButton();
jComboBox_status = new javax.swing.JComboBox();
jButton_clean = new javax.swing.JButton();

setClosable(true);
setIconifiable(true);

jTable_list.setModel(new javax.swing.table.DefaultTableModel(
new Object[][] {

}, new String[] { "编号", "温度", "日期", "状态" })
{
boolean[] canEdit = new boolean[] { false, false, false, false };

public boolean isCellEditable(int rowIndex, int columnIndex)
{
return canEdit[columnIndex];
}
});
jTable_list.addMouseListener(new java.awt.event.MouseAdapter()
{
public void mousePressed(java.awt.event.MouseEvent evt)
{
jTable_listMousePressed(evt);
}
});
jScrollPane1.setViewportView(jTable_list);

jLabel1.setText("\u7f16 \u53f7\uff1a");

jTextField_id.setEditable(false);

jLabel2.setText("\u6e29 \u5ea6\uff1a");

jLabel3.setText("\u65e5 \u671f\uff1a");

jLabel4.setText("\u72b6 \u6001\uff1a");

jButton_query.setIcon(new javax.swing.ImageIcon(
"C:\\Documents and Settings\\pc\\桌面\\Images\\query.png")); // NOI18N
jButton_query.setText("\u67e5\u8be2");
jButton_query.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
jButton_queryActionPerformed(evt);
}
});

jButton_delete.setIcon(new javax.swing.ImageIcon(
"C:\\Documents and Settings\\pc\\桌面\\Images\\delete.png")); // NOI18N
jButton_delete.setText("\u5220\u9664");
jButton_delete.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
jButton_deleteActionPerformed(evt);
}
});

jButton_modify.setIcon(new javax.swing.ImageIcon(
"C:\\Documents and Settings\\pc\\桌面\\Images\\modify.png")); // NOI18N
jButton_modify.setText("\u4fee\u6539");
jButton_modify.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
jButton_modifyActionPerformed(evt);
}
});

jComboBox_status.setModel(new javax.swing.DefaultComboBoxModel(
new String[] { "正常", "报警" }));

jButton_clean.setText("\u6e05\u7a7a");
jButton_clean.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
jButton_cleanActionPerformed(evt);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(layout
.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
layout.createSequentialGroup()
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
layout.createSequentialGroup()
.addContainerGap()
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(
jScrollPane1,
javax.swing.GroupLayout.DEFAULT_SIZE,
469,
Short.MAX_VALUE)
.addGroup(
layout.createSequentialGroup()
.addGap(22,
22,
22)
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
layout.createSequentialGroup()
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(
jLabel1)
.addComponent(
jLabel3))
.addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING,
false)
.addComponent(
jTextField_id)
.addComponent(
jTextField_date,
javax.swing.GroupLayout.DEFAULT_SIZE,
97,
Short.MAX_VALUE))
.addGap(18,
18,
18)
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.TRAILING,
false)
.addGroup(
javax.swing.GroupLayout.Alignment.LEADING,
layout.createSequentialGroup()
.addComponent(
jLabel2)
.addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(
jTextField_temp,
javax.swing.GroupLayout.PREFERRED_SIZE,
109,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(
javax.swing.GroupLayout.Alignment.LEADING,
layout.createSequentialGroup()
.addComponent(
jLabel4)
.addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(
jComboBox_status,
0,
javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE))))
.addGroup(
layout.createSequentialGroup()
.addComponent(
jButton_query)
.addGap(18,
18,
18)
.addComponent(
jButton_clean,
javax.swing.GroupLayout.PREFERRED_SIZE,
72,
javax.swing.GroupLayout.PREFERRED_SIZE))))))
.addGroup(
layout.createSequentialGroup()
.addGap(34, 34,
34)
.addComponent(
jButton_delete)
.addGap(18, 18,
18)
.addComponent(
jButton_modify)))
.addContainerGap()));
layout.setVerticalGroup(layout
.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
layout.createSequentialGroup()
.addGap(48, 48, 48)
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
layout.createSequentialGroup()
.addGap(2, 2, 2)
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(
jLabel1)
.addComponent(
jTextField_id,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(13, 13,
13)
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(
jLabel3)
.addComponent(
jTextField_date,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(
layout.createSequentialGroup()
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(
jLabel2)
.addComponent(
jTextField_temp,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(13, 13,
13)
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(
jLabel4)
.addComponent(
jComboBox_status,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))))
.addGap(18, 18, 18)
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton_query)
.addComponent(
jButton_clean,
javax.swing.GroupLayout.PREFERRED_SIZE,
25,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(50, 50, 50)
.addComponent(jScrollPane1,
javax.swing.GroupLayout.PREFERRED_SIZE,
169,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(26, 26, 26)
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton_modify)
.addComponent(jButton_delete))
.addContainerGap(54, Short.MAX_VALUE)));

pack();
}// </editor-fold>
//GEN-END:initComponents

private void jButton_cleanActionPerformed(java.awt.event.ActionEvent evt)
{
this.resetValue();
this.fillDesktop(null);
}

private void jButton_modifyActionPerformed(java.awt.event.ActionEvent evt)
{

String id = jTextField_id.getText().trim();
String temp = jTextField_temp.getText().trim();
String date = jTextField_date.getText().trim();
int status = "正常".equals(jComboBox_status.getSelectedItem()) ? 0 : 1;

if (StringUtil.isEmpty(id))
{
JOptionPane.showMessageDialog(null, "请选择要修改的记录!");
return;
}

Tempera tem = new Tempera(Long.parseLong(id), Integer.parseInt(temp),
date, status);
Connection conn = null;
try
{
conn = dbUtil.getConn();
int ok = temDao.modifyTempera(conn, tem);
if (ok == 1)
{
JOptionPane.showMessageDialog(null, "修改成功");
this.resetValue();
this.fillDesktop(null);
}
else
{
JOptionPane.showMessageDialog(null, "修改失败!");
}
}
catch (Exception e)
{
e.printStackTrace();
JOptionPane.showMessageDialog(null, "修改失败!");
}
finally
{
try
{
dbUtil.closeConn(conn);
}
catch (Exception e)
{
e.printStackTrace();
}
}

}

private void jButton_deleteActionPerformed(java.awt.event.ActionEvent evt)
{
String id = jTextField_id.getText().trim();
if (StringUtil.isEmpty(id))
{
JOptionPane.showMessageDialog(null, "请选择要删除的记录!");
return;
}

Connection conn = null;
try
{
conn = dbUtil.getConn();
int ok = temDao.deleteTempera(conn, id);
if (ok == 1)
{
JOptionPane.showMessageDialog(null, "删除成功!");
this.resetValue();
this.fillDesktop(null);
}
else
{
JOptionPane.showMessageDialog(null, "删除失败!");
return;
}
}
catch (Exception e)
{
e.printStackTrace();
JOptionPane.showMessageDialog(null, "删除失败!");
return;
}
finally
{
try
{
dbUtil.closeConn(conn);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}

/**
* 重置
*/
private void resetValue()
{
this.jTextField_id.setText("");
this.jTextField_temp.setText("");
this.jTextField_date.setText("");
this.jComboBox_status.setSelectedItem("正常");
}

/**
* table中触发 pressed事件效果
* @param evt
*/
private void jTable_listMousePressed(java.awt.event.MouseEvent evt)
{
int row = this.jTable_list.getSelectedRow();
this.jTextField_id.setText(jTable_list.getValueAt(row, 0) + "");
this.jTextField_temp.setText(jTable_list.getValueAt(row, 1) + "");
this.jTextField_date.setText(jTable_list.getValueAt(row, 2) + "");
this.jComboBox_status.setSelectedItem(jTable_list.getValueAt(row, 3)
+ "");
}

private void jButton_queryActionPerformed(java.awt.event.ActionEvent evt)
{
int temp = StringUtil.isEmpty(this.jTextField_temp.getText().trim()) ? 0 : Integer.parseInt(this.jTextField_temp.getText().trim());
String date = this.jTextField_date.getText().trim();
int status = this.jComboBox_status.getSelectedIndex();
Tempera tem = new Tempera(temp, date == null ? "" : date, status);
this.fillDesktop(tem);
}

public void fillDesktop(Tempera tem)
{
DefaultTableModel dtm = (DefaultTableModel) jTable_list.getModel();
dtm.setRowCount(0);//设置成 0 行
Connection conn = null;
try
{
conn = dbUtil.getConn();
ResultSet rs = temDao.listTempera(conn, tem);
while (rs.next())
{
Vector v = new Vector();
v.add(rs.getLong("id") + "");
v.add(rs.getInt("temp") + "");
v.add(rs.getString("date"));
v.add(rs.getInt("status") == 0 ? "正常" : "报警");
dtm.addRow(v);
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
dbUtil.closeConn(conn);
}
catch (Exception e)
{
e.printStackTrace();
}
}

}

//GEN-BEGIN:variables
// Variables declaration - do not modify
private javax.swing.JButton jButton_clean;
private javax.swing.JButton jButton_delete;
private javax.swing.JButton jButton_modify;
private javax.swing.JButton jButton_query;
private javax.swing.JComboBox jComboBox_status;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable_list;
private javax.swing.JTextField jTextField_date;
private javax.swing.JTextField jTextField_id;
private javax.swing.JTextField jTextField_temp;
// End of variables declaration//GEN-END:variables

}


--来自网页客户端
二维码.jpg
已邀请:

要回复问题请先登录注册

Template error, template file not found