前言
雖然現(xiàn)在已經(jīng)很少項(xiàng)目會(huì)涉及GUI技術(shù),但作為一個(gè)合格的Java開(kāi)發(fā)工程師,還是得了解才得
本文記錄,idea使用JFormDesigner插件進(jìn)行Java GUI 桌面應(yīng)用開(kāi)發(fā)
GUI Swing
圖形化的用戶界面(Graphical User Interface,簡(jiǎn)稱GUI),java提供了一套可以輕松構(gòu)建GUI的工具
GUI開(kāi)發(fā)包:
java.awt 包: 主要提供字體/布局管理器
javax.Swing 包:主要提供各種組件(窗口/按鈕/文本框),商業(yè)開(kāi)發(fā)常用
java.awt.event 包:事件處理,后臺(tái)功能的實(shí)現(xiàn)
Swing組件
相對(duì)于AWT而言Swing包中提供了更多的豐富的、快捷的、強(qiáng)大的GUI組件
大體上包括以下內(nèi)容:window頂層容器(窗口)、container中間容器(面板)、component基本組件(按鈕、標(biāo)簽等)
JFormDesigner
JFormDesigner,高級(jí)Swing GUI設(shè)計(jì)器,支持MigLayout, JGoodies FormLayout, GroupLayout(自由設(shè)計(jì)),TableLayout和GridBagLayout,這使得它很容易創(chuàng)建專(zhuān)業(yè)外觀的表單。
通俗的講就是使用這個(gè)插件進(jìn)行拖拉布局,快速創(chuàng)建頁(yè)面,但是這個(gè)插件需要購(gòu)買(mǎi)許可才能使用
破解教程:https://www.cnblogs.com/ylkh/p/13858433.html
1、打開(kāi)注冊(cè)機(jī)JFormDesigner_Keygen.exe,選擇idea plug-in,里面的可以隨便填2、點(diǎn)擊Patch,選擇已安裝的插件,生成bak文件(插件位置idea安裝路徑pluginsFormDesigner)3、點(diǎn)擊generate生成JFormDesigner_license.txt文件
代碼編寫(xiě)
創(chuàng)建圖形頁(yè)面,插件會(huì)自動(dòng)同步生成java文件
進(jìn)行拖拉布局
關(guān)鍵點(diǎn):
1、頂層容器(JFrame)的Name值要為:this
2、生成的java文件要繼承 javax.swing.JFrame
public class TestFrame extends javax.swing.JFrame{ //省略其他代碼... }
3、需要進(jìn)行單選的RadioButton,要添加同一個(gè)Button Group
4、defaultCloseOperation要設(shè)置成EXIT,點(diǎn)擊X號(hào)退出頁(yè)面時(shí)才會(huì)退出程序
5、綁定事件,給按鈕添加一個(gè)actionPerformed即可
6、關(guān)閉當(dāng)前頁(yè)面:this.dispose(); //退出當(dāng)前界面
7、代碼彈出對(duì)話框:JOptionPane.showMessageDialog(null, "恭喜哦,登錄成功!");// Message 對(duì)話框
生成的java文件
/* * Created by JFormDesigner on Tue Dec 28 15:24:42 CST 2021 */package cn.huanzi.qch.view;import java.awt.*;import java.awt.event.*;import javax.swing.*;/** * 測(cè)試 */public class TestFrame extends javax.swing.JFrame{ public TestFrame() { initComponents(); setVisible(true);// 顯示 setLocationRelativeTo(null);// JFrame 窗口居中顯示 } public static void main(String[] args) { java.awt.EventQueue.invokeLater(() -> { new TestFrame(); System.out.println("啟動(dòng)成功!"); }); } private void SubmitActionPerformed(ActionEvent e) { // TODO add your code here System.out.println("---------------------------"); System.out.println("姓名:" userNameTestField.getText()); String sex = ""; if (xyRadioButton.isSelected()) { sex = "男"; } else if (xxRadioButton.isSelected()) { sex = "女"; }else if (yyRadioButton.isSelected()) { sex = "不確定"; } System.out.println("性別:" sex); String hobby = ""; if (singCheckBox.isSelected()) { hobby = "唱、"; } if (skipCheckBox.isSelected()) { hobby = "跳、"; } if (rapCheckBox.isSelected()) { hobby = "rap、"; } System.out.println("愛(ài)好:" hobby); System.out.println("自我評(píng)價(jià):" selfTextArea.getText()); } private void initComponents() { // JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents label1 = new JLabel(); userNameTestField = new JTextField(); label2 = new JLabel(); label3 = new JLabel(); submit = new JButton(); reset = new JButton(); xxRadioButton = new JRadioButton(); xyRadioButton = new JRadioButton(); yyRadioButton = new JRadioButton(); scrollPane1 = new JScrollPane(); selfTextArea = new JTextArea(); label4 = new JLabel(); label5 = new JLabel(); singCheckBox = new JCheckBox(); skipCheckBox = new JCheckBox(); rapCheckBox = new JCheckBox(); //======== this ======== setBackground(Color.gray); setTitle("Test GUI"); setForeground(SystemColor.windowText); setMinimumSize(new Dimension(300, 200)); setResizable(false); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); Container contentPane = getContentPane(); contentPane.setLayout(null); //---- label1 ---- label1.setText("u59d3u540duff1a"); contentPane.add(label1); label1.setBounds(34, 55, 65, 30); contentPane.add(userNameTestField); userNameTestField.setBounds(119, 55, 200, userNameTestField.getPreferredSize().height); //---- label2 ---- label2.setText("u6027u522buff1a"); contentPane.add(label2); label2.setBounds(34, 95, 65, 30); //---- label3 ---- label3.setText("u81eau6211u8bc4u4ef7uff1a"); contentPane.add(label3); label3.setBounds(34, 165, 65, 30); //---- submit ---- submit.setText("u63d0u4ea4"); submit.addActionListener(e -> SubmitActionPerformed(e)); contentPane.add(submit); submit.setBounds(new Rectangle(new Point(64, 271), submit.getPreferredSize())); //---- reset ---- reset.setText("u91cdu7f6e"); contentPane.add(reset); reset.setBounds(new Rectangle(new Point(219, 271), reset.getPreferredSize())); //---- xxRadioButton ---- xxRadioButton.setText("u5973"); contentPane.add(xxRadioButton); xxRadioButton.setBounds(new Rectangle(new Point(184, 100), xxRadioButton.getPreferredSize())); //---- xyRadioButton ---- xyRadioButton.setText("u7537"); contentPane.add(xyRadioButton); xyRadioButton.setBounds(new Rectangle(new Point(129, 100), xyRadioButton.getPreferredSize())); //---- yyRadioButton ---- yyRadioButton.setText("u4e0du786eu5b9a"); contentPane.add(yyRadioButton); yyRadioButton.setBounds(new Rectangle(new Point(239, 100), yyRadioButton.getPreferredSize())); //======== scrollPane1 ======== { scrollPane1.setViewportView(selfTextArea); } contentPane.add(scrollPane1); scrollPane1.setBounds(117, 165, 202, 71); //---- label4 ---- label4.setText("u6d4bu8bd5u8868u5355"); label4.setFont(label4.getFont().deriveFont(22f)); contentPane.add(label4); label4.setBounds(124, 0, 100, 45); //---- label5 ---- label5.setText("u7231u597duff1a"); contentPane.add(label5); label5.setBounds(34, 130, 65, 30); //---- singCheckBox ---- singCheckBox.setText("u5531"); contentPane.add(singCheckBox); singCheckBox.setBounds(new Rectangle(new Point(129, 135), singCheckBox.getPreferredSize())); //---- skipCheckBox ---- skipCheckBox.setText("u8df3"); contentPane.add(skipCheckBox); skipCheckBox.setBounds(new Rectangle(new Point(184, 135), skipCheckBox.getPreferredSize())); //---- rapCheckBox ---- rapCheckBox.setText("rap"); contentPane.add(rapCheckBox); rapCheckBox.setBounds(239, 135, 50, rapCheckBox.getPreferredSize().height); { // compute preferred size Dimension preferredSize = new Dimension(); for(int i = 0; i < contentPane.getComponentCount(); i ) { Rectangle bounds = contentPane.getComponent(i).getBounds(); preferredSize.width = Math.max(bounds.x bounds.width, preferredSize.width); preferredSize.height = Math.max(bounds.y bounds.height, preferredSize.height); } Insets insets = contentPane.getInsets(); preferredSize.width = insets.right; preferredSize.height = insets.bottom; contentPane.setMinimumSize(preferredSize); contentPane.setPreferredSize(preferredSize); } setSize(400, 365); setLocationRelativeTo(null); //---- buttonGroup2 ---- ButtonGroup buttonGroup2 = new ButtonGroup(); buttonGroup2.add(xxRadioButton); buttonGroup2.add(xyRadioButton); buttonGroup2.add(yyRadioButton); // JFormDesigner - End of component initialization //GEN-END:initComponents } // JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables private JLabel label1; private JTextField userNameTestField; private JLabel label2; private JLabel label3; private JButton submit; private JButton reset; private JRadioButton xxRadioButton; private JRadioButton xyRadioButton; private JRadioButton yyRadioButton; private JScrollPane scrollPane1; private JTextArea selfTextArea; private JLabel label4; private JLabel label5; private JCheckBox singCheckBox; private JCheckBox skipCheckBox; private JCheckBox rapCheckBox; // JFormDesigner - End of variables declaration //GEN-END:variables}
效果演示
后記
掌握了基本操作后,下面分享我在大學(xué)時(shí)期做的單機(jī)版五子棋、以及java課程實(shí)訓(xùn)作品:圖書(shū)管理系統(tǒng)
單機(jī)版五子棋
猛戳:Java GUI 單機(jī)版五子棋
圖書(shū)管理系統(tǒng)
數(shù)據(jù)庫(kù)用mysql,GUI圖形化頁(yè)面實(shí)現(xiàn)用戶登錄后對(duì)圖書(shū)進(jìn)行CRUD操作
登錄頁(yè)面
圖書(shū)管理
圖書(shū)類(lèi)別管理
關(guān)于我們
版權(quán)聲明
作者:huanzi-qch
出處:https://www.cnblogs.com/huanzi-qch
若標(biāo)題中有“轉(zhuǎn)載”字樣,則本文版權(quán)歸原作者所有。若無(wú)轉(zhuǎn)載字樣,本文版權(quán)歸作者所有,歡迎轉(zhuǎn)載,但未經(jīng)作者同意必須保留此段聲明,且在文章頁(yè)面明顯位置給出原文鏈接,否則保留追究法律責(zé)任的權(quán)利.
版權(quán)聲明:本文內(nèi)容由互聯(lián)網(wǎng)用戶自發(fā)貢獻(xiàn),該文觀點(diǎn)僅代表作者本人。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如發(fā)現(xiàn)本站有涉嫌抄襲侵權(quán)/違法違規(guī)的內(nèi)容, 請(qǐng)發(fā)送郵件至 舉報(bào),一經(jīng)查實(shí),本站將立刻刪除。