柚子快報激活碼778899分享:開發(fā)語言 JAVA中的GUI
目錄
?一.GUI的概念
1.1基本概念
1.2GUI的特點
1.3Swing的概念
1.4GUI中的容器組件
二.常用容器
2.1JFrame
2.2JFrame中常用的方法
?2.3JPanel
三.GUI面板的布局
3.1流式布局
?3.2邊界布局
3.3網格布局
四.文本框
4.1JLabe
4.2JTextField
4.4 多行文本框(JTextArea)
?4.5按鈕
五.菜單
六.事件處理
?一.GUI的概念
1.1基本概念
GUI是Graphical User Interface(圖形用戶界面)的縮寫,指的是通過圖形方式顯示和操作的用戶界面。它提供了一種直觀、可視化的方式,讓用戶可以通過鼠標、鍵盤或觸摸等輸入設備與計算機進行交互。
GUI的設計目的是簡化用戶與計算機系統(tǒng)之間的交互過程,使用戶能夠更輕松、高效地完成各種任務。傳統(tǒng)的命令行界面(CLI)需要用戶記憶和輸入命令,而GUI則通過圖形元素如窗口、按鈕、菜單、對話框等來呈現(xiàn)信息和提供操作選項,使用戶可以通過直接點擊、拖拽、輸入等方式與計算機進行交互。
1.2GUI的特點
視覺化:GUI使用圖形元素和視覺效果來代表應用程序的功能、數(shù)據(jù)和操作,以便用戶能夠直觀地理解和操作。 交互性:GUI允許用戶通過鼠標、鍵盤或觸摸等輸入設備進行交互操作,包括點擊、拖拽、輸入等,使用戶能夠主動地與計算機進行溝通和反饋。 易用性:GUI設計注重用戶體驗,追求簡潔、易于理解和操作的界面,減少用戶的學習成本和操作困難。 多任務支持:GUI允許多個應用程序同時運行在屏幕上的不同窗口中,并提供了任務切換和管理的功能,方便用戶在不同應用程序之間進行切換和操作。 可視化編程:基于GUI的開發(fā)工具和框架使開發(fā)人員能夠使用可視化方式設計和構建應用程序界面,簡化開發(fā)流程。
1.3Swing的概念
Swing是純Java組件,使得應用程序在不同的平臺上運行時具有相同外觀和相同 的行為。
Swing中的大部分組件類位于javax.swing包中.
Swing中的組件非常豐富,支持很多功能強大的組件。
1.4GUI中的容器組件
Java的圖形用戶界面的基本組成部分是組件,組件是一個以圖形化的方式 顯示在屏幕上并能與用戶進行交互的對象; 組件不能獨立地顯示出來,必須將組件放在一定的容器(container)中才 可以顯示出來。
容器可以容納多個組件,通過調用容器的add(Component comp)方法 向容器中添加組件。
窗口(Frame)和面板(Panel)是最常用的兩個容器。
二.常用容器
2.1JFrame
JFrame是一個用于創(chuàng)建窗口和應用程序框架的類。它是javax.swing包中的一部分,提供了一些基本功能,如窗口管理、布局管理和事件處理等。
JFrame類代表了一個頂級窗口,可以包含其他GUI組件,如按鈕、文本框、標簽等。通過使用JFrame,可以創(chuàng)建具有標題欄、最大化、最小化、關閉按鈕等標準窗口功能的應用程序。
創(chuàng)建了一個繼承自JFrame的自定義類MyFrame。在構造函數(shù)中,設置了窗口的標題、大小、默認的關閉操作,并將窗口設置為可見。最后,在main方法中創(chuàng)建了MyFrame對象,即實例化了窗口并顯示出來。
除了基本的窗口和應用程序框架功能外,JFrame還提供了許多方法用于添加和管理其他GUI組件、設置窗口屬性和處理事件等。通過靈活運用JFrame以及其他Swing組件,可以構建出功能強大、交互友好的Java圖形界面應用程序。
下面是GUI窗口的簡單創(chuàng)建:
import javax.swing.*;
import java.awt.*;
public class MyJFrame extends JFrame {
public MyJFrame() throws HeadlessException {
this.setSize(500,500);//設置窗口大小
this.setLocationRelativeTo(null);//設置水平居中
this.setVisible(true);//啟動窗口
}
public static void main(String[] args) {
new MyJFrame();
}
}
代碼執(zhí)行的結果為:
2.2JFrame中常用的方法
setTitle(String title):設置窗口標題。 setSize(int width, int height):設置窗口的寬度和高度。 setLocation(int x, int y):設置窗口在屏幕上的位置。 setResizable(boolean resizable):設置窗口是否可調整大小。 setDefaultCloseOperation(int operation):設置窗口關閉時的操作,常用的操作有:
JFrame.EXIT_ON_CLOSE:退出程序。JFrame.HIDE_ON_CLOSE:隱藏窗口。JFrame.DISPOSE_ON_CLOSE:釋放窗口占用的資源。 setVisible(boolean visible):設置窗口是否可見。 getContentPane():獲取窗口的內容面板,用于添加和管理其他GUI組件。 add(Component comp):將指定的組件添加到窗口的內容面板中。 setLayout(LayoutManager manager):設置窗口的布局管理器,用于管理組件的位置和大小。 pack():自動調整窗口的大小,以適應其中包含的所有組件。 setDefaultCloseOperation(int operation):設置窗口關閉時的操作。
這些方法在實際中的運用:
public class MyJFrame extends JFrame {
public MyJFrame() throws HeadlessException {
this.setTitle("窗口標題");//設置窗口標題
this.setSize(500,500);//設置窗口大小
//this.setLocation(300,400);//設置窗口在界面的顯示位置
this.setLocationRelativeTo(null);//設置窗戶口顯示位置水平居中
this.setResizable(false);//設置之后禁止設置窗口大小
//this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);//DO_NOTHING_ON_CLOSE設置之后無法自己關閉界面
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//設置之后有了關閉選項
this.setVisible(true);//啟動窗口
}
public static void main(String[] args) {
new MyJFrame();
}
}
代碼實現(xiàn)結果為:
?2.3JPanel
JPanel是Java GUI編程中常用的容器類,它位于javax.swing包中,用于組織和管理其他GUI組件。JPanel可以看作是一個空白的面板,你可以將其他組件添加到其中,并對其進行布局、設置風格和添加事件監(jiān)聽等操作。
以下是JPanel中常用的方法:
add(Component comp):將指定的組件添加到面板中。 remove(Component comp):從面板中移除指定的組件。 setLayout(LayoutManager manager):設置面板的布局管理器,用于管理組件的位置和大小。 setBackground(Color color):設置面板的背景顏色。 setPreferredSize(Dimension size):設置面板的首選大小。 setBorder(Border border):設置面板的邊框。 setVisible(boolean visible):設置面板是否可見。 validate():重新驗證面板及其所有子組件的布局。 repaint():請求重繪面板及其所有子組件。 getComponent(int index):獲取面板指定位置索引處的組件。 getComponents():獲取面板中的所有組件。 getLayout():獲取面板的布局管理器。
JPanel提供了一種靈活的方式來組織和管理GUI界面中的組件。它可以嵌套在其他容器中,例如JFrame、JDialog等。通過使用多個JPanel,可以實現(xiàn)復雜的布局結構和層次化的容器組織。
三.GUI面板的布局
3.1流式布局
JPanel默認的就是流式布局。
流式布局的樣式為:
以下代碼是在流式布局中添加組件的代碼:
public class MyJFrame extends JFrame {
public MyJFrame() throws HeadlessException {
this.setTitle("窗口標題");//設置窗口標題
this.setSize(500,500);//設置窗口大小
//this.setLocation(300,400);//設置窗口在界面的顯示位置
this.setLocationRelativeTo(null);//設置窗戶口顯示位置水平居中
this.setResizable(false);//設置之后禁止設置窗口大小
//this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);//DO_NOTHING_ON_CLOSE設置之后無法自己關閉界面
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//設置之后有了關閉選項
//窗口添加面板
JPanel jPanel = new JPanel();//創(chuàng)建組件
jPanel.setBackground(new Color(185, 213, 165));//設置創(chuàng)建組建的背景顏色
JButton jButton = new JButton("登錄");//在組件上添加按鈕,默認添加水平居中
jButton.setLocation(10,10);//設置按鈕的距離邊距的大小
jButton.setBackground(Color.white);//設置按鈕顏色
jPanel.add(jButton);
this.add(jPanel);//功能設置完之后在添加到界面當中
this.setVisible(true);//啟動窗口
}
public static void main(String[] args) {
new MyJFrame();
}
}
?執(zhí)行結果為:
?3.2邊界布局
在創(chuàng)建面板的時候,就要選擇創(chuàng)鍵的面板:
JPanel jPanel = new JPanel(new BorderLayout());
此代碼執(zhí)行的結果就是創(chuàng)建了一個邊界布局的面板。
邊界布局的格式為:
在邊界布局中添加組件的代碼為:
public class Demo3Border extends JFrame {
public Demo3Border() throws HeadlessException {
this.setTitle("窗口標題");//設置窗口標題
this.setSize(700,500);//設置窗口大小
this.setLocation(200,200);//設置界面邊距大小
//this.setLocation(300,400);//設置窗口在界面的顯示位置
this.setLocationRelativeTo(null);//設置窗戶口顯示位置水平居中
this.setResizable(false);//設置之后禁止設置窗口大小
//邊界布局
JPanel jPanel = new JPanel(new BorderLayout());
JButton jb1 = new JButton("登錄1");//指定組件在邊界布局中的位置
jb1.setSize(100,100);
JButton jb2 = new JButton("登錄2");
JButton jb3 = new JButton("登錄3");
jPanel.add(jb1,BorderLayout.EAST);
jPanel.add(jb2,BorderLayout.NORTH);
jPanel.add(jb3,BorderLayout.SOUTH);
this.add(jPanel);
this.setVisible(true);
}
public static void main(String[] args) {
new Demo3Border();
}
}
執(zhí)行的結果如下:
?若是在添加時不設置顯示位置,就會默認將組件放在中間位置。
3.3網格布局
創(chuàng)建網格布局的代碼為;
JPanel jp = new JPanel(new GridLayout(2,2));
//注意:在創(chuàng)建網格布局的時候,要給定網格的布局(幾行幾列)
public class Demo4Grid extends JFrame {
public Demo4Grid() throws HeadlessException {
this.setTitle("窗口標題");//設置窗口標題
this.setSize(700,500);//設置窗口大小
this.setLocation(200,200);//設置界面邊距大小
//this.setLocation(300,400);//設置窗口在界面的顯示位置
this.setLocationRelativeTo(null);//設置窗戶口顯示位置水平居中
this.setResizable(false);//設置之后禁止設置窗口大小
//網格布局
JPanel jp = new JPanel(new GridLayout(2,2));
JButton jb1 = new JButton("登錄1");
JButton jb2 = new JButton("登錄2");
JButton jb3 = new JButton("登錄3");
JButton jb4 = new JButton("登錄4");
jp.add(jb1);
jp.add(jb2);
jp.add(jb3);
jp.add(jb4);
this.add(jp);
this.setVisible(true);
}
public static void main(String[] args) {
new Demo4Grid();
}
}
執(zhí)行的結果為:
四.文本框
?標簽(JLabel) 標簽是容納文本和圖標的控件,通常用來在界面中標識別的控件。
4.1JLabe
構造函數(shù): JLabel() 創(chuàng)建一個空的標簽
? ? ? ? ? ? ? ? ? ?JLabel(String text) 創(chuàng)建一個帶文本的標簽
? ? ? ? ? ? ? ? ? ?JLabel(Icon image) 創(chuàng)建一個帶圖像的標簽
方法: void setText(String text) 設置標簽上的文本?
? ? ? ? ? ? String getText() 獲得標簽上的文本
? ? ? ? ? ? setFont(new Font(“宋體”,Font.BOLD, 18)); 設置文本框字體
4.2JTextField
單行文本(JTextField)
JTextField的構造函數(shù):
JTextField() JTextField(String text )
JTextField(int columns)
JTextField(String text, int columns) 方法:
void setText(String text) 設置文本框中的文本
String getText() 獲得文本框中的文本
void setEditable(boolean b) 設置文本框是否可以編輯
setColumns(20); 設置列數(shù)
在代碼中的具體實現(xiàn):
public class Demo6Label extends JFrame {
public Demo6Label() throws HeadlessException {
this.setTitle("窗口標題");
this.setSize(500, 500);//大小
//this.setLocation(300, 500);//位置坐標
this.setLocationRelativeTo(null);//相對位置 水平垂直居中
this.setResizable(false);//禁止設置窗口大小
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//關閉窗口選項
//默認流式布局
JPanel jp = new JPanel();
JLabel jl = new JLabel("賬號");
jl.setForeground(Color.BLUE);//設置字體顏色
jl.setFont(new Font("楷體",Font.BOLD,20));
JTextField jt =new JTextField(20);//創(chuàng)建文本框
JButton jb = new JButton("登錄");
jp.add(jl);
jp.add(jt);
jp.add(jb);
this.add(jp);
this.setVisible(true);
}
public static void main(String[] args) {
new Demo6Label();
}
}
代碼實現(xiàn)的結果為:
4.4 多行文本框(JTextArea)
構造函數(shù):
JTextArea() 創(chuàng)建一個空的文本域
JTextArea(String text) 用指定文本初始化文本域
JTextArea(int rows, int columns) 創(chuàng)建一個指定行數(shù)和列數(shù)的空文本域
JTextArea(String text,int rows, int columns) 創(chuàng)建一個帶文本,并指行數(shù)和列數(shù)的
方法:
void setText(String text) 設置文本域中的文本
String getText() 獲得文本域中的文本
void setFont(Font font) 設置文本域中文本的字體
void setLineWrap(boolean wrap) //是否自動換行,默認false 如果需要文本區(qū)自動出現(xiàn)滾動條,可將文本區(qū)對象放入
滾動窗格(JScrollPane)中: JScrollPane scrollPane = new JScrollPane(txtArea);
add(scrollPane )
這些常用的方法在代碼中的具體實現(xiàn):
public class Demo7JTextArea extends JFrame {
public Demo7JTextArea() throws HeadlessException {
this.setTitle("文本框");//設置文本框標題
this.setSize(900,700);//是指GUI界面的大小
this.setLocationRelativeTo(null);//設置文本框水平居中
this.setResizable(false);//設置能否修改窗口大小
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//關閉窗口選項
JPanel jp = new JPanel();
//多行輸入文本框
JTextArea jTextArea = new JTextArea(10,20);
jTextArea.setLineWrap(true);//自動換行
jTextArea.setWrapStyleWord(true);//換行時必須是完整的字符換行
JScrollPane jScrollPane = new JScrollPane(jTextArea);//給此文本框加滾動條
jp.add(jScrollPane);
this.add(jp);
this.setVisible(true);
}
public static void main(String[] args) {
new Demo7JTextArea();
}
}
實現(xiàn)之后的結果:
?4.5按鈕
按鈕(JButton)
構造方法:
JButton(String text) 創(chuàng)建一個帶文本的標簽
JButton(Icon image) 創(chuàng)建一個帶圖像的標簽
方法:
void setBackground(Color bg) 設置按鈕的背景色
void setEnabled(boolean b) 設置啟用(或禁用)按鈕,由參數(shù)b決定
void setToolTipText(String text) 設置按鈕的懸停提示信息
五.菜單
菜單欄組件:
構造方法:
JMenuBar();
方法:
add(menu); 向菜單欄添加菜單
菜單組件:
構造方法:
JMenu(“菜單名稱");
方法:add(menuItem); 向菜單添加菜單選項
菜單項組件:
構造方法:
JMenuItem(“菜單項名稱");
將菜單欄添加到窗口 setJMenuBar(menuBar);
菜單加到面板的具體操作:
public class Demo8JMenuBar extends JFrame {
public Demo8JMenuBar() throws HeadlessException {
this.setTitle("文本框");//設置文本框標題
this.setSize(300,200);//是指GUI界面的大小
this.setLocationRelativeTo(null);//設置文本框水平居中
this.setResizable(false);//設置能否修改窗口大小
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//關閉窗口選項
//設置窗口菜單
//菜單分為三部分 菜單欄,菜單,菜單項
JPanel jPanel = new JPanel();
JMenuBar jMenuBar = new JMenuBar();//創(chuàng)建菜單欄
JMenu jMenu1 = new JMenu("選擇");//創(chuàng)建菜單
JMenuItem jMenuItem1 = new JMenuItem("保存");//創(chuàng)建菜單項
JMenuItem jMenuItem2 = new JMenuItem("刪除");
jMenu1.add(jMenuItem1);
jMenu1.add(jMenuItem2);
JMenu jMenu2 = new JMenu("編輯");
JMenuItem ji3 = new JMenuItem("剪切");
JMenuItem ji4 = new JMenuItem("復制");
jMenu2.add(ji3);
jMenu2.add(ji4);
JMenu jMenu3 = new JMenu("幫助");
JMenuItem ji5 = new JMenuItem("關于");
jMenu3.add(ji5);
jMenuBar.add(jMenu1);
jMenuBar.add(jMenu2);
jMenuBar.add(jMenu3);
this.setJMenuBar(jMenuBar);
this.add(jPanel);
this.setVisible(true);
}
public static void main(String[] args) {
new Demo8JMenuBar();
}
}
?代碼實現(xiàn)的結果:
六.事件處理
一個源(事件源)產生一個事件(事件對象)并把它送到監(jiān)聽器那里, 監(jiān)聽器只是簡單地等待,直到它收到一個事件,一旦事件被接受,監(jiān)聽 器將處理這些事件。
一個事件源必須注冊監(jiān)聽器以便監(jiān)聽器可以接受關于一個特定事件的通知。
添加事件監(jiān)聽器(此處即為匿名類) 按鈕對象.
addActionListener(new ActionListener() {
// 事件處理
@Override public void actionPerformed(ActionEvent e) {
執(zhí)行操作 }
});
在代碼中的具體實現(xiàn):
package com.ffyc.javaGUI.guitest.test3;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class GridDemo3 extends JFrame {
public GridDemo3() throws HeadlessException {
this.setTitle("登錄界面");
this.setSize(300,200);//設置大小
this.setLocationRelativeTo(null);//設置窗口水平居中
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//設置窗口關閉選項
this.setResizable(false);//禁止修改窗口大小
//添加菜單
JMenuBar jMenuBar = new JMenuBar();
JMenu jMenu1 = new JMenu("文件");
JMenuItem jMenuItem1 = new JMenuItem("保存");
JMenuItem jMenuItem2 = new JMenuItem("新建");
jMenu1.add(jMenuItem1);
jMenu1.add(jMenuItem2);
JMenu jMenu2 = new JMenu("幫助");
jMenuBar.add(jMenu1);
jMenuBar.add(jMenu2);
//網格布局
JPanel jPanel = new JPanel(new GridLayout(3,1));
JPanel jPanel1 = new JPanel();
JLabel jLabel = new JLabel("賬戶");
//jLabel.setForeground(new Color(0x4DBB6EEA, true));
jLabel.setFont(new Font("",Font.BOLD,15));
JTextField jTextField = new JTextField(15);
jPanel1.add(jLabel);
jPanel1.add(jTextField);
JPanel jPanel2 = new JPanel();
JLabel jLabel1 = new JLabel("密碼");
jLabel1.setFont(new Font("",Font.BOLD,15));
JPasswordField jTextField1 = new JPasswordField(15);
jPanel2.add(jLabel1);
jPanel2.add(jTextField1);
JPanel jPanel3 = new JPanel();
JButton jButton = new JButton("登錄");
jPanel3.add(jButton);
jPanel.add(jPanel1);
jPanel.add(jPanel2);
jPanel.add(jPanel3);
this.add(jPanel);
this.setJMenuBar(jMenuBar);//將菜單添加到窗口
this.setVisible(true);
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showConfirmDialog(null,"是否登錄賬號");
if(jTextField.getText().length()<=0){
System.out.println("賬號不能為空");
}else{
if(jTextField.getText().equals("123456")||jTextField1.getText().equals("123456")){
JOptionPane.showMessageDialog(null,"登錄成功","操作提示",JOptionPane.INFORMATION_MESSAGE);
}else {
JOptionPane.showMessageDialog(null,"賬號或密碼錯誤");
}
}
}
});
jMenu1.addMouseListener(new MouseAdapter() {//點擊操作
@Override
public void mouseClicked(MouseEvent e) {
JOptionPane.showMessageDialog(null,"點擊");
}
});
}
public static void main(String[] args) {
new GridDemo3();
}
}
結果:
JOptionPane對話框
showMessageDialog():消息對話框
主要有五種消息類型,類型不同,圖標不同:
ERROR_MESSAGE 錯誤消息提示
INFORMATION_MESSAGE 信息提示
WARNING_MESSAGE 警告提示
QUESTION_MESSAGE 問題提示
PLAIN_MESSAGE 簡潔提示
showConfirmDialog():確認對話框 主要有四種消息類型,類型不同,
圖標不同: DEFAULT_OPTION 默認選項
YES_NO_OPTION 是/否選項
YES_NO_CANCEL_OPTION 是/否/取消選項
?OK_CANCEL_OPTION 確定/取消
?
柚子快報激活碼778899分享:開發(fā)語言 JAVA中的GUI
參考鏈接
本文內容根據(jù)網絡資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點和立場。
轉載請注明,如有侵權,聯(lián)系刪除。