Java有趣好玩的圖形界面開發八個案例實現_第1頁
Java有趣好玩的圖形界面開發八個案例實現_第2頁
Java有趣好玩的圖形界面開發八個案例實現_第3頁
Java有趣好玩的圖形界面開發八個案例實現_第4頁
Java有趣好玩的圖形界面開發八個案例實現_第5頁
已閱讀5頁,還剩4頁未讀, 繼續免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

第Java有趣好玩的圖形界面開發八個案例實現目錄1.復選框和單選框按鈕組2.文本編輯組件和滾動窗格3.多個選項卡設置4.在框架窗口中加入面板5.在窗口中加入標簽6.框架中加入指定大小的標簽7.在框架窗口中加入按鈕8.框架窗口的創建總結雖然GUI技術沒有很大的市場,甚至很多初學者放棄學習GUI,但是學習GUI編程的過程對于提高編程興趣,深入理解Java編程有很大的作用。效果圖如下,加油吧??!

1.復選框和單選框按鈕組

在框架窗口中加入復選框和單選框按鈕組

importjavax.swing.*;

publicclassAppextendsJFrame{

staticJFramejFrame=newJFrame("復選框和單選組按鈕選取框");

staticJCheckBoxjCheckBox1=newJCheckBox("粗體",true);

staticJCheckBoxjCheckBox2=newJCheckBox("斜體");

staticJCheckBoxjCheckBox3=newJCheckBox("下劃線");

staticJRadioButtonjRadioButton1=newJRadioButton("紅色",true);

staticJRadioButtonjRadioButton2=newJRadioButton("綠色",true);

staticJRadioButtonjRadioButton3=newJRadioButton("藍色");

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.文本編輯組件和滾動窗格

設置文本編輯組件和滾動窗格

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+"轉換后"+str);

jFrame.setVisible(true);

jFrame.setResizable(false);

jFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);

輸出結果:密碼是:[C@370736d9轉換后HelloWorld

3.多個選項卡設置

在窗口中放一個選項卡窗格,并在選項卡窗格中加入若干選項卡,每個選項卡中放置一個帶圖像的標簽組件。

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("選項卡的應用");

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("點擊我");

jButton.setSize(80,20);

jButton.setLocation(20,30);

jFrame.setLayout(null);

jPanel.setLayout(null);

jPanel.add(jButton);

jPanel.setBorder(newTitledBorder("面板區"));

jFrame.add(jPanel);

jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

jFrame.setVisible(true);

5.在窗口中加入標簽

在窗口中加入標簽,并設置框架的背景色及標簽上文字的顏色和字體。

importjavax.swing.*;

importjava.awt.*;

publicclassApp{

publicstaticvoidmain(String[]args){

JFramejFrame=newJFrame("標簽類窗口");

JLabeljLabel=newJLabel("我是一個標簽",JLabel.CENTER);//創建標簽類對象

jFrame.setLayout(null);//取消默認布局管理器

jFrame.setSize(300,200);//設置窗口的大小

Containerc=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);

Fontfont=newFont("楷體",Font.PLAIN,20);//創建字體對象

jLabel.setFont(font);//設置標簽上的字體

jFrame.add(jLabel);

jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

jFrame.setVisible(true);

6.框架中加入指定大小的標簽

在框架中加入指定大小的標簽,并設置當鼠標懸停在標簽上時給出相應的提示信息。

importjavax.swing.*;

importjava.awt.*;

publicclassApp{

publicstaticvoidmain(String[]args){

JFramejFrame=newJFrame("標簽類窗口");

JLabeljLabel=newJLabel("我是一個標簽",JLabel.CENTER);//創建標簽類對象

jFrame.setLayout(null);//取消默認布局管理器

jFrame.setSize(300,200);//設置窗口的大小

Containerc=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("我被設置為不透明");

Fontfont=newFont("楷體",Font.PLAIN,20);//創建字體對象

jLabel.setFont(font);//設置標簽上的字體

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.框架窗口的創建

importjavax.swing.*;

importjava.awt.*;

publicclassApp{

staticJFramejFrame=newJFrame("這是一個Swing程序");//創建靜態框架并設置標題

publicstaticvoidmain(String[]args){

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論