赞
踩
1、Java使用JFrame开发出漂亮的桌面程序附源码下载
源码下载:https://download.csdn.net/download/nosprings/12048149
Java使用JFrame开发出漂亮的桌面程序附源码下载
- /**
- * <p>Title: MainFrameTest.java</p>
- * <p>Description: </p>
- * <p>Copyright: Copyright (c) 2019</p>
- * <p>Company: www.nosprings.com</p>
- * @author liuhaibing
- * @date 2019年3月22日
- * @version 1.0
- */
- package com.jdesktop;
-
- import java.awt.BorderLayout;
- import java.awt.Color;
- import java.awt.EventQueue;
- import java.awt.Graphics;
- import java.awt.Image;
-
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.border.EmptyBorder;
- import javax.swing.JMenuBar;
- import javax.swing.JMenu;
- import javax.swing.JMenuItem;
- import javax.swing.JToolBar;
- import javax.swing.SwingUtilities;
- import javax.swing.JSplitPane;
- import javax.swing.JButton;
- import javax.swing.JDialog;
- import javax.imageio.ImageIO;
- import javax.swing.ImageIcon;
-
- import java.awt.event.ActionListener;
- import java.awt.event.ComponentEvent;
- import java.awt.event.ComponentListener;
- import java.awt.image.RenderedImage;
- import java.io.File;
- import java.io.IOException;
- import java.util.Iterator;
- import java.util.Random;
- import java.awt.event.ActionEvent;
- import javax.swing.JSeparator;
- import javax.swing.JPopupMenu;
- import javax.swing.JScrollPane;
- import java.awt.Component;
- import java.awt.FlowLayout;
- import javax.swing.JTextArea;
- import javax.swing.JTextPane;
- import javax.swing.JSlider;
- import javax.swing.JComboBox;
- import javax.swing.JLabel;
- import javax.swing.event.ChangeListener;
- import javax.swing.text.BadLocationException;
- import javax.swing.text.Style;
- import javax.swing.text.StyleConstants;
- import javax.swing.text.StyledDocument;
-
- import org.apache.log4j.Logger;
-
- import com.jdesktop.desktopsnapshot.DeskTopSnapshot;
- import com.jdesktop.messages.Message;
- import com.jdesktop.tcpclient.TcpImageReceiver;
- import com.jdesktop.tcpserver.TCPServer;
- import com.jdesktop.udpreceiver.UDPReceiver;
- import com.jdesktop.udpsender.UDPSender;
-
- import javax.swing.event.ChangeEvent;
-
- /**
- * @author liuhaibing
- * @date 2019年3月22日
- * @version 1.0
- */
- public class JDesktopUI extends JFrame {
-
- private static Logger log = Logger.getLogger(JDesktopUI.class.getClass());
-
- private static JDesktopUI frame; // 主界面
- private static JSplitPane splitPane; // 左右分割窗口
- private static JSplitPane rightSplitPanel; // 右侧上下分割窗口
- private JPanel contentPane;
- private JComboBox roomList;
- private JTextArea recMsg;
- private JTextArea sendMsg;
- private JLabel desktopLable;
-
- private JButton startBroadCast;
-
- // 通过当前的类得到classpath
- private static String basePath = JDesktopUI.class.getResource("/").getPath();
- private static ImageIcon startIcon = new ImageIcon(basePath + "icons/shape_square.png");
- private static ImageIcon icon2 = new ImageIcon(basePath + "icons/application_home.png");
- private static ImageIcon icon3 = new ImageIcon(basePath + "icons/photo.png");
- private static ImageIcon icon4 = new ImageIcon(basePath + "icons/application_osx_terminal.png");
- private static ImageIcon icon5 = new ImageIcon(basePath + "icons/folder_go.png");
- private static ImageIcon icon6 = new ImageIcon(basePath + "icons/emoticon_smile.png");
-
- // 通讯工作相关
- private static int defaultUDPClientPort = 8055;
- private static int defaultUDPServerPort = 8066;
-
- // UDP消息发送
- private UDPSender udpSender = new UDPSender(defaultUDPClientPort); // 本地端口是8055
- private UDPReceiver udpServer;
- private String broadcastAddress = "255.255.255.255"; // 广播地址
-
- // TCP消息发送
- private TCPServer tcpServer;
- private TcpImageReceiver tcpReceiver;
-
- /**
- * Launch the application.
- */
- public static void main(String[] args) {
- frame = new JDesktopUI();
- frame.setVisible(true);
- splitPane.setDividerLocation(0.75);
- rightSplitPanel.setDividerLocation(0.60);
- frame.startUDPServer(defaultUDPServerPort);
- }
-
- public void startUDPServer(int port) {
- udpServer = new UDPReceiver(port);
- udpServer.start();
-
- // 启动线程,接受数据并刷新到接受窗口
- new Thread(new Runnable() {
- public void run() {
- while (true) {
- log.info("udp阻塞1");
- synchronized (udpServer.queue) {
- log.info("udp阻塞2");
- if(udpServer.queue.size() == 0) {
- try {
- // System.out.println("udp阻塞");
- log.info("udp阻塞3");
- udpServer.queue.wait();
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }else {
- Iterator<Message> it = udpServer.queue.iterator();
- while(it.hasNext()) {
- final Message message = it.next();
- it.remove();
- // 这个是UI编程的一个机制:子线程访问GUI线程的时候必须这么使用
- SwingUtilities.invokeLater(new Runnable() {
- public void run() {
- // 收到的消息从队列里取出来,整理格式
- String newMessage = "";
- newMessage += "【" + message.getIpAddress() + "】:";
- newMessage += message.getTime().toLocaleString() + "\n";
- newMessage += message.getContent() + "\n";
- // 然后刷新到接受窗口
- recMsg.append(newMessage);
- }
- });
- }
- }
- }
-
-
- }
- }
-
- }).start();
- }
-
- public void startTcpServer(int port) {
- tcpServer = new TCPServer(8088);
- tcpServer.start();
- DeskTopSnapshot deskTopSnapshot = new DeskTopSnapshot();
- deskTopSnapshot.start();
- }
-
- public void startTcpClient(String ip, int port) {
- tcpReceiver = new TcpImageReceiver("10.0.2.253", 8088);
- tcpReceiver.start();
-
- // 启动线程,接受数据并刷新到接受窗口
- new Thread(new Runnable() {
- public void run() {
- while (true) {
- synchronized (tcpReceiver.clientImageQueue) {
- if(tcpReceiver.clientImageQueue.size() == 0) {
- try {
- System.out.println("TCP阻塞");
- tcpReceiver.clientImageQueue.wait();
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }else {
-
- try {
- //System.out.println(tcpReceiver.clientImageQueue.size());
- final Image image = tcpReceiver.clientImageQueue.poll();
- //System.out.println(tcpReceiver.clientImageQueue.size());
- SwingUtilities.invokeLater(new Runnable() {
- public void run() {
- // System.out.println("刷新图片");
-
- ImageIcon imageIcon = new ImageIcon(image); // Icon由图片文件形成
- Image image = imageIcon.getImage(); // 但这个图片太大不适合做Icon
- // 为把它缩小点,先要取出这个Icon的image ,然后缩放到合适的大小
- Image smallImage = image.getScaledInstance(desktopLable.getWidth(),desktopLable.getHeight(),Image.SCALE_SMOOTH);
- // 再由修改后的Image来生成合适的Icon
- ImageIcon smallIcon = new ImageIcon(smallImage);
-
- desktopLable.setIcon(smallIcon);
-
- //desktopLable.repaint();
- //desktopLable.updateUI();
- //desktopLable.setVisible(true);
- frame.invalidate();
- }
- });
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- }
- }
-
- }).start();
- }
-
- /**
- * Create the frame.
- */
- public JDesktopUI() {
- setTitle("JDesktop共享软件");
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setBounds(100, 100, 850, 532);
-
- JMenuBar menuBar = new JMenuBar();
- setJMenuBar(menuBar);
-
- JMenu sysSet = new JMenu("设置");
- menuBar.add(sysSet);
-
- JMenuItem exit = new JMenuItem("退出");
- exit.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- System.exit(0);
- }
- });
- sysSet.add(exit);
-
- JMenu help = new JMenu("帮助");
- menuBar.add(help);
-
- JMenuItem helpItem = new JMenuItem("帮助");
- help.add(helpItem);
-
- JSeparator separator = new JSeparator();
- help.add(separator);
-
- JMenuItem about = new JMenuItem("关于");
- help.add(about);
- contentPane = new JPanel();
- contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
- contentPane.setLayout(new BorderLayout(0, 0));
- setContentPane(contentPane);
-
- JToolBar toolBar = new JToolBar();
- contentPane.add(toolBar, BorderLayout.NORTH);
-
- startBroadCast = new JButton();
- startBroadCast.setIcon(startIcon); // 给button添加图片
- startBroadCast.setText("开始广播");
-
- startBroadCast.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- frame.startTcpServer(8088);
- startBroadCast.setText("正在广播");
- }
- });
- toolBar.add(startBroadCast);
-
- JButton desktopDetect = new JButton();
- desktopDetect.setText("桌面监控");
- desktopDetect.setIcon(icon3); // 给button添加图片
- toolBar.add(desktopDetect);
-
- JButton game = new JButton();
- game.setText("小游戏");
- game.setIcon(icon4); // 给button添加图片
- toolBar.add(game);
-
- JButton fileShare = new JButton();
- fileShare.setText("文件共享");
- fileShare.setIcon(icon5); // 给button添加图片
- toolBar.add(fileShare);
-
- JLabel chooseRoom = new JLabel(" 选择教室: ");
- toolBar.add(chooseRoom);
-
- String[] schoolRoom = { "第一教室", "第二教室", "第三教室", "第四教室", "第五教室" };
- roomList = new JComboBox(schoolRoom);
- toolBar.add(roomList);
-
- JButton enterRoom = new JButton();
- enterRoom.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- frame.startTcpClient("127.0.0.1", 8088);;
- }
- });
- enterRoom.setIcon(icon2); // 给button添加图片
- enterRoom.setText("进入");
- toolBar.add(enterRoom);
-
- JLabel setViewClear = new JLabel(" 设置清晰度:");
- toolBar.add(setViewClear);
-
- JSlider slider = new JSlider();
- slider.addChangeListener(new ChangeListener() {
- public void stateChanged(ChangeEvent e) {
- }
- });
- toolBar.add(slider);
-
- splitPane = new JSplitPane();
- contentPane.add(splitPane, BorderLayout.CENTER);
-
- JPanel rightPanel = new JPanel();
- splitPane.setRightComponent(rightPanel);
- rightPanel.setLayout(new BorderLayout(0, 0));
-
- rightSplitPanel = new JSplitPane();
- rightSplitPanel.setOrientation(JSplitPane.VERTICAL_SPLIT);
- rightPanel.add(rightSplitPanel, BorderLayout.CENTER);
-
- JPanel topPanel = new JPanel();
- rightSplitPanel.setLeftComponent(topPanel);
- topPanel.setLayout(new BorderLayout(0, 0));
-
- recMsg = new JTextArea();
- recMsg.setAutoscrolls(true);
- recMsg.setLineWrap(true);
- recMsg.setWrapStyleWord(true);
- JScrollPane recScroll = new JScrollPane(recMsg);
- recScroll.setVerticalScrollBarPolicy(
- JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
- topPanel.add(recScroll, BorderLayout.CENTER);
-
- JPanel bottomPanel = new JPanel();
- rightSplitPanel.setRightComponent(bottomPanel);
- bottomPanel.setLayout(new BorderLayout(0, 0));
-
- JToolBar talkToolBar = new JToolBar();
- bottomPanel.add(talkToolBar, BorderLayout.NORTH);
-
- JButton btnNewButton_2 = new JButton();
- btnNewButton_2.setIcon(icon6);
- talkToolBar.add(btnNewButton_2);
-
- sendMsg = new JTextArea();
- sendMsg.setAutoscrolls(true);
- sendMsg.setLineWrap(true);
- sendMsg.setWrapStyleWord(true);
- JScrollPane sendScroll = new JScrollPane(sendMsg);
- sendScroll.setVerticalScrollBarPolicy(
- JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
- bottomPanel.add(sendScroll, BorderLayout.CENTER);
-
- /*
- * StyledDocument doc = (StyledDocument) sendMsg.getDocument();
- *
- * Style style = doc.addStyle("StyleName", null); StyleConstants.setIcon(style,
- * icon3);
- *
- * try { doc.insertString(doc.getLength(), "ignored text", style); } catch
- * (BadLocationException e1) { // TODO Auto-generated catch block
- * e1.printStackTrace(); }
- */
-
- JPanel panel = new JPanel();
- bottomPanel.add(panel, BorderLayout.SOUTH);
- panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
-
- JButton btnNewButton = new JButton("清除");
- panel.add(btnNewButton);
-
- JButton sendMessage = new JButton("发送");
- sendMessage.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- // 发送按钮回去发送文本框内的内容
- String message = sendMsg.getText();
- // 调用
- if (message != null && !message.equals("")) {
- // 调用sender 发送报文, 采用广播方式
- udpSender.sendMessage(message, broadcastAddress, defaultUDPServerPort);
- // 清空输入框
- sendMsg.setText("");
- }
- }
- });
- panel.add(sendMessage);
-
- //JPanel leftPanel = new JPanel();
- // splitPane.setLeftComponent(leftPanel);
-
- JPanel mainPanel = new JPanel();
- // leftPanel.add(mainPanel);
- mainPanel.setLayout(new BorderLayout(0, 0));
- splitPane.setLeftComponent(mainPanel);
-
- desktopLable = new JLabel();
- mainPanel.add(desktopLable, BorderLayout.CENTER);
-
- //
- this.addComponentListener(new ComponentListener() {
-
- public void componentResized(ComponentEvent e) {
- // TODO Auto-generated method stub
- splitPane.setDividerLocation(0.75);
- rightSplitPanel.setDividerLocation(0.60);
- }
-
- public void componentMoved(ComponentEvent e) {
- // TODO Auto-generated method stub
-
- }
-
- public void componentShown(ComponentEvent e) {
- // TODO Auto-generated method stub
-
- }
-
- public void componentHidden(ComponentEvent e) {
- // TODO Auto-generated method stub
-
- }
- });
- }
-
- private static void addPopup(Component component, final JPopupMenu popup) {
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。