Java圖形化界面編程介紹
1.內(nèi)容概述
先談?wù)剛€人對圖形化界面編程的認(rèn)識,圖形化界面編程可以直接的看到每一步操作帶來的效果,相對于傳統(tǒng)編程盯著黑框框?qū)W起來是非常非常有意思的。
再談?wù)勛詈蟮男Ч?,界面是由窗口和組件構(gòu)成的。而組件在窗口內(nèi)的排列并不是沒有章法可言,依賴于布局管理器使組件以合適的位置以及合理的排布呈現(xiàn)。排布于窗口內(nèi)的組件又可以通過事件監(jiān)聽器與用戶進(jìn)行交互…
2.容器Container
什么是容器?容器是特殊的組件。容器是用來裝東西的,不僅可以存放組件,也可以用來存放容器,而存放的容器又可以存放容器或組件。聽起來有點(diǎn)反復(fù)套娃,但學(xué)起來還是很容易的!
2.1Window
Window是可以獨(dú)立存在的頂級窗口,其默認(rèn)使用BorderLayout布局管理器。
frame.setLocation(500,300)方法用來設(shè)置窗口的位置,通常計算機(jī)的遠(yuǎn)點(diǎn)坐標(biāo)在左上角。
frame.setSize(500,300)方法用來設(shè)置窗口的尺寸。
frame.setVisible(true)設(shè)置窗口是否可見。
運(yùn)行效果(使用Frame來創(chuàng)建一個窗口):

注意此時的窗口不能通過單擊右上角的’X’關(guān)閉窗口,只能手動結(jié)束程序,因?yàn)檫€沒有加入事件監(jiān)聽機(jī)制。
代碼:
import java.awt.*;
public class WindowDemo {
? ? public static void main(String[] args) {
? ? ? ? //創(chuàng)建一個窗口對象
? ? ? ? Frame frame = new Frame("測試Window窗口");
? ? ? ? //指定窗口的位置和大小
? ? ? ? frame.setLocation(500,300);
? ? ? ? frame.setSize(500,300);
? ? ? ? //設(shè)置窗口可見
? ? ? ? frame.setVisible(true);
? ? }
}2.2Panel
Panel是內(nèi)嵌式容器,必須內(nèi)嵌于其它容器中使用,不能獨(dú)立存在。其默認(rèn)使用FlowLayout布局管理器。
運(yùn)行效果:

例如:將panel加入Frame中,F(xiàn)lowLayout排列的性質(zhì)使Panel使用便于被使用。
通過Panel的add方法(p.add(new TextField("測試文本"));)向Panel中加入了一個TextField組件和一個Button組件。最后將Panel加入Frame中。
setBounds(100,100,500,300)方法可以一次性設(shè)置窗口的坐標(biāo)以及尺寸。
代碼:
import java.awt.*;
public class PanelDemo {
? ? public static void main(String[] args) {
? ? ? ? //1.創(chuàng)建一個Window對象,因?yàn)閜anel以及其它容器不能獨(dú)立存在必須依附于Window
? ? ? ? Frame frame = new Frame("這里演示panel");
? ? ? ? //2.創(chuàng)建一個panel對象
? ? ? ? Panel p = new Panel();
? ? ? ? //3.創(chuàng)建一個文本框和按鈕,并把它們放到Panel中
? ? ? ? p.add(new TextField("測試文本"));
? ? ? ? p.add(new Button("測試按鈕"));
? ? ? ? //4.把panel放入到Window中
? ? ? ? frame.add(p);
? ? ? ? //5.設(shè)置Window得位置及大小
? ? ? ? frame.setBounds(100,100,500,300);
? ? ? ? //6.設(shè)置Window可見
? ? ? ? frame.setVisible(true);
? ? }
}2.3ScrollPane
Scrollpane是帶滾動條的容器,不能獨(dú)立存在,默認(rèn)使用布BorderLayout局管理器。代碼第7行ScrollPane構(gòu)造方法中的參數(shù)ScrollPane.SCROLLBARS_ALWAYS可以使ScrollPane默認(rèn)顯示滾動條,因?yàn)楫?dāng)內(nèi)容不多時,ScrollPane不會默認(rèn)顯示滾動條。
運(yùn)行效果:

代碼:
import java.awt.*;
public class ScrollPaneDemo {
? ? public static void main(String[] args) {
? ? ? ? Frame frame = new Frame("這是測試ScrollPane");
? ? ? ? //創(chuàng)建一個ScrollPane
? ? ? ? ScrollPane sp = new ScrollPane(ScrollPane.SCROLLBARS_ALWAYS);
? ? ? ? //往ScrollPane中添加內(nèi)容
? ? ? ? sp.add(new TextField("測試文本"));
? ? ? ? //將ScrollPane加入Frame
? ? ? ? frame.add(sp);
? ? ? ? frame.setBounds(100,100,500,300);
? ? ? ? frame.setVisible(true);
? ? }
}2.4Box
Box容器,可以將容納的組件或容器水平或垂直排列非常有利于模塊化構(gòu)建窗口框架。
frame.pack()pack()方法可根據(jù)窗口內(nèi)組件的數(shù)量以及尺寸自動設(shè)置窗口的最佳大小。
使用Box.createHorizontalBox()方法創(chuàng)建一個水平Box容器,其存放內(nèi)容只能水平排列。
使用Box.createVerticalBox()方法創(chuàng)建一個垂直Box容器,其存放內(nèi)容只能垂直排列。
存放內(nèi)容的間隔使用Box.createHorizontalGlue()或Box.createVerticalGlue()方法,注意此類間隔的大小會隨著窗口拖動而改變。使用Box.createHorizontalStrut(width)(Box.createVerticalStrut(height))可以創(chuàng)建在水平(垂直)方向上尺寸不變的間隔。
運(yùn)行效果:

代碼:
import javax.swing.*;
import java.awt.*;
public class BoxDemo {
? ? public static void main(String[] args) {
? ? ? ? Frame frame = new Frame();
? ? ? ? //創(chuàng)建一個水平Box
? ? ? ? Box hbox = Box.createHorizontalBox();
? ? ? ? hbox.add(new Button("水平按鈕1"));
? ? ? ? hbox.add(Box.createHorizontalGlue());//尺寸不固定間隔
? ? ? ? hbox.add(new Button("水平按鈕2"));
? ? ? ? hbox.add(Box.createHorizontalStrut(30));;//水平方向尺寸不變間隔
? ? ? ? hbox.add(new Button("水平按鈕3"));
? ? ? ? //創(chuàng)建一個垂直Box
? ? ? ? Box vbox = Box.createVerticalBox();
? ? ? ? vbox.add(new Button("垂直按鈕1"));
? ? ? ? vbox.add(Box.createVerticalGlue());//尺寸不固定間隔
? ? ? ? vbox.add(new Button("垂直按鈕2"));
? ? ? ? vbox.add(Box.createVerticalStrut(30));//垂直方向尺寸不變間隔
? ? ? ? vbox.add(new Button("垂直按鈕3"));
? ? ? ? frame.add(hbox,BorderLayout.NORTH);
? ? ? ? frame.add(vbox);
? ? ? ? frame.pack();
? ? ? ? frame.setVisible(true);
? ? }
}3.布局管理器
3.1FlowLayout
FlowLayout流式布局管理器,按從左往右從上往下的順序添加內(nèi)容。可以自定義間距以及排列方式。 setLayout();方法可以為指定容器設(shè)置布局管理器。
如:frame.setLayout(new FlowLayout(FlowLayout.CENTER,40,20));就是將frame的布局管理器(frame默認(rèn)為BorderLayout)更改為FlowLayout。
構(gòu)造方法中FlowLayout(FlowLayout.CENTER,40,20)第一個參數(shù)為指定排列方式,后兩個參數(shù)為行間距以及列間距。FlowLayout.CENTER表示居中對齊;FlowLayout.LEFT表示左對齊;FlowLayout.RIGHT表示右對齊。
運(yùn)行效果(使用流式布局管理器加入9個按鈕):

代碼:
import java.awt.*;
public class FlowLayoutDemo {
? ? public static void main(String[] args) {
? ? ? ? Frame frame = new Frame();
? ? ? ? //1.通過setLayout
? ? ? ? frame.setLayout(new FlowLayout(FlowLayout.CENTER,40,20));
? ? ? ? for(int i=1;i<=9;i++){
? ? ? ? ? ? frame.add(new Button(""+i));
? ? ? ? }
? ? ? ? frame.pack();
? ? ? ? frame.setVisible(true);
? ? }
}3.2BorderLayout
邊界布局管理器,Frame和ScrollPane默認(rèn)使用BorderLayout布局管理器。BorderLayout將區(qū)域劃分為中部(CENTER)、北部(NORTH)、南部(SOUTH)、西部(WEST)和東部(EAST)。注意每個區(qū)域只能容納一個組件或容器,在同一區(qū)域多次放入組件會造成覆蓋。但可以向區(qū)域中加入容器,比如向中部加入Panel,再向Panel中加入很多按鈕或文本是可以的。
運(yùn)行效果(區(qū)域分布):

當(dāng)某一區(qū)域不存在時,會由中部區(qū)域填充。
代碼:
import java.awt.*;
public class BorderLayoutDemo {
? ? public static void main(String[] args) {
? ? ? ? Frame frame = new Frame("測試BorderLayout");
? ? ? ? //1.通過setLayout
? ? ? ? frame.setLayout(new BorderLayout(30,10));
? ? ? ? frame.add(new Button("北部"),BorderLayout.NORTH);
? ? ? ? frame.add(new Button("南部"),BorderLayout.SOUTH);
? ? ? ? frame.add(new Button("東部"),BorderLayout.EAST);
? ? ? ? frame.add(new Button("西部"),BorderLayout.WEST);
? ? ? ? frame.add(new Button("中部"));//不添加區(qū)域指定,默認(rèn)中部
? ? ? ? frame.pack();
? ? ? ? frame.setVisible(true);
? ? }
}嘗試向中部區(qū)域加入裝有9個按鈕的Panel。
運(yùn)行效果:

代碼:
package Awt;
import java.awt.*;
public class BorderLayoutDemo {
? ? public static void main(String[] args) {
? ? ? ? Frame frame = new Frame("測試BorderLayout");
? ? ? ? //1.通過setLayout
? ? ? ? frame.setLayout(new BorderLayout(30,10));
? ? ? ? frame.add(new Button("北部"),BorderLayout.NORTH);
? ? ? ? frame.add(new Button("南部"),BorderLayout.SOUTH);
? ? ? ? frame.add(new Button("東部"),BorderLayout.EAST);
? ? ? ? frame.add(new Button("西部"),BorderLayout.WEST);
? ? ? ? Panel panel = new Panel();
? ? ? ? for(int i=0;i<9;i++){
? ? ? ? ? ? panel.add(new Button(i+""));
? ? ? ? }
? ? ? ? frame.add(panel);
? ? ? ? frame.pack();
? ? ? ? frame.setVisible(true);
? ? }
}3.3GridLayout
GridLayout網(wǎng)式布局管理器,可以將區(qū)域劃分為r*c個小區(qū)域,GridLayout構(gòu)造方法GridLayout(rows,cols,hgap,vgap)四個參數(shù)分別指定了要劃分的行、列、水平間距和垂直間距。
在Frame的北部區(qū)域放置一個文本框,中部區(qū)域存放一個指定布局管理器為網(wǎng)式布局管理器的Panel,并加入按鈕組件,會發(fā)生什么?
運(yùn)行效果:

注意:此時的窗口還未加入事件監(jiān)聽,計算器還不能使用。但也快了。
代碼:
import java.awt.*;
public class GridLayOutDemo {
? ? public static void main(String[] args) {
? ? ? ? Frame frame = new Frame("計算器");
? ? ? ? frame.add(new TextField(30),BorderLayout.NORTH);
? ? ? ? Panel p = new Panel();
? ? ? ? p.setLayout(new GridLayout(3,5,4,4));
? ? ? ? for (int i = 0; i < 10; i++) {
? ? ? ? ? ? p.add(new Button(i+""));
? ? ? ? }
? ? ? ? String s = "+-*/.";
? ? ? ? for(int i=0;i<5;i++){
? ? ? ? ? ? p.add(new Button(s.charAt(i)+""));
? ? ? ? }
? ? ? ? frame.add(p);
? ? ? ? frame.pack();
? ? ? ? frame.setVisible(true);
? ? }
}3.4Cardlayout
CardLayout卡片式布局管理器,相當(dāng)于一疊撲克牌,疊放式分布。
初識事件監(jiān)聽機(jī)制,對按鈕注冊監(jiān)聽,可以達(dá)到點(diǎn)擊按鈕有對應(yīng)響應(yīng)的效果。簡單了解事件監(jiān)聽后續(xù)有詳細(xì)講解,其中代碼27行e.getActionCommand()得到的信息就是按鈕上的字符。
運(yùn)行效果:

代碼:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class CardLayoutDemo {
? ? public static void main(String[] args) {
? ? ? ? Frame frame = new Frame();
? ? ? ? Panel p = new Panel();
? ? ? ? CardLayout cardLayout = new CardLayout();
? ? ? ? p.setLayout(cardLayout);
? ? ? ? String[] names = {"第一張","第二張","第三張","第四張","第五張"};
? ? ? ? for(int i=0;i<5;i++){
? ? ? ? ? ? p.add(names[i],new Button(names[i]));
? ? ? ? }
? ? ? ? frame.add(p);
? ? ? ? String[] operat = {"上一張","下一張","第一張","最后一張","第三張"};
? ? ? ? Panel p2 = new Panel();
? ? ? ? Button b1 = new Button(operat[0]);
? ? ? ? Button b2 = new Button(operat[1]);
? ? ? ? Button b3 = new Button(operat[2]);
? ? ? ? Button b4 = new Button(operat[3]);
? ? ? ? Button b5 = new Button(operat[4]);
? ? ? ? ActionListener listener = new ActionListener(){
? ? ? ? ? ? @Override
? ? ? ? ? ? public void actionPerformed(ActionEvent e){
? ? ? ? ? ? ? ? String actionCommand = e.getActionCommand();
? ? ? ? ? ? ? ? switch(actionCommand){
? ? ? ? ? ? ? ? ? ? case "上一張":
? ? ? ? ? ? ? ? ? ? ? ? cardLayout.previous(p);
? ? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? case "下一張":
? ? ? ? ? ? ? ? ? ? ? ? cardLayout.next(p);
? ? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? case "第一張":
? ? ? ? ? ? ? ? ? ? ? ? cardLayout.first(p);
? ? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? case "最后一張":
? ? ? ? ? ? ? ? ? ? ? ? cardLayout.last(p);
? ? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? case "第三張":
? ? ? ? ? ? ? ? ? ? ? ? cardLayout.show(p,"第三張");
? ? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? };
? ? ? ? b1.addActionListener(listener);
? ? ? ? b2.addActionListener(listener);
? ? ? ? b3.addActionListener(listener);
? ? ? ? b4.addActionListener(listener);
? ? ? ? b5.addActionListener(listener);
? ? ? ? p2.add(b1);
? ? ? ? p2.add(b2);
? ? ? ? p2.add(b3);
? ? ? ? p2.add(b4);
? ? ? ? p2.add(b5);
? ? ? ? frame.add(p2,BorderLayout.SOUTH);
? ? ? ? frame.pack();
? ? ? ? frame.setVisible(true);
? ? }
}4.AWT基本組件
Button:按鈕組件,可以單擊并作出響應(yīng)。TextField:單行文本框??梢杂胹et()和get()方法設(shè)置和獲取文本內(nèi)容。TextArea:多行文本域。Choice:下拉選擇框。Checkbox復(fù)選框組件,也可以單獨(dú)使用作為單選框組件。CheckboxGroup:將多個Checkbox組裝為一組,每組中只有一個選項(xiàng)可以被選中。List:列表框組件可以添加多項(xiàng)條目。
運(yùn)行效果:

代碼:
import javax.swing.*;
import java.awt.*;
public class BasicComponentDemo {
? ? Frame frame = new Frame();
? ? //文本框
? ? TextArea ta = new TextArea(5,20);
? ? //下拉選擇框
? ? Choice colorChooser = new Choice();
? ? //復(fù)選框
? ? CheckboxGroup cbg = new CheckboxGroup();
? ? Checkbox male = new Checkbox("男",cbg,true);
? ? Checkbox famale = new Checkbox("女",cbg,false);
? ? Checkbox isMarred = new Checkbox("是否已婚?");
? ? //單行文本框
? ? TextField tf = new TextField(20);
? ? //按鈕
? ? Button ok = new Button("確認(rèn)");
? ? //列表框
? ? List colorList = new List(6,true);
? ? public void init(){
? ? ? ? Box bBox = Box.createHorizontalBox();
? ? ? ? //底部
? ? ? ? bBox.add(tf);
? ? ? ? bBox.add(ok);
? ? ? ? frame.add(bBox,BorderLayout.SOUTH);
? ? ? ? //topLeft
? ? ? ? colorChooser.add("紅色");
? ? ? ? colorChooser.add("藍(lán)色");
? ? ? ? colorChooser.add("黃色");
? ? ? ? Box cBox = Box.createHorizontalBox();
? ? ? ? cBox.add(colorChooser);
? ? ? ? cBox.add(male);
? ? ? ? cBox.add(famale);
? ? ? ? cBox.add(isMarred);
? ? ? ? Box topLeft = Box.createVerticalBox();
? ? ? ? topLeft.add(ta);
? ? ? ? topLeft.add(cBox);
? ? ? ? Box top = Box.createHorizontalBox();
? ? ? ? top.add(topLeft);
? ? ? ? //topRight
? ? ? ? colorList.add("紅色");
? ? ? ? colorList.add("黃色");
? ? ? ? colorList.add("藍(lán)色");
? ? ? ? top.add(colorList);
? ? ? ? //組裝
? ? ? ? frame.add(top);
? ? ? ? frame.pack();
? ? ? ? frame.setVisible(true);
? ? }
? ? public static void main(String[] args) {
? ? ? ?new BasicComponentDemo().init();
? ? }
}5.事件處理
當(dāng)組件上發(fā)生某些操作時會自動觸發(fā)一段代碼的執(zhí)行。
一個事件的發(fā)生是由事件源產(chǎn)生事件,事件監(jiān)聽器捕獲事件最后做出相應(yīng)的響應(yīng)(自動執(zhí)行一段代碼)。將事件監(jiān)聽器加入到事件源上的過程稱為注冊監(jiān)聽。
例如:當(dāng)按鈕為事件源,添加myListener監(jiān)聽器注冊監(jiān)聽,事件發(fā)生時會自動向單行文本框中添加“Hello world!”。
執(zhí)行效果:

代碼:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Hello {
? ? Frame frame = new Frame("測試監(jiān)聽事件");
? ? //事件源
? ? Button b = new Button("確定");
? ? TextField tf = new TextField(30);
? ? public void init(){
? ? ? ? //監(jiān)聽器
? ? ? ? MyListener myListener = new MyListener();
? ? ? ? //注冊監(jiān)聽
? ? ? ? //匿名內(nèi)部類 事件監(jiān)聽器只與一個事件有關(guān)
? ? ? ? b.addActionListener(new ActionListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void actionPerformed(ActionEvent e) {
? ? ? ? ? ? ? ? tf.setText("Hello world!");
? ? ? ? ? ? }
? ? ? ? });
? ? ? ? frame.add(tf,BorderLayout.NORTH);
? ? ? ? frame.add(b);
? ? ? ? frame.pack();
? ? ? ? frame.setVisible(true);
? ? }
? ? //內(nèi)部類 共同一類事件使用
? ? private class MyListener implements ActionListener {
? ? ? ? @Override
? ? ? ? public void actionPerformed(ActionEvent e) {
? ? ? ? ? ? tf.setText("Hello world!");
? ? ? ? }
? ? }
? ? public static void main(String[] args) {
? ? ? ? new Hello().init();
? ? }
}常見的事件監(jiān)聽器:
ComponentEvent:組件事件,當(dāng)組件尺寸、位置、顯示/隱藏狀態(tài)發(fā)生改變時觸發(fā)事件。ContainerEvent:容器事件,當(dāng)容器里添加刪除組件時觸發(fā)該事件。WindonEvent:窗口事件,當(dāng)窗口狀態(tài)改變時觸發(fā)該事件。FoucusEvent:焦點(diǎn)事件,當(dāng)組件得到焦點(diǎn)或失去焦點(diǎn)時觸發(fā)該事件。KeyEvent:鍵盤事件,當(dāng)按、松開下鍵盤時觸發(fā)該事件。MouseEvent:鼠標(biāo)事件,當(dāng)單擊、松開或移動鼠標(biāo)時觸發(fā)該事件。
利用窗口事件寫一個可以點(diǎn)擊’X’關(guān)閉的窗口。
代碼:
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class WindowDemo {
? ? public static void main(String[] args) {
? ? ? ? //創(chuàng)建一個窗口對象
? ? ? ? Frame frame = new Frame("測試Window窗口");
? ? ? ? //指定窗口的位置和大小
? ? ? ? frame.setLocation(500,300);
? ? ? ? frame.setSize(500,300);
? ? ? ? frame.addWindowListener(new WindowAdapter() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void windowClosing(WindowEvent e) {
? ? ? ? ? ? ? ? System.exit(0);
? ? ? ? ? ? }
? ? ? ? });
? ? ? ? //設(shè)置窗口可見
? ? ? ? frame.setVisible(true);
? ? }
}常見監(jiān)聽器測試:

代碼:
import java.awt.*;
import java.awt.event.*;
public class ListenerDemo {
? ? public static void main(String[] args) {
? ? ? ? Frame frame = new Frame();
? ? ? ? Choice nameChooser = new Choice();
? ? ? ? nameChooser.add("Red");
? ? ? ? nameChooser.add("Yellow");
? ? ? ? nameChooser.add("Blue");
? ? ? ? //下拉選擇框添加ItemListener 監(jiān)聽條目變化
? ? ? ? nameChooser.addItemListener(new ItemListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void itemStateChanged(ItemEvent e) {
? ? ? ? ? ? ? ? Object item = e.getItem();
? ? ? ? ? ? ? ? System.out.println("當(dāng)前所選條目為:"+item);
? ? ? ? ? ? }
? ? ? ? });
? ? ? ? TextField tf = new TextField(30);
? ? ? ? tf.addTextListener(new TextListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void textValueChanged(TextEvent e) {
? ? ? ? ? ? ? ? String s = tf.getText();
? ? ? ? ? ? ? ? System.out.println("文本框內(nèi)容為:"+s);
? ? ? ? ? ? }
? ? ? ? });
? ? ? ? frame.add(nameChooser,BorderLayout.WEST);
? ? ? ? frame.add(tf);
? ? ? ? frame.addWindowListener(new WindowAdapter() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void windowClosing(WindowEvent e) {
? ? ? ? ? ? ? ? System.exit(0);
? ? ? ? ? ? }
? ? ? ? });
? ? ? ? frame.pack();
? ? ? ? frame.setVisible(true);
? ? }
}6.開發(fā)一個簡單計算器
FrameNORTH區(qū)域放置TextField組件,將指定為4行5列GridLayout布局管理器的Panel放置于Frame中部區(qū)域,其中填充操作符和操作數(shù)按鈕。
按鈕觸發(fā)事件源,對按鈕添加ActionListener注冊監(jiān)聽。自定義NumListener(操作數(shù)監(jiān)聽類)、OperatListener(操作符監(jiān)聽類)、EqualListener(’=‘符監(jiān)聽類)和匿名內(nèi)部類(如b[11]’-'符監(jiān)聽類)分情況對按鈕事件進(jìn)行監(jiān)聽并響應(yīng)。
注意:整數(shù)、浮點(diǎn)、負(fù)數(shù)以及連續(xù)運(yùn)算均可以。

代碼:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import static java.awt.Color.blue;
public class Calculation {
? ? //操作數(shù)
? ? double x,y;
? ? String op;
? ? boolean flag;
? ? Frame frame = new Frame("智子的計算器!");
? ? TextField tf = new TextField(30);
? ? Button[] b = new Button[20];
? ? public void init(){
? ? ? ? //北部區(qū)域放置文本框
? ? ? ? frame.add(tf,BorderLayout.NORTH);
? ? ? ? Panel panel = new Panel();
? ? ? ? panel.setLayout(new GridLayout(4,5,2,2));
? ? ? ? //設(shè)置按鈕
? ? ? ? String s = "+-*/%";
? ? ? ? for(int i=0;i<10;i++) {//運(yùn)算數(shù)
? ? ? ? ? ? b[i] = new Button(i + "");
? ? ? ? ? ? b[i].setForeground(blue);
? ? ? ? }
? ? ? ? for(int i=0;i<5;i++) {//運(yùn)算符
? ? ? ? ? ? b[i+10]=new Button(s.charAt(i)+"");
? ? ? ? ? ? b[i+10].setForeground(blue);
? ? ? ? }
? ? ? ? String[] t = {"sqrt","^2","^3","=","."};
? ? ? ? for(int i=0;i<5;i++){
? ? ? ? ? ? b[i+15]=new Button(t[i]);
? ? ? ? ? ? b[i+15].setForeground(blue);
? ? ? ? }
? ? ? ? //按鈕注冊監(jiān)聽
? ? ? ? for (int i = 0; i < 10; i++) {//操作數(shù)注冊監(jiān)聽
? ? ? ? ? ? b[i].addActionListener(new NumListener());
? ? ? ? }
? ? ? ? for (int i = 10; i < 18; i++) {//操作符注冊監(jiān)聽
? ? ? ? ? ? if(i==11) continue;
? ? ? ? ? ? b[i].addActionListener(new OperatListener());
? ? ? ? }
? ? ? ? b[11].addActionListener(new ActionListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void actionPerformed(ActionEvent e) {
? ? ? ? ? ? ? ? if(!flag){
? ? ? ? ? ? ? ? ? ? tf.setText("-");
? ? ? ? ? ? ? ? ? ? flag = true;
? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? x = Double.parseDouble(tf.getText());
? ? ? ? ? ? ? ? ? ? op = e.getActionCommand();
? ? ? ? ? ? ? ? ? ? flag = false;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? });
? ? ? ? //“=”注冊監(jiān)聽
? ? ? ? b[18].addActionListener(new EqualListener());
? ? ? ? //“back”注冊監(jiān)聽
? ? ? ? b[19].addActionListener(new NumListener());
? ? ? ? //將按鈕加入panel
? ? ? ? for (int i = 0; i < 20; i++) {
? ? ? ? ? ? panel.add(b[i]);
? ? ? ? }
? ? ? ? //設(shè)置中部按鈕
? ? ? ? frame.add(panel);
? ? ? ? //窗口監(jiān)聽器 注冊監(jiān)聽
? ? ? ? frame.addWindowListener(new WindowAdapter() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void windowClosing(WindowEvent e) {
? ? ? ? ? ? ? ? System.exit(0);
? ? ? ? ? ? }
? ? ? ? });
? ? ? ? //設(shè)置窗口最優(yōu)并可見
? ? ? ? frame.pack();
? ? ? ? frame.setVisible(true);
? ? }
? ? //數(shù)字按鈕監(jiān)聽器類
? ? public class NumListener implements ActionListener{
? ? ? ? @Override
? ? ? ? public void actionPerformed(ActionEvent e) {
? ? ? ? ? ? String t = e.getActionCommand();
? ? ? ? ? ? String s = tf.getText();
? ? ? ? ? ? if(flag==false)
? ? ? ? ? ? ? ? tf.setText(t);
? ? ? ? ? ? else
? ? ? ? ? ? ? ? tf.setText(s+t);
? ? ? ? ? ? flag = true;
? ? ? ? }
? ? }
? ? //操作符按鈕監(jiān)聽器類
? ? public class OperatListener implements ActionListener{
? ? ? ? @Override
? ? ? ? public void actionPerformed(ActionEvent e) {
? ? ? ? ? ? x = Double.parseDouble(tf.getText());
? ? ? ? ? ? op = e.getActionCommand();
? ? ? ? ? ? flag = false;
? ? ? ? }
? ? }
? ? //等號按鈕監(jiān)聽器類
? ? public class EqualListener implements ActionListener{
? ? ? ? @Override
? ? ? ? public void actionPerformed(ActionEvent e) {
? ? ? ? ? ? y = Double.parseDouble(tf.getText());
? ? ? ? ? ? flag = true;
? ? ? ? ? ? switch(op){
? ? ? ? ? ? ? ? case "+":tf.setText(x+y+"");
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case "-":tf.setText(x-y+"");
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case "*":tf.setText(x*y+"");
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case "/":
? ? ? ? ? ? ? ? ? ? if(y!=0)
? ? ? ? ? ? ? ? ? ? ? ? tf.setText(x/y+"");
? ? ? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? ? ? ? ? tf.setText("inf");
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case "%":tf.setText(x%y+"");
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case "sqrt":tf.setText((int)Math.sqrt(x)+"");
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case "^2":tf.setText(y*y+"");
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case "^3":tf.setText(y*y*y+"");
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? public static void main(String[] args) {
? ? ? ? new Calculation().init();
? ? }
}
到此這篇關(guān)于Java圖形化界面編程介紹的文章就介紹到這了,更多相關(guān)Java圖形化界面編程內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java反射學(xué)習(xí) getClass()函數(shù)應(yīng)用
所謂反射,可以理解為在運(yùn)行時期獲取對象類型信息的操作,本文將詳細(xì)介紹,需要的朋友可以參考下2012-12-12
關(guān)于使用Lambda表達(dá)式簡化Comparator的使用問題
這篇文章主要介紹了關(guān)于使用Lambda表達(dá)式簡化Comparator的使用問題,文中圖文講解了Comparator對象的方法,需要的朋友可以參考下2023-04-04
Java中 % 與Math.floorMod() 區(qū)別詳解
這篇文章主要介紹了Java中 % 與Math.floorMod() 區(qū)別詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08

