




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
第Java有趣好玩的圖形界面開發(fā)八個(gè)案例實(shí)現(xiàn)目錄1.復(fù)選框和單選框按鈕組2.文本編輯組件和滾動(dòng)窗格3.多個(gè)選項(xiàng)卡設(shè)置4.在框架窗口中加入面板5.在窗口中加入標(biāo)簽6.框架中加入指定大小的標(biāo)簽7.在框架窗口中加入按鈕8.框架窗口的創(chuàng)建總結(jié)雖然GUI技術(shù)沒有很大的市場,甚至很多初學(xué)者放棄學(xué)習(xí)GUI,但是學(xué)習(xí)GUI編程的過程對于提高編程興趣,深入理解Java編程有很大的作用。效果圖如下,加油吧!!
1.復(fù)選框和單選框按鈕組
在框架窗口中加入復(fù)選框和單選框按鈕組
importjavax.swing.*;
publicclassAppextendsJFrame{
staticJFramejFrame=newJFrame("復(fù)選框和單選組按鈕選取框");
staticJCheckBoxjCheckBox1=newJCheckBox("粗體",true);
staticJCheckBoxjCheckBox2=newJCheckBox("斜體");
staticJCheckBoxjCheckBox3=newJCheckBox("下劃線");
staticJRadioButtonjRadioButton1=newJRadioButton("紅色",true);
staticJRadioButtonjRadioButton2=newJRadioButton("綠色",true);
staticJRadioButtonjRadioButton3=newJRadioButton("藍(lán)色");
publicstaticvoidmain(String[]args){
ButtonGroupbuttonGroup=newButtonGroup();
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)窗格
importjavax.swing.*;
publicclassAppextendsJFrame{
JTextFieldjTextField=newJTextField("該文本框不可編輯",30);
staticJPasswordFieldjPasswordField=newJPasswordField("HelloWorld",30);
publicApp(Stringstr){
super(str);
jTextField.setBounds(20,40,140,20);
jTextField.setEditable(false);
add(jTextField);
publicstaticvoidmain(String[]args){
AppjFrame=newApp("文本編輯功能窗口");
JTextAreajTextArea=newJTextArea("你好",10,30);
JScrollPanejScrollPane=newJScrollPane(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();
Stringstr=newString(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)簽組件。
importjavax.swing.*;
publicclassAppextendsJFrame{
publicApp(){
JLabel[]jLabels=newJLabel[6];
Iconpic;
Stringtitle;
for(inti=1;ii++){
pic=newImageIcon("images\\t"+i+".png");
jLabels[i]=newJLabel();
jLabels[i].setIcon(pic);
title="第"+i+"頁";
jTabbedPane.add(title,jLabels[i]);
this.add(jTabbedPane);
JTabbedPanejTabbedPane=newJTabbedPane(JTabbedPane.TOP);
publicstaticvoidmain(String[]args){
AppjFrame=newApp();
jFrame.setTitle("選項(xiàng)卡的應(yīng)用");
jFrame.setSize(300,300);
jFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
jFrame.setVisible(true);
4.在框架窗口中加入面板
importjavax.swing.*;
importjavax.swing.border.TitledBorder;
publicclassApp{
publicstaticvoidmain(String[]args){
JFramejFrame=newJFrame("我的框架");
jFrame.setSize(210,180);
jFrame.setLocation(500,400);
JPaneljPanel=newJPanel();
jPanel.setSize(120,90);
jPanel.setLocation(40,30);
JButtonjButton=newJButton("點(diǎn)擊我");
jButton.setSize(80,20);
jButton.setLocation(20,30);
jFrame.setLayout(null);
jPanel.setLayout(null);
jPanel.add(jButton);
jPanel.setBorder(newTitledBorder("面板區(qū)"));
jFrame.add(jPanel);
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jFrame.setVisible(true);
5.在窗口中加入標(biāo)簽
在窗口中加入標(biāo)簽,并設(shè)置框架的背景色及標(biāo)簽上文字的顏色和字體。
importjavax.swing.*;
importjava.awt.*;
publicclassApp{
publicstaticvoidmain(String[]args){
JFramejFrame=newJFrame("標(biāo)簽類窗口");
JLabeljLabel=newJLabel("我是一個(gè)標(biāo)簽",JLabel.CENTER);//創(chuàng)建標(biāo)簽類對象
jFrame.setLayout(null);//取消默認(rèn)布局管理器
jFrame.setSize(300,200);//設(shè)置窗口的大小
Containerc=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);
Fontfont=newFont("楷體",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)的提示信息。
importjavax.swing.*;
importjava.awt.*;
publicclassApp{
publicstaticvoidmain(String[]args){
JFramejFrame=newJFrame("標(biāo)簽類窗口");
JLabeljLabel=newJLabel("我是一個(gè)標(biāo)簽",JLabel.CENTER);//創(chuàng)建標(biāo)簽類對象
jFrame.setLayout(null);//取消默認(rèn)布局管理器
jFrame.setSize(300,200);//設(shè)置窗口的大小
Containerc=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è)置為不透明");
Fontfont=newFont("楷體",Font.PLAIN,20);//創(chuàng)建字體對象
jLabel.setFont(font);//設(shè)置標(biāo)簽上的字體
jFrame.add(jLabel);
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jFrame.setVisible(true);
7.在框架窗口中加入按鈕
importjavax.swing.*;
importjava.awt.*;
publicclassAppextendsJFrame{
publicstaticvoidmain(String[]args){
AppjFrame=newApp();
jFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
ImageIconicon=newImageIcon("images\\java.png");
JButtonjButton=newJButton();
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)建
importjavax.swing.*;
importjava.awt.*;
publicclassApp{
staticJFramejFrame=newJFrame("這是一個(gè)Swing程序");//創(chuàng)建靜態(tài)框架并設(shè)置標(biāo)題
publicstaticvoidmain(String[]args){
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 融資合同補(bǔ)充協(xié)議書范本
- 裝修材料訂金合同協(xié)議
- 蘋果裝飾施工合同協(xié)議
- 藥廠委托協(xié)議書模板
- 融資租賃土地合同協(xié)議
- 裝修建材材料合同協(xié)議
- 茶樓會(huì)員協(xié)議書范本
- 自聘教師聘請合同協(xié)議
- 營業(yè)執(zhí)照收購合同協(xié)議
- 英國簽證宿舍合同協(xié)議
- 電腦和打印機(jī)維保服務(wù)投標(biāo)文件、方案
- 《消費(fèi)者心理與行為分析》第五版 課件全套 肖澗松 單元1-10 消費(fèi)者心理與行為概述 - 消費(fèi)者購買決策與購后行為
- 塑料污染治理-洞察分析
- 商場運(yùn)營管理規(guī)定范文(2篇)
- 反詐知識競賽題庫及答案(共286題)
- 稀土材料技術(shù)基礎(chǔ)知識單選題100道及答案解析
- 生理學(xué)基礎(chǔ)題庫(46道)
- 量子儲(chǔ)能材料的探索
- 2023年人教版六年級語文下冊期末考試卷(A4打印版)
- ESG信息披露、表現(xiàn)和評級綜合研究:國內(nèi)外對比分析
- 2024年全國普法知識競賽法律知識題庫及答案
評論
0/150
提交評論