第一步:实现桌面托盘代码,包括设计窗体的图标,托盘的图标(注意图片最好是16*16像素),按键布局等
package com.lin.schedule;
import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
@SuppressWarnings("serial")
public class TestFrame extends JFrame{
private TrayIcon trayIcon=null;//托盘图标
private SystemTray systemTray=null;//系统托盘
public static final int TEXTAREA_ROWS=10;
public static final int TEXTAREA_CLOUMS=30;
public TestFrame() {
if(SystemTray.isSupported()){
systemTray=SystemTray.getSystemTray();//实例
try {
URL path=TestFrame.class.getResource("/images/22.jpg");//图片像素最好16*16
ImageIcon icon = new ImageIcon(path);
Image image = icon.getImage();
trayIcon=new TrayIcon(image); //创建托盘图标
this.setIconImage(image);//更改窗体图标
systemTray.add(trayIcon); //设置添加托盘
//添加窗口监听事件
this.addWindowListener(
new WindowAdapter(){//适配器接收window事件
@Override
public void windowIconified(WindowEvent e)//窗口图标化(最小化)触发
{
dispose();//释放本机所有资源
}
});
//为图标设置鼠标监听器
trayIcon.addMouseListener(
new MouseAdapter(){
@Override
public void mouseClicked(MouseEvent e){
if(e.getClickCount()==2){
setExtendedState(Frame.NORMAL);
setVisible(true);
}
}
});
} catch (AWTException e1) {
e1.printStackTrace();
}
// 设置标题
setTitle("定时任务窗口");
// 设置布局
setLayout(new FlowLayout());
// 按钮对象数组
/* JButton[] btn = new JButton[3];*/
JButton btn1=new JButton("增加数据");
JButton btn2=new JButton("更新数据");
JButton btn3=new JButton("删除数据");
JButton btn4=new JButton("查询数据");
JButton btn5=new JButton("退出");
//设置板面局部按钮靠西
JPanel westPanel=new JPanel();
westPanel.setLayout(new GridLayout(5,1));
westPanel.add(btn1);
westPanel.add(btn2);
westPanel.add(btn3);
westPanel.add(btn4);
westPanel.add(btn5);
/*add(westPanel,BorderLayout.WEST);*/
this.getContentPane().add(westPanel,BorderLayout.WEST);//添加面板到框架中
//添加按钮的监听事件
btn4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==btn4){
MyPanel mp = new MyPanel();
mp.getData();
}
}
});
btn5.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==btn5){
System.exit(0);
}
}
});
JTextArea textArea=new JTextArea(TEXTAREA_ROWS, TEXTAREA_CLOUMS);
JScrollPane scrollPane=new JScrollPane(textArea); //一个轻量级的可滚动视图 创建一个 JScrollPane,显示指定组件的内容,在垂直和水平两个每当组件的内容大于视图滚动条出现
add(scrollPane, BorderLayout.CENTER);
// 设置窗体的一些属性,比如点击关闭按钮时的操作
setDefaultCloseOperation(EXIT_ON_CLOSE);
// 窗体是否可调整大小
setResizable(true);
// 窗体的大小
setSize(500, 250);
// 居中显示
setLocationRelativeTo(null);
// 将窗体显示在屏幕上
setVisible(true);
}
}
public static void main(String[] args) {
// 用的时候直接new出来就可以了,代码都在构造函数里面写了
new TestFrame();
}
}
第二步:能够点击窗体按钮,弹出对话框,在对话框中显示数据库读取的数据
package com.lin.schedule;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
public class MyPanel {
Connection conn = null;
PreparedStatement pStat = null;
ResultSet rs = null;
ResultSetMetaData m = null;
public void getData(){
DB db = new DB();
try {
String sql = "select * from t_customers";
conn = db.getConn();
pStat = db.prepare(conn, sql);
rs = pStat.executeQuery();
m = rs.getMetaData();
int columns=m.getColumnCount();
String str = "<html>";
for(int i=1;i<=columns;i++){
if(i==1){
str += "<tr>";
str +="<td>" + m.getColumnName(i)+"</td>";
}
else if(i==columns){
str +="<td>" + m.getColumnName(i) + "</td></tr>";
}
else{
str +="<td>" + m.getColumnName(i) + "</td>";
}
}
while (rs.next()) {
for(int i=1;i<=columns;i++){
if(i==1){
str += "<tr>";
str +="<td>" +rs.getString(i)+"</td>";
}
else if(i==columns){
str +="<td>" + rs.getString(i) + "</td></tr>";
}
else{
str +="<td>" + rs.getString(i) + "</td>";
}
}
}
str += "</html>";
if(rs!=null){
/* JDialog dialog = new JDialog();
// dialog.setBounds(300, 200, 500, 400);
dialog.setSize(200,200);
JLabel jl = new JLabel();
System.out.println(str);
jl.setVerticalAlignment(SwingConstants.CENTER);
dialog.add(jl);
jl.setText("<html><tr><td>Id</td><td>Name</td><td>Age</td></tr><tr><td>1</td><td>TOM</td><td>21</td></tr><tr><td>2</td><td>MIKE</td><td>24</td></tr><tr><td>3</td><td>JACK</td><td>30</td></tr><tr><td>4</td><td>jerry</td><td>25</td></tr><tr><td>5</td><td>LINDA</td><td>30</td></tr></html>");
dialog.setVisible(true);
MusicPlayer mp = new MusicPlayer();
mp.MuiscPlay();
Thread.sleep(4000);
dialog.dispose(); */
JFrame fr = new JFrame("查询结果");//窗体
Container cont = fr.getContentPane();//容器
JDialog dialog = new JDialog(fr);//JDialog
JButton button = new JButton("关闭");//按钮
JLabel label=new JLabel();//文本标签
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dialog.setVisible(false);//“关闭”JDialog,用System.exit()会让主窗体也被关掉
}
});//添加监听器
// fr.setLayout(null);//绝对布局
fr.setLocationRelativeTo(null);//居中显示
button.setBounds(200, 360, 100, 20);//设置位置
fr.setSize(500,420);//设置窗体大小
cont.add(button);//将按钮添加至容器
// label.setBounds(225, 225, 30, 30);//label大小
label.setSize(100, 100);
label.setText(str);
cont.add(label,BorderLayout.CENTER);
fr.setVisible(true);//显示窗体
}
} catch (SQLException e) {
e.printStackTrace();
} /*catch (InterruptedException e) {
e.printStackTrace();
}*/finally {
try {
rs.close();
pStat.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
第二步中虽然写了数据库的查询语句,但是可以先注释掉之后再添上。
第三步写数据库的查询语句
package com.lin.schedule;
import java.sql.*;
public class DB {
public static Connection getConn() {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost/study1?user=root&password=root");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static PreparedStatement prepare(Connection conn, String sql) {
PreparedStatement pstmt = null;
try {
if(conn != null) {
pstmt = conn.prepareStatement(sql);
}
} catch (SQLException e) {
e.printStackTrace();
}
return pstmt;
}
public static PreparedStatement prepare(Connection conn, String sql, int autoGenereatedKeys) {
PreparedStatement pstmt = null;
try {
if(conn != null) {
pstmt = conn.prepareStatement(sql, autoGenereatedKeys);
}
} catch (SQLException e) {
e.printStackTrace();
}
return pstmt;
}
public static Statement getStatement(Connection conn) {
Statement stmt = null;
try {
if(conn != null) {
stmt = conn.createStatement();
}
} catch (SQLException e) {
e.printStackTrace();
}
return stmt;
}
/*
public static ResultSet getResultSet(Connection conn, String sql) {
Statement stmt = getStatement(conn);
ResultSet rs = getResultSet(stmt, sql);
close(stmt);
return rs;
}
*/
public static ResultSet getResultSet(Statement stmt, String sql) {
ResultSet rs = null;
try {
if(stmt != null) {
rs = stmt.executeQuery(sql);
}
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
public static void executeUpdate(Statement stmt, String sql) {
try {
if(stmt != null) {
stmt.executeUpdate(sql);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void close(Connection conn) {
try {
if(conn != null) {
conn.close();
conn = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void close(Statement stmt) {
try {
if(stmt != null) {
stmt.close();
stmt = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void close(ResultSet rs) {
try {
if(rs != null) {
rs.close();
rs = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
运行结果如图: