




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
第Java有趣好玩的圖形界面開(kāi)發(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ù)沒(méi)有很大的市場(chǎng),甚至很多初學(xué)者放棄學(xué)習(xí)GUI,但是學(xué)習(xí)GUI編程的過(guò)程對(duì)于提高編程興趣,深入理解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+"頁(yè)";
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)簽類(lèi)窗口");
JLabeljLabel=newJLabel("我是一個(gè)標(biāo)簽",JLabel.CENTER);//創(chuàng)建標(biāo)簽類(lèi)對(duì)象
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)建字體對(duì)象
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)簽類(lèi)窗口");
JLabeljLabel=newJLabel("我是一個(gè)標(biāo)簽",JLabel.CENTER);//創(chuàng)建標(biāo)簽類(lèi)對(duì)象
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)建字體對(duì)象
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("按鈕類(lèi)窗口");
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. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 灑水車(chē)租車(chē)合同協(xié)議書(shū)
- 電梯監(jiān)理協(xié)議書(shū)
- 退還公款協(xié)議書(shū)
- 職員崗位協(xié)議書(shū)
- 烤煙專業(yè)化烘烤協(xié)議書(shū)
- 萊茵合作協(xié)議書(shū)
- 藍(lán)城小鎮(zhèn)協(xié)議書(shū)
- 稅款劃扣協(xié)議書(shū)
- 拱形棚造價(jià)合同協(xié)議書(shū)
- 租地改建協(xié)議書(shū)
- 外貿(mào)業(yè)務(wù)員內(nèi)部培訓(xùn)
- 石英砂采購(gòu)合同
- 電腦和打印機(jī)維保服務(wù)投標(biāo)文件、方案
- 老年患者進(jìn)食安全
- 玉米病蟲(chóng)害監(jiān)測(cè)與預(yù)警系統(tǒng)-洞察分析
- 山西建投考試題
- 初中生物大單元教學(xué)設(shè)計(jì)與課堂實(shí)施的策略與技巧
- 電機(jī)故障機(jī)理分析
- 建筑工程質(zhì)量管理培訓(xùn)
- 合伙開(kāi)家小型賓館協(xié)議書(shū)范文最簡(jiǎn)單
- 學(xué)生西服使用購(gòu)買(mǎi)問(wèn)卷調(diào)查
評(píng)論
0/150
提交評(píng)論