package com.service.test; /** * Created by Administrator on 2018-04-04. */ import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.net.URL; public class TestJFrame { private static TrayIcon trayIcon = null; static SystemTray tray = SystemTray.getSystemTray(); public static void main(String[] args) { startView(); } private static void startView() { JFrame frame = new JFrame("****系统"); // 创建 JFrame 实例 frame.setSize(400, 300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationRelativeTo(null); JPanel panel = new JPanel(); //面板 frame.add(panel); placeComponents(panel, frame); windowsClick(frame); } private static void placeComponents(final JPanel panel, final JFrame frame) { panel.setLayout(null); JLabel userLabel = new JLabel("欢迎使用****系统"); //面板上面的字 userLabel.setFont(new java.awt.Font("宋体", 0, 20)); userLabel.setBounds(100, 10, 200, 40); panel.add(userLabel); final JButton button = new JButton("启 动"); //按钮 button.setFont(new java.awt.Font("宋体", 0, 15)); //设置字体 是否加粗 大小 button.setBorderPainted(false); button.setBorder(BorderFactory.createRaisedBevelBorder()); button.setBounds(290, 120, 90, 25); //设置相对页面位置 宽度 高度 button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { //添加按钮监听事件 XunLingApplication cRead = new XunLingApplication(); int i = cRead.startComPort(); if (i == 1) { // 启动线程来处理收到的数据 cRead.start(); } button.setText("已启动"); button.setEnabled(false); } }); panel.add(button); JButton button2 = new JButton("系统设置"); button2.setBounds(1, 230, 150, 30); button2.setBorderPainted(false); button2.setFont(new java.awt.Font("宋体", 0, 13)); button2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { newSystemView(); frame.dispose(); } }); panel.add(button2); } private static void newSystemView() { final JFrame frame = new JFrame("*****系统设置"); frame.setSize(400, 300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationRelativeTo(null); JPanel panel = new JPanel(); frame.add(panel); panel.setLayout(null); JLabel userLabel = new JLabel("系统设置"); userLabel.setFont(new java.awt.Font("宋体", 0, 20)); userLabel.setBounds(10, 10, 200, 40); panel.add(userLabel); JButton button2 = new JButton("返回"); button2.setBounds(330, 15, 60, 20); button2.setBorderPainted(false); button2.setFont(new java.awt.Font("宋体", 0, 13)); panel.add(button2); button2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { startView(); frame.dispose(); } }); JLabel userLabel2 = new JLabel("***地址:"); userLabel2.setFont(new java.awt.Font("宋体", 0, 15)); userLabel2.setBounds(10, 100, 140, 30); panel.add(userLabel2); JTextField jtext1 = new JTextField(""); jtext1.setBounds(120, 108, 200, 20); jtext1.setFont(new java.awt.Font("宋体", 0, 20)); panel.add(jtext1); JButton button = new JButton("保存"); button.setBounds(245, 140, 70, 20); button.setBorderPainted(false); button.setFont(new java.awt.Font("宋体", 0, 13)); panel.add(button); windowsClick(frame); } private static void windowsClick(final JFrame frame) { frame.setVisible(true); // 使窗口可见 frame.setResizable(false); //使窗口大小不能调整 frame.addWindowListener(new WindowAdapter() { // 窗口关闭事件 public void windowClosing(WindowEvent e) { System.exit(0); } public void windowIconified(WindowEvent e) { // 窗口最小化事件 frame.setVisible(false); //隐藏窗口 new TestJFrame().miniTray(frame); } }); } private void miniTray(final JFrame frame) { // 窗口最小化到任务栏托盘 URL url = TestJFrame.class.getClassLoader().getResource("timg.jpg"); ImageIcon trayImg = null; if (url == null) { url = TestJFrame.class.getClassLoader().getResource("resources/timg.jpg"); if (url == null) { trayImg = new ImageIcon(" "); } else { trayImg = new ImageIcon(url); } } else { trayImg = new ImageIcon(url); // 托盘图标 } trayIcon = new TrayIcon(trayImg.getImage(), "****系统", new PopupMenu()); trayIcon.setImageAutoSize(true); trayIcon.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { if (e.getClickCount() == 2) { // 单击 1 双击 2 tray.remove(trayIcon); frame.setVisible(true); frame.setExtendedState(JFrame.NORMAL); frame.toFront(); } } }); try { tray.add(trayIcon); } catch (AWTException e1) { e1.printStackTrace(); } } }
初入Swing全面详细解析备注
猜你喜欢
转载自blog.csdn.net/samhuangliang/article/details/79878214
今日推荐
周排行