Java有趣好玩的圖形界面開發(fā)八個(gè)案例實(shí)現(xiàn)
雖然GUI技術(shù)沒有很大的市場,甚至很多初學(xué)者放棄學(xué)習(xí)GUI,但是學(xué)習(xí)GUI編程的過程對于提高編程興趣,深入理解Java編程有很大的作用。效果圖如下,加油吧??!

1.復(fù)選框和單選框按鈕組
—在框架窗口中加入復(fù)選框和單選框按鈕組
import javax.swing.*;
public class App extends JFrame{
static JFrame jFrame=new JFrame("復(fù)選框和單選組按鈕選取框");
static JCheckBox jCheckBox1=new JCheckBox("粗體",true);
static JCheckBox jCheckBox2=new JCheckBox("斜體");
static JCheckBox jCheckBox3=new JCheckBox("下劃線");
static JRadioButton jRadioButton1=new JRadioButton("紅色",true);
static JRadioButton jRadioButton2=new JRadioButton("綠色",true);
static JRadioButton jRadioButton3=new JRadioButton("藍(lán)色");
public static void main(String[] args) {
ButtonGroup buttonGroup=new ButtonGroup();
jFrame.setLocation(200,150);
jFrame.setSize(300,220);
jFrame.setLayout(null);
jCheckBox1.setBounds(20,20,50,20);
jCheckBox2.setBounds(20,40,50,20);
jCheckBox3.setBounds(20,60,70,20);
jRadioButton1.setBounds(40,100,50,20);
jRadioButton2.setBounds(40,120,50,20);
jRadioButton3.setBounds(40,140,50,20);
jFrame.add(jCheckBox1);
jFrame.add(jCheckBox2);
jFrame.add(jCheckBox3);
buttonGroup.add(jRadioButton1);
buttonGroup.add(jRadioButton2);
buttonGroup.add(jRadioButton3);
jFrame.add(jRadioButton1);
jFrame.add(jRadioButton2);
jFrame.add(jRadioButton3);
jFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
jFrame.setVisible(true);
}
}

2.文本編輯組件和滾動(dòng)窗格
—設(shè)置文本編輯組件和滾動(dòng)窗格
import javax.swing.*;
public class App extends JFrame{
JTextField jTextField=new JTextField("該文本框不可編輯",30);
static JPasswordField jPasswordField=new JPasswordField("HelloWorld",30);
public App(String str){
super(str);
jTextField.setBounds(20,40,140,20);
jTextField.setEditable(false);
add(jTextField);
}
public static void main(String[] args) {
App jFrame=new App("文本編輯功能窗口");
JTextArea jTextArea=new JTextArea("你好",10,30);
JScrollPane jScrollPane=new JScrollPane(jTextArea);
jFrame.setLocation(200,150);
jFrame.setSize(240,220);
jFrame.setLayout(null);
jScrollPane.setBounds(20,70,160,100);
jPasswordField.setBounds(20,10,140,10);
jFrame.add(jPasswordField);
jFrame.add(jScrollPane);
char[] passWorld=jPasswordField.getPassword();
String str=new String(passWorld);
System.out.println("密碼是:"+passWorld+"轉(zhuǎn)換后"+str);
jFrame.setVisible(true);
jFrame.setResizable(false);
jFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}

輸出結(jié)果:密碼是:[C@370736d9轉(zhuǎn)換后HelloWorld
3.多個(gè)選項(xiàng)卡設(shè)置
—在窗口中放一個(gè)選項(xiàng)卡窗格,并在選項(xiàng)卡窗格中加入若干選項(xiàng)卡,每個(gè)選項(xiàng)卡中放置一個(gè)帶圖像的標(biāo)簽組件。
import javax.swing.*;
public class App extends JFrame {
public App(){
JLabel[] jLabels=new JLabel[6];
Icon pic;
String title;
for(int i=1;i<=5;i++){
pic=new ImageIcon("images\\t"+i+".png");
jLabels[i]=new JLabel();
jLabels[i].setIcon(pic);
title="第"+i+"頁";
jTabbedPane.add(title,jLabels[i]);
}
this.add(jTabbedPane);
}
JTabbedPane jTabbedPane=new JTabbedPane(JTabbedPane.TOP);
public static void main(String[] args) {
App jFrame=new App();
jFrame.setTitle("選項(xiàng)卡的應(yīng)用");
jFrame.setSize(300,300);
jFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
jFrame.setVisible(true);
}
}

4.在框架窗口中加入面板
import javax.swing.*;
import javax.swing.border.TitledBorder;
public class App {
public static void main(String[] args) {
JFrame jFrame=new JFrame("我的框架");
jFrame.setSize(210,180);
jFrame.setLocation(500,400);
JPanel jPanel=new JPanel();
jPanel.setSize(120,90);
jPanel.setLocation(40,30);
JButton jButton=new JButton("點(diǎn)擊我");
jButton.setSize(80,20);
jButton.setLocation(20,30);
jFrame.setLayout(null);
jPanel.setLayout(null);
jPanel.add(jButton);
jPanel.setBorder(new TitledBorder("面板區(qū)"));
jFrame.add(jPanel);
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jFrame.setVisible(true);
}
}

5.在窗口中加入標(biāo)簽
—在窗口中加入標(biāo)簽,并設(shè)置框架的背景色及標(biāo)簽上文字的顏色和字體。
import javax.swing.*;
import java.awt.*;
public class App {
public static void main(String[] args) {
JFrame jFrame=new JFrame("標(biāo)簽類窗口");
JLabel jLabel=new JLabel("我是一個(gè)標(biāo)簽",JLabel.CENTER);//創(chuàng)建標(biāo)簽類對象
jFrame.setLayout(null);//取消默認(rèn)布局管理器
jFrame.setSize(300,200);//設(shè)置窗口的大小
Container c=jFrame.getContentPane();//獲取內(nèi)容窗格
c.setBackground(Color.CYAN);//設(shè)置窗口的背景色
jLabel.setOpaque(true);//設(shè)置標(biāo)簽為不透明
jLabel.setBackground(Color.RED);//設(shè)置標(biāo)簽的背景色
jLabel.setForeground(Color.YELLOW);//設(shè)置標(biāo)簽的前景色
jLabel.setLocation(80,60);
jLabel.setSize(130,30);
Font font=new Font("楷體",Font.PLAIN,20);//創(chuàng)建字體對象
jLabel.setFont(font);//設(shè)置標(biāo)簽上的字體
jFrame.add(jLabel);
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jFrame.setVisible(true);
}
}

6.框架中加入指定大小的標(biāo)簽
—在框架中加入指定大小的標(biāo)簽,并設(shè)置當(dāng)鼠標(biāo)懸停在標(biāo)簽上時(shí)給出相應(yīng)的提示信息。
import javax.swing.*;
import java.awt.*;
public class App {
public static void main(String[] args) {
JFrame jFrame=new JFrame("標(biāo)簽類窗口");
JLabel jLabel=new JLabel("我是一個(gè)標(biāo)簽",JLabel.CENTER);//創(chuàng)建標(biāo)簽類對象
jFrame.setLayout(null);//取消默認(rèn)布局管理器
jFrame.setSize(300,200);//設(shè)置窗口的大小
Container c=jFrame.getContentPane();//獲取內(nèi)容窗格
c.setBackground(Color.CYAN);//設(shè)置窗口的背景色
jLabel.setOpaque(true);//設(shè)置標(biāo)簽為不透明
jLabel.setBackground(Color.RED);//設(shè)置標(biāo)簽的背景色
jLabel.setForeground(Color.YELLOW);//設(shè)置標(biāo)簽的前景色
jLabel.setLocation(80,60);
jLabel.setSize(130,30);
jLabel.setToolTipText("我被設(shè)置為不透明");
Font font=new Font("楷體",Font.PLAIN,20);//創(chuàng)建字體對象
jLabel.setFont(font);//設(shè)置標(biāo)簽上的字體
jFrame.add(jLabel);
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jFrame.setVisible(true);
}
}

7.在框架窗口中加入按鈕
import javax.swing.*;
import java.awt.*;
public class App extends JFrame {
public static void main(String[] args) {
App jFrame=new App();
jFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
ImageIcon icon=new ImageIcon("images\\java.png");
JButton jButton=new JButton();
jButton.setText("選擇");
jButton.setIcon(icon);
jFrame.setLayout(null);
jFrame.setSize(200,180);
jFrame.setTitle("按鈕類窗口");
jButton.setBounds(50,45,100,40);
jButton.setToolTipText("我是按鈕");
jFrame.add(jButton);
jFrame.setVisible(true);
}
}

8.框架窗口的創(chuàng)建
import javax.swing.*;
import java.awt.*;
public class App {
static JFrame jFrame = new JFrame("這是一個(gè)Swing程序");//創(chuàng)建靜態(tài)框架并設(shè)置標(biāo)題
public static void main(String[] args) {
JLabel label = new JLabel("我是一個(gè)標(biāo)簽");//創(chuàng)建一個(gè)標(biāo)簽對象
jFrame.setSize(400, 300);//設(shè)置框架的大小
Image image=(new ImageIcon("images\\java.jpg")).getImage();//創(chuàng)建圖標(biāo)對象
jFrame.setIconImage(image);//設(shè)置窗口的顯示圖標(biāo)
jFrame.setLocationRelativeTo(null);//設(shè)置窗口的位置
jFrame.add(label);//將標(biāo)簽對象加入到窗口中
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//單擊窗口的關(guān)閉按鈕,結(jié)束程序
jFrame.setVisible(true);//設(shè)置窗口可見
}
}
總結(jié)
- 圖形用戶界面是應(yīng)用程序與用戶交互的窗口,利用它可以接受用戶的輸入并向用戶輸出程序執(zhí)行的結(jié)果。
- 圖形用戶界面技術(shù)(GUI)是指用圖形的方式,借助菜單,按鈕等標(biāo)準(zhǔn)界面元素與鼠標(biāo)操作,幫助用戶方便的向計(jì)算機(jī)系統(tǒng)發(fā)出指令,啟動(dòng)操作,并將計(jì)算機(jī)系統(tǒng)運(yùn)行的結(jié)果以圖形的方式顯示給用戶的技術(shù)。
- Java提供了兩個(gè)處理圖形界面的包:java.awt和javax.swing。其中javax.swing包是java.awt的擴(kuò)展。
- Javax.swing包中包含組件類,事件類,接口,布局類,菜單類等,其繼承關(guān)系如下:

少年沒有烏托邦,心向遠(yuǎn)方自明朗。與風(fēng)隨行皆理想,遺憾最終皆幻想。
到此這篇關(guān)于Java有趣好玩的圖形界面開發(fā)八個(gè)案例實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Java圖形界面內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決swaggerUI頁面沒有顯示Controller方法的坑
這篇文章主要介紹了解決swaggerUI頁面沒有顯示Controller方法的坑,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
解決springboot mapper注入報(bào)紅問題
這篇文章主要介紹了解決springboot mapper注入報(bào)紅問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
Java的Swing編程中使用SwingWorker線程模式及頂層容器
這篇文章主要介紹了在Java的Swing編程中使用SwingWorker線程模式及頂層容器的方法,適用于客戶端圖形化界面軟件的開發(fā),需要的朋友可以參考下2016-01-01
一文帶你吃透JSP增刪改查實(shí)戰(zhàn)案例詳細(xì)解讀
這篇文章主要為大家詳細(xì)介紹了JSP中增刪改查實(shí)戰(zhàn)案例的相關(guān)知識(shí),文中的示例代碼講解現(xiàn)象,具有一定的借鑒價(jià)值,感興趣的小伙伴可以了解一下2023-03-03
Java中的字符流FileReader與FileWriter詳解
這篇文章主要介紹了Java中的字符流FileReader與FileWriter詳解,在Java中,使用Unicode約定存儲(chǔ)字符,字符流自動(dòng)允許我們逐字符讀/寫數(shù)據(jù),有助于執(zhí)行16位Unicode的輸入和輸出,它是以reader和writer結(jié)尾的,需要的朋友可以參考下2023-10-10
如何把Spring Cloud Data Flow部署在Kubernetes上
這篇文章主要介紹了把Spring Cloud Data Flow部署在Kubernetes上,再跑個(gè)任務(wù)試試,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08
Spring Boot將項(xiàng)目打包成war包的操作方法
這篇文章主要介紹了Spring Boot將項(xiàng)目打包成war包的操作方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-09-09

