当前位置:   article > 正文

Java入门基础:Java使用JFrame开发出漂亮的桌面程序附源码下载(谁说Java不可以开发出漂亮的界面)_一个简单的java桌面程序源码下载

一个简单的java桌面程序源码下载

1、Java使用JFrame开发出漂亮的桌面程序附源码下载

源码下载:https://download.csdn.net/download/nosprings/12048149

Java使用JFrame开发出漂亮的桌面程序附源码下载

  1. /**
  2. * <p>Title: MainFrameTest.java</p>
  3. * <p>Description: </p>
  4. * <p>Copyright: Copyright (c) 2019</p>
  5. * <p>Company: www.nosprings.com</p>
  6. * @author liuhaibing
  7. * @date 2019年3月22日
  8. * @version 1.0
  9. */
  10. package com.jdesktop;
  11. import java.awt.BorderLayout;
  12. import java.awt.Color;
  13. import java.awt.EventQueue;
  14. import java.awt.Graphics;
  15. import java.awt.Image;
  16. import javax.swing.JFrame;
  17. import javax.swing.JPanel;
  18. import javax.swing.border.EmptyBorder;
  19. import javax.swing.JMenuBar;
  20. import javax.swing.JMenu;
  21. import javax.swing.JMenuItem;
  22. import javax.swing.JToolBar;
  23. import javax.swing.SwingUtilities;
  24. import javax.swing.JSplitPane;
  25. import javax.swing.JButton;
  26. import javax.swing.JDialog;
  27. import javax.imageio.ImageIO;
  28. import javax.swing.ImageIcon;
  29. import java.awt.event.ActionListener;
  30. import java.awt.event.ComponentEvent;
  31. import java.awt.event.ComponentListener;
  32. import java.awt.image.RenderedImage;
  33. import java.io.File;
  34. import java.io.IOException;
  35. import java.util.Iterator;
  36. import java.util.Random;
  37. import java.awt.event.ActionEvent;
  38. import javax.swing.JSeparator;
  39. import javax.swing.JPopupMenu;
  40. import javax.swing.JScrollPane;
  41. import java.awt.Component;
  42. import java.awt.FlowLayout;
  43. import javax.swing.JTextArea;
  44. import javax.swing.JTextPane;
  45. import javax.swing.JSlider;
  46. import javax.swing.JComboBox;
  47. import javax.swing.JLabel;
  48. import javax.swing.event.ChangeListener;
  49. import javax.swing.text.BadLocationException;
  50. import javax.swing.text.Style;
  51. import javax.swing.text.StyleConstants;
  52. import javax.swing.text.StyledDocument;
  53. import org.apache.log4j.Logger;
  54. import com.jdesktop.desktopsnapshot.DeskTopSnapshot;
  55. import com.jdesktop.messages.Message;
  56. import com.jdesktop.tcpclient.TcpImageReceiver;
  57. import com.jdesktop.tcpserver.TCPServer;
  58. import com.jdesktop.udpreceiver.UDPReceiver;
  59. import com.jdesktop.udpsender.UDPSender;
  60. import javax.swing.event.ChangeEvent;
  61. /**
  62. * @author liuhaibing
  63. * @date 2019年3月22日
  64. * @version 1.0
  65. */
  66. public class JDesktopUI extends JFrame {
  67. private static Logger log = Logger.getLogger(JDesktopUI.class.getClass());
  68. private static JDesktopUI frame; // 主界面
  69. private static JSplitPane splitPane; // 左右分割窗口
  70. private static JSplitPane rightSplitPanel; // 右侧上下分割窗口
  71. private JPanel contentPane;
  72. private JComboBox roomList;
  73. private JTextArea recMsg;
  74. private JTextArea sendMsg;
  75. private JLabel desktopLable;
  76. private JButton startBroadCast;
  77. // 通过当前的类得到classpath
  78. private static String basePath = JDesktopUI.class.getResource("/").getPath();
  79. private static ImageIcon startIcon = new ImageIcon(basePath + "icons/shape_square.png");
  80. private static ImageIcon icon2 = new ImageIcon(basePath + "icons/application_home.png");
  81. private static ImageIcon icon3 = new ImageIcon(basePath + "icons/photo.png");
  82. private static ImageIcon icon4 = new ImageIcon(basePath + "icons/application_osx_terminal.png");
  83. private static ImageIcon icon5 = new ImageIcon(basePath + "icons/folder_go.png");
  84. private static ImageIcon icon6 = new ImageIcon(basePath + "icons/emoticon_smile.png");
  85. // 通讯工作相关
  86. private static int defaultUDPClientPort = 8055;
  87. private static int defaultUDPServerPort = 8066;
  88. // UDP消息发送
  89. private UDPSender udpSender = new UDPSender(defaultUDPClientPort); // 本地端口是8055
  90. private UDPReceiver udpServer;
  91. private String broadcastAddress = "255.255.255.255"; // 广播地址
  92. // TCP消息发送
  93. private TCPServer tcpServer;
  94. private TcpImageReceiver tcpReceiver;
  95. /**
  96. * Launch the application.
  97. */
  98. public static void main(String[] args) {
  99. frame = new JDesktopUI();
  100. frame.setVisible(true);
  101. splitPane.setDividerLocation(0.75);
  102. rightSplitPanel.setDividerLocation(0.60);
  103. frame.startUDPServer(defaultUDPServerPort);
  104. }
  105. public void startUDPServer(int port) {
  106. udpServer = new UDPReceiver(port);
  107. udpServer.start();
  108. // 启动线程,接受数据并刷新到接受窗口
  109. new Thread(new Runnable() {
  110. public void run() {
  111. while (true) {
  112. log.info("udp阻塞1");
  113. synchronized (udpServer.queue) {
  114. log.info("udp阻塞2");
  115. if(udpServer.queue.size() == 0) {
  116. try {
  117. // System.out.println("udp阻塞");
  118. log.info("udp阻塞3");
  119. udpServer.queue.wait();
  120. } catch (InterruptedException e) {
  121. // TODO Auto-generated catch block
  122. e.printStackTrace();
  123. }
  124. }else {
  125. Iterator<Message> it = udpServer.queue.iterator();
  126. while(it.hasNext()) {
  127. final Message message = it.next();
  128. it.remove();
  129. // 这个是UI编程的一个机制:子线程访问GUI线程的时候必须这么使用
  130. SwingUtilities.invokeLater(new Runnable() {
  131. public void run() {
  132. // 收到的消息从队列里取出来,整理格式
  133. String newMessage = "";
  134. newMessage += "【" + message.getIpAddress() + "】:";
  135. newMessage += message.getTime().toLocaleString() + "\n";
  136. newMessage += message.getContent() + "\n";
  137. // 然后刷新到接受窗口
  138. recMsg.append(newMessage);
  139. }
  140. });
  141. }
  142. }
  143. }
  144. }
  145. }
  146. }).start();
  147. }
  148. public void startTcpServer(int port) {
  149. tcpServer = new TCPServer(8088);
  150. tcpServer.start();
  151. DeskTopSnapshot deskTopSnapshot = new DeskTopSnapshot();
  152. deskTopSnapshot.start();
  153. }
  154. public void startTcpClient(String ip, int port) {
  155. tcpReceiver = new TcpImageReceiver("10.0.2.253", 8088);
  156. tcpReceiver.start();
  157. // 启动线程,接受数据并刷新到接受窗口
  158. new Thread(new Runnable() {
  159. public void run() {
  160. while (true) {
  161. synchronized (tcpReceiver.clientImageQueue) {
  162. if(tcpReceiver.clientImageQueue.size() == 0) {
  163. try {
  164. System.out.println("TCP阻塞");
  165. tcpReceiver.clientImageQueue.wait();
  166. } catch (InterruptedException e) {
  167. // TODO Auto-generated catch block
  168. e.printStackTrace();
  169. }
  170. }else {
  171. try {
  172. //System.out.println(tcpReceiver.clientImageQueue.size());
  173. final Image image = tcpReceiver.clientImageQueue.poll();
  174. //System.out.println(tcpReceiver.clientImageQueue.size());
  175. SwingUtilities.invokeLater(new Runnable() {
  176. public void run() {
  177. // System.out.println("刷新图片");
  178. ImageIcon imageIcon = new ImageIcon(image); // Icon由图片文件形成
  179. Image image = imageIcon.getImage(); // 但这个图片太大不适合做Icon
  180. // 为把它缩小点,先要取出这个Icon的image ,然后缩放到合适的大小
  181. Image smallImage = image.getScaledInstance(desktopLable.getWidth(),desktopLable.getHeight(),Image.SCALE_SMOOTH);
  182. // 再由修改后的Image来生成合适的Icon
  183. ImageIcon smallIcon = new ImageIcon(smallImage);
  184. desktopLable.setIcon(smallIcon);
  185. //desktopLable.repaint();
  186. //desktopLable.updateUI();
  187. //desktopLable.setVisible(true);
  188. frame.invalidate();
  189. }
  190. });
  191. } catch (Exception e) {
  192. // TODO Auto-generated catch block
  193. e.printStackTrace();
  194. }
  195. }
  196. }
  197. }
  198. }
  199. }).start();
  200. }
  201. /**
  202. * Create the frame.
  203. */
  204. public JDesktopUI() {
  205. setTitle("JDesktop共享软件");
  206. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  207. setBounds(100, 100, 850, 532);
  208. JMenuBar menuBar = new JMenuBar();
  209. setJMenuBar(menuBar);
  210. JMenu sysSet = new JMenu("设置");
  211. menuBar.add(sysSet);
  212. JMenuItem exit = new JMenuItem("退出");
  213. exit.addActionListener(new ActionListener() {
  214. public void actionPerformed(ActionEvent e) {
  215. System.exit(0);
  216. }
  217. });
  218. sysSet.add(exit);
  219. JMenu help = new JMenu("帮助");
  220. menuBar.add(help);
  221. JMenuItem helpItem = new JMenuItem("帮助");
  222. help.add(helpItem);
  223. JSeparator separator = new JSeparator();
  224. help.add(separator);
  225. JMenuItem about = new JMenuItem("关于");
  226. help.add(about);
  227. contentPane = new JPanel();
  228. contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  229. contentPane.setLayout(new BorderLayout(0, 0));
  230. setContentPane(contentPane);
  231. JToolBar toolBar = new JToolBar();
  232. contentPane.add(toolBar, BorderLayout.NORTH);
  233. startBroadCast = new JButton();
  234. startBroadCast.setIcon(startIcon); // 给button添加图片
  235. startBroadCast.setText("开始广播");
  236. startBroadCast.addActionListener(new ActionListener() {
  237. public void actionPerformed(ActionEvent e) {
  238. frame.startTcpServer(8088);
  239. startBroadCast.setText("正在广播");
  240. }
  241. });
  242. toolBar.add(startBroadCast);
  243. JButton desktopDetect = new JButton();
  244. desktopDetect.setText("桌面监控");
  245. desktopDetect.setIcon(icon3); // 给button添加图片
  246. toolBar.add(desktopDetect);
  247. JButton game = new JButton();
  248. game.setText("小游戏");
  249. game.setIcon(icon4); // 给button添加图片
  250. toolBar.add(game);
  251. JButton fileShare = new JButton();
  252. fileShare.setText("文件共享");
  253. fileShare.setIcon(icon5); // 给button添加图片
  254. toolBar.add(fileShare);
  255. JLabel chooseRoom = new JLabel(" 选择教室: ");
  256. toolBar.add(chooseRoom);
  257. String[] schoolRoom = { "第一教室", "第二教室", "第三教室", "第四教室", "第五教室" };
  258. roomList = new JComboBox(schoolRoom);
  259. toolBar.add(roomList);
  260. JButton enterRoom = new JButton();
  261. enterRoom.addActionListener(new ActionListener() {
  262. public void actionPerformed(ActionEvent e) {
  263. frame.startTcpClient("127.0.0.1", 8088);;
  264. }
  265. });
  266. enterRoom.setIcon(icon2); // 给button添加图片
  267. enterRoom.setText("进入");
  268. toolBar.add(enterRoom);
  269. JLabel setViewClear = new JLabel(" 设置清晰度:");
  270. toolBar.add(setViewClear);
  271. JSlider slider = new JSlider();
  272. slider.addChangeListener(new ChangeListener() {
  273. public void stateChanged(ChangeEvent e) {
  274. }
  275. });
  276. toolBar.add(slider);
  277. splitPane = new JSplitPane();
  278. contentPane.add(splitPane, BorderLayout.CENTER);
  279. JPanel rightPanel = new JPanel();
  280. splitPane.setRightComponent(rightPanel);
  281. rightPanel.setLayout(new BorderLayout(0, 0));
  282. rightSplitPanel = new JSplitPane();
  283. rightSplitPanel.setOrientation(JSplitPane.VERTICAL_SPLIT);
  284. rightPanel.add(rightSplitPanel, BorderLayout.CENTER);
  285. JPanel topPanel = new JPanel();
  286. rightSplitPanel.setLeftComponent(topPanel);
  287. topPanel.setLayout(new BorderLayout(0, 0));
  288. recMsg = new JTextArea();
  289. recMsg.setAutoscrolls(true);
  290. recMsg.setLineWrap(true);
  291. recMsg.setWrapStyleWord(true);
  292. JScrollPane recScroll = new JScrollPane(recMsg);
  293. recScroll.setVerticalScrollBarPolicy(
  294. JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
  295. topPanel.add(recScroll, BorderLayout.CENTER);
  296. JPanel bottomPanel = new JPanel();
  297. rightSplitPanel.setRightComponent(bottomPanel);
  298. bottomPanel.setLayout(new BorderLayout(0, 0));
  299. JToolBar talkToolBar = new JToolBar();
  300. bottomPanel.add(talkToolBar, BorderLayout.NORTH);
  301. JButton btnNewButton_2 = new JButton();
  302. btnNewButton_2.setIcon(icon6);
  303. talkToolBar.add(btnNewButton_2);
  304. sendMsg = new JTextArea();
  305. sendMsg.setAutoscrolls(true);
  306. sendMsg.setLineWrap(true);
  307. sendMsg.setWrapStyleWord(true);
  308. JScrollPane sendScroll = new JScrollPane(sendMsg);
  309. sendScroll.setVerticalScrollBarPolicy(
  310. JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
  311. bottomPanel.add(sendScroll, BorderLayout.CENTER);
  312. /*
  313. * StyledDocument doc = (StyledDocument) sendMsg.getDocument();
  314. *
  315. * Style style = doc.addStyle("StyleName", null); StyleConstants.setIcon(style,
  316. * icon3);
  317. *
  318. * try { doc.insertString(doc.getLength(), "ignored text", style); } catch
  319. * (BadLocationException e1) { // TODO Auto-generated catch block
  320. * e1.printStackTrace(); }
  321. */
  322. JPanel panel = new JPanel();
  323. bottomPanel.add(panel, BorderLayout.SOUTH);
  324. panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
  325. JButton btnNewButton = new JButton("清除");
  326. panel.add(btnNewButton);
  327. JButton sendMessage = new JButton("发送");
  328. sendMessage.addActionListener(new ActionListener() {
  329. public void actionPerformed(ActionEvent e) {
  330. // 发送按钮回去发送文本框内的内容
  331. String message = sendMsg.getText();
  332. // 调用
  333. if (message != null && !message.equals("")) {
  334. // 调用sender 发送报文, 采用广播方式
  335. udpSender.sendMessage(message, broadcastAddress, defaultUDPServerPort);
  336. // 清空输入框
  337. sendMsg.setText("");
  338. }
  339. }
  340. });
  341. panel.add(sendMessage);
  342. //JPanel leftPanel = new JPanel();
  343. // splitPane.setLeftComponent(leftPanel);
  344. JPanel mainPanel = new JPanel();
  345. // leftPanel.add(mainPanel);
  346. mainPanel.setLayout(new BorderLayout(0, 0));
  347. splitPane.setLeftComponent(mainPanel);
  348. desktopLable = new JLabel();
  349. mainPanel.add(desktopLable, BorderLayout.CENTER);
  350. //
  351. this.addComponentListener(new ComponentListener() {
  352. public void componentResized(ComponentEvent e) {
  353. // TODO Auto-generated method stub
  354. splitPane.setDividerLocation(0.75);
  355. rightSplitPanel.setDividerLocation(0.60);
  356. }
  357. public void componentMoved(ComponentEvent e) {
  358. // TODO Auto-generated method stub
  359. }
  360. public void componentShown(ComponentEvent e) {
  361. // TODO Auto-generated method stub
  362. }
  363. public void componentHidden(ComponentEvent e) {
  364. // TODO Auto-generated method stub
  365. }
  366. });
  367. }
  368. private static void addPopup(Component component, final JPopupMenu popup) {
  369. }
  370. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小惠珠哦/article/detail/844534
推荐阅读
相关标签
  

闽ICP备14008679号