雖然GUI技術沒有很大的市場,甚至很多初學者放棄學習GUI,但是學習GUI編程的過程對于提高編程興趣,深入理解Java編程有很大的作用。效果圖如下,加油吧!!
1.復選框和單選框按鈕組
—在框架窗口中加入復選框和單選框按鈕組
import javax.swing.*;
public class App extends JFrame{
static JFrame jFrame=new JFrame("復選框和單選組按鈕選取框");
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("藍色");
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.文本編輯組件和滾動窗格
—設置文本編輯組件和滾動窗格
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+"轉換后"+str);
jFrame.setVisible(true);
jFrame.setResizable(false);
jFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
輸出結果:密碼是:[C@370736d9轉換后HelloWorld
3.多個選項卡設置
—在窗口中放一個選項卡窗格,并在選項卡窗格中加入若干選項卡,每個選項卡中放置一個帶圖像的標簽組件。
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("選項卡的應用");
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("點擊我");
jButton.setSize(80,20);
jButton.setLocation(20,30);
jFrame.setLayout(null);
jPanel.setLayout(null);
jPanel.add(jButton);
jPanel.setBorder(new TitledBorder("面板區"));
jFrame.add(jPanel);
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jFrame.setVisible(true);
}
}
5.在窗口中加入標簽
—在窗口中加入標簽,并設置框架的背景色及標簽上文字的顏色和字體。
import javax.swing.*;
import java.awt.*;
public class App {
public static void main(String[] args) {
JFrame jFrame=new JFrame("標簽類窗口");
JLabel jLabel=new JLabel("我是一個標簽",JLabel.CENTER);//創建標簽類對象
jFrame.setLayout(null);//取消默認布局管理器
jFrame.setSize(300,200);//設置窗口的大小
Container c=jFrame.getContentPane();//獲取內容窗格
c.setBackground(Color.CYAN);//設置窗口的背景色
jLabel.setOpaque(true);//設置標簽為不透明
jLabel.setBackground(Color.RED);//設置標簽的背景色
jLabel.setForeground(Color.YELLOW);//設置標簽的前景色
jLabel.setLocation(80,60);
jLabel.setSize(130,30);
Font font=new Font("楷體",Font.PLAIN,20);//創建字體對象
jLabel.setFont(font);//設置標簽上的字體
jFrame.add(jLabel);
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jFrame.setVisible(true);
}
}
6.框架中加入指定大小的標簽
—在框架中加入指定大小的標簽,并設置當鼠標懸停在標簽上時給出相應的提示信息。
import javax.swing.*;
import java.awt.*;
public class App {
public static void main(String[] args) {
JFrame jFrame=new JFrame("標簽類窗口");
JLabel jLabel=new JLabel("我是一個標簽",JLabel.CENTER);//創建標簽類對象
jFrame.setLayout(null);//取消默認布局管理器
jFrame.setSize(300,200);//設置窗口的大小
Container c=jFrame.getContentPane();//獲取內容窗格
c.setBackground(Color.CYAN);//設置窗口的背景色
jLabel.setOpaque(true);//設置標簽為不透明
jLabel.setBackground(Color.RED);//設置標簽的背景色
jLabel.setForeground(Color.YELLOW);//設置標簽的前景色
jLabel.setLocation(80,60);
jLabel.setSize(130,30);
jLabel.setToolTipText("我被設置為不透明");
Font font=new Font("楷體",Font.PLAIN,20);//創建字體對象
jLabel.setFont(font);//設置標簽上的字體
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.框架窗口的創建
import javax.swing.*;
import java.awt.*;
public class App {
static JFrame jFrame = new JFrame("這是一個Swing程序");//創建靜態框架并設置標題
public static void main(String[] args) {
JLabel label = new JLabel("我是一個標簽");//創建一個標簽對象
jFrame.setSize(400, 300);//設置框架的大小
Image image=(new ImageIcon("images\\java.jpg")).getImage();//創建圖標對象
jFrame.setIconImage(image);//設置窗口的顯示圖標
jFrame.setLocationRelativeTo(null);//設置窗口的位置
jFrame.add(label);//將標簽對象加入到窗口中
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//單擊窗口的關閉按鈕,結束程序
jFrame.setVisible(true);//設置窗口可見
}
}
總結
- 圖形用戶界面是應用程序與用戶交互的窗口,利用它可以接受用戶的輸入并向用戶輸出程序執行的結果。
- 圖形用戶界面技術(GUI)是指用圖形的方式,借助菜單,按鈕等標準界面元素與鼠標操作,幫助用戶方便的向計算機系統發出指令,啟動操作,并將計算機系統運行的結果以圖形的方式顯示給用戶的技術。
- Java提供了兩個處理圖形界面的包:java.awt和javax.swing。其中javax.swing包是java.awt的擴展。
- Javax.swing包中包含組件類,事件類,接口,布局類,菜單類等,其繼承關系如下:
少年沒有烏托邦,心向遠方自明朗。與風隨行皆理想,遺憾最終皆幻想。
到此這篇關于Java有趣好玩的圖形界面開發八個案例實現的文章就介紹到這了,更多相關Java圖形界面內容請搜索html5模板網以前的文章希望大家以后多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!