你可能不知道的Java GUI 桌面應用開發

前言

雖然現在已經很少項目會涉及GUI技術,但作為一個合格的Java開發工程師,還是得了解才得

本文記錄,idea使用JFormDesigner插件進行Java GUI 桌面應用開發

GUI Swing

圖形化的用戶界面(Graphical User Interface,簡稱GUI),java提供了一套可以輕松構建GUI的工具

GUI開發包:

java.awt 包: 主要提供字體/布局管理器

javax.swing 包:主要提供各種組件(窗口/按鈕/文本框),商業開發常用

java.awt.event 包:事件處理,后臺功能的實現

Swing組件

相對于AWT而言Swing包中提供了更多的豐富的、快捷的、強大的GUI組件

大體上包括以下內容:window頂層容器(窗口)、container中間容器(面板)、component基本組件(按鈕、標簽等)

JFormDesigner

JFormDesigner,高級Swing GUI設計器,支持MigLayout, JGoodies FormLayout, GroupLayout(自由設計),TableLayout和GridBagLayout,這使得它很容易創建專業外觀的表單。

通俗的講就是使用這個插件進行拖拉布局,快速創建頁面,但是這個插件需要購買許可才能使用

破解教程:https://www.cnblogs.com/ylkh/p/13858433.html

1、打開注冊機JFormDesigner_Keygen.exe,選擇idea plug-in,里面的可以隨便填2、點擊Patch,選擇已安裝的插件,生成bak文件(插件位置idea安裝路徑\plugins\FormDesigner\)3、點擊generate生成JFormDesigner_license.txt文件

代碼編寫

創建圖形頁面,插件會自動同步生成java文件

進行拖拉布局

關鍵點:

1、頂層容器(JFrame)的Name值要為:this

2、生成的java文件要繼承 javax.swing.JFrame

publicclassTestFrameextendsjavax.swing.JFrame{//省略其他代碼...? ? ? }

3、需要進行單選的RadioButton,要添加同一個Button Group

4、defaultCloseOperation要設置成EXIT,點擊X號退出頁面時才會退出程序

5、綁定事件,給按鈕添加一個actionPerformed即可

6、關閉當前頁面:this.dispose(); //退出當前界面

7、代碼彈出對話框:JOptionPane.showMessageDialog(null, "恭喜哦,登錄成功!");// Message 對話框

生成的java文件

/*

* Created by JFormDesigner on Tue Dec 28 15:24:42 CST 2021

*/package cn.huanzi.qch.view;importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;/**

* 測試

*/publicclassTestFrameextendsjavax.swing.JFrame{public TestFrame() {? ? ? ? initComponents();? ? ? ? setVisible(true);// 顯示

? ? ? ? setLocationRelativeTo(null);//JFrame 窗口居中顯示? ? }? ? public staticvoidmain(String[] args) {? ? ? ? java.awt.EventQueue.invokeLater(() -> {newTestFrame();? ? ? ? ? ? System.out.println("啟動成功!");? ? ? ? });? ? }privatevoidSubmitActionPerformed(ActionEvent e){? ? ? ? //TODOaddyourcodehereSystem.out.println("---------------------------");System.out.println("姓名:"+userNameTestField.getText());Stringsex= "";if(xyRadioButton.isSelected()){sex= "男";? ? ? ? }elseif(xxRadioButton.isSelected()){sex= "女";? ? ? ? }elseif(yyRadioButton.isSelected()){sex= "不確定";? ? ? ? }System.out.println("性別:"+sex);Stringhobby= "";if(singCheckBox.isSelected()){hobby+= "唱、";? ? ? ? }if(skipCheckBox.isSelected()){hobby+= "跳、";? ? ? ? }if(rapCheckBox.isSelected()){hobby+= "rap、";? ? ? ? }System.out.println("愛好:"+hobby);System.out.println("自我評價:"+selfTextArea.getText());? ? }privatevoidinitComponents(){? ? ? ? //JFormDesigner-Componentinitialization-DONOTMODIFY//GEN-BEGIN:initComponentslabel1=newJLabel();userNameTestField=newJTextField();label2=newJLabel();label3=newJLabel();submit=newJButton();reset=newJButton();xxRadioButton=newJRadioButton();xyRadioButton=newJRadioButton();yyRadioButton=newJRadioButton();scrollPane1=newJScrollPane();selfTextArea=newJTextArea();label4=newJLabel();label5=newJLabel();singCheckBox=newJCheckBox();skipCheckBox=newJCheckBox();rapCheckBox=newJCheckBox();? ? ? ? //========this========setBackground(Color.gray);setTitle("Test GUI");setForeground(SystemColor.windowText);setMinimumSize(newDimension(300,200));setResizable(false);setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);ContainercontentPane=getContentPane();contentPane.setLayout(null);? ? ? ? //----label1----label1.setText("\u59d3\u540d\uff1a");contentPane.add(label1);label1.setBounds(34,55,65,30);contentPane.add(userNameTestField);userNameTestField.setBounds(119,55,200, userNameTestField.getPreferredSize().height);? ? ? ? //----label2----label2.setText("\u6027\u522b\uff1a");contentPane.add(label2);label2.setBounds(34,95,65,30);? ? ? ? //----label3----label3.setText("\u81ea\u6211\u8bc4\u4ef7\uff1a");contentPane.add(label3);label3.setBounds(34,165,65,30);? ? ? ? //----submit----submit.setText("\u63d0\u4ea4");submit.addActionListener(e -> SubmitActionPerformed(e));contentPane.add(submit);submit.setBounds(newRectangle(newPoint(64,271), submit.getPreferredSize()));? ? ? ? //----reset----reset.setText("\u91cd\u7f6e");contentPane.add(reset);reset.setBounds(newRectangle(newPoint(219,271), reset.getPreferredSize()));? ? ? ? //----xxRadioButton----xxRadioButton.setText("\u5973");contentPane.add(xxRadioButton);xxRadioButton.setBounds(newRectangle(newPoint(184,100), xxRadioButton.getPreferredSize()));? ? ? ? //----xyRadioButton----xyRadioButton.setText("\u7537");contentPane.add(xyRadioButton);xyRadioButton.setBounds(newRectangle(newPoint(129,100), xyRadioButton.getPreferredSize()));? ? ? ? //----yyRadioButton----yyRadioButton.setText("\u4e0d\u786e\u5b9a");contentPane.add(yyRadioButton);yyRadioButton.setBounds(newRectangle(newPoint(239,100), yyRadioButton.getPreferredSize()));? ? ? ? //========scrollPane1========? ? ? ? {scrollPane1.setViewportView(selfTextArea);? ? ? ? }contentPane.add(scrollPane1);scrollPane1.setBounds(117,165,202,71);? ? ? ? //----label4----label4.setText("\u6d4b\u8bd5\u8868\u5355");label4.setFont(label4.getFont().deriveFont(22f));contentPane.add(label4);label4.setBounds(124,0,100,45);? ? ? ? //----label5----label5.setText("\u7231\u597d\uff1a");contentPane.add(label5);label5.setBounds(34,130,65,30);? ? ? ? //----singCheckBox----singCheckBox.setText("\u5531");contentPane.add(singCheckBox);singCheckBox.setBounds(newRectangle(newPoint(129,135), singCheckBox.getPreferredSize()));? ? ? ? //----skipCheckBox----skipCheckBox.setText("\u8df3");contentPane.add(skipCheckBox);skipCheckBox.setBounds(newRectangle(newPoint(184,135), skipCheckBox.getPreferredSize()));? ? ? ? //----rapCheckBox----rapCheckBox.setText("rap");contentPane.add(rapCheckBox);rapCheckBox.setBounds(239,135,50, rapCheckBox.getPreferredSize().height);? ? ? ? {? ? ? ? ? ? //computepreferredsizeDimensionpreferredSize=newDimension();for(int i =0; i < contentPane.getComponentCount(); i++){Rectanglebounds=contentPane.getComponent(i).getBounds();preferredSize.width=Math.max(bounds.x + bounds.width, preferredSize.width);preferredSize.height=Math.max(bounds.y + bounds.height, preferredSize.height);? ? ? ? ? ? }Insetsinsets=contentPane.getInsets();preferredSize.width+=insets.right;preferredSize.height+=insets.bottom;contentPane.setMinimumSize(preferredSize);contentPane.setPreferredSize(preferredSize);? ? ? ? }setSize(400,365);setLocationRelativeTo(null);? ? ? ? //----buttonGroup2----ButtonGroupbuttonGroup2=newButtonGroup();buttonGroup2.add(xxRadioButton);buttonGroup2.add(xyRadioButton);buttonGroup2.add(yyRadioButton);? ? ? ? //JFormDesigner-Endofcomponentinitialization//GEN-END:initComponents}? ? //JFormDesigner-Variablesdeclaration-DONOTMODIFY//GEN-BEGIN:variablesprivateJLabellabel1;privateJTextFielduserNameTestField;privateJLabellabel2;privateJLabellabel3;privateJButtonsubmit;privateJButtonreset;privateJRadioButtonxxRadioButton;privateJRadioButtonxyRadioButton;privateJRadioButtonyyRadioButton;privateJScrollPanescrollPane1;privateJTextAreaselfTextArea;privateJLabellabel4;privateJLabellabel5;privateJCheckBoxsingCheckBox;privateJCheckBoxskipCheckBox;privateJCheckBoxrapCheckBox;? ? //JFormDesigner-Endofvariablesdeclaration//GEN-END:variables}

效果演示

后記

掌握了基本操作后,下面分享我在大學時期做的課程實訓作品:圖書管理系統

圖書管理系統

數據庫用mysql,GUI圖形化頁面實現用戶登錄后對圖書進行CRUD操作

登錄頁面

圖書管理

圖書類別管理

關于我們

資料獲取方式 https://docs.qq.com/doc/DQVZmeHpBdWJBeXpi
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 227,818評論 6 531
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,185評論 3 414
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事?!?“怎么了?”我有些...
    開封第一講書人閱讀 175,656評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,647評論 1 309
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,446評論 6 405
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 54,951評論 1 321
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,041評論 3 440
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,189評論 0 287
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,718評論 1 333
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,602評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,800評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,316評論 5 358
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,045評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,419評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,671評論 1 281
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,420評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,755評論 2 371

推薦閱讀更多精彩內容