?? 《java就業(yè)培訓(xùn)教程》_張孝祥_書內(nèi)源碼_09.txt
字號:
《Java就業(yè)培訓(xùn)教程》 作者:張孝祥 書中源碼
《Java就業(yè)培訓(xùn)教程》P316源碼
程序清單:TestStopWatch.java
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.text.SimpleDateFormat;
class StopWatch extends Canvas implements Runnable
{
private long startTime = 0;
private long endTime = 0;
private boolean bStart = false;
public StopWatch()
{
enableEvents(AWTEvent.MOUSE_EVENT_MASK);
setSize(80,30);
}
protected void processMouseEvent(MouseEvent e)
{
if(e.getID() == MouseEvent.MOUSE_PRESSED)
{
/*鼠標(biāo)按下時(shí),啟動(dòng)計(jì)時(shí)線程,并讓起始時(shí)間變量和終止時(shí)間變量都等于當(dāng)前時(shí)間*/
bStart = true;
startTime = endTime = System.currentTimeMillis();
repaint();
new Thread(this).start();
}
else if(e.getID() == MouseEvent.MOUSE_RELEASED)
{
/*鼠標(biāo)釋放時(shí),終止計(jì)時(shí)線程,并重繪窗口表面上的內(nèi)容*/
bStart = false;
repaint();
}
super.processMouseEvent(e);
}
public void paint(Graphics g)
{
/*時(shí)間值的小時(shí)、分鐘、秒、都用兩位數(shù)字顯示,
不足兩位的部分前面加0,即"HH:mm:ss"這種的格式。*/
SimpleDateFormat sdf= new SimpleDateFormat("HH:mm:ss");
/*最剛開始編寫這個(gè)程序的時(shí)候,直接使用elapsedTime.setTime(endTime-startTime);
語句設(shè)置elapsedTime時(shí)間對象的數(shù)字值,從運(yùn)行結(jié)果上發(fā)現(xiàn),即使endTime-startTime等于0,
但elapsedTime顯示的時(shí)間卻不是"00:00:00",而是"08:00:00"。我們曾經(jīng)講過,時(shí)間在計(jì)算機(jī)
內(nèi)存中也是用一個(gè)長整數(shù)表示的,在這里,我們又發(fā)現(xiàn),即使這個(gè)內(nèi)存中的長整數(shù)等于0時(shí),由于
Date類考慮了本地時(shí)區(qū)問題,所以,其表示的時(shí)間就不一定為"零點(diǎn):零分:零秒"。這里不需要
考慮時(shí)區(qū)問題,只是借助Date類來幫我們生成"HH:mm:ss"這種時(shí)間表示格式。明白這個(gè)問題后,
我們就不難想像出,可以先求出顯示時(shí)間為"00:00:00"的時(shí)間對象在內(nèi)存中對應(yīng)的那個(gè)長整數(shù),
然后在這個(gè)基礎(chǔ)上加上計(jì)時(shí)器所記下的時(shí)間值,最后就可以顯示出我們想要的結(jié)果。*/
Date elapsedTime =null;
try
{
elapsedTime= sdf.parse("00:00:00");
}catch(Exception e){}
elapsedTime.setTime(endTime - startTime +
elapsedTime.getTime());
String display = sdf.format(elapsedTime);
g.drawRect(0,0,78,28);
g.fill3DRect(2,2,75,25,true);
g.setColor(Color.WHITE);
g.drawString(display,10,20);
}
public void run()
{
while(bStart)
{
try
{
Thread.sleep(500);
}catch(Exception e){e.printStackTrace();}
endTime = System.currentTimeMillis();
repaint();
}
}
}
public class TestStopWatch
{
public static void main(String [] args)
{
Frame f =new Frame("StopWatch");
f.add(new StopWatch());
f.setSize(200,200);
f.setVisible(true);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
}
《Java就業(yè)培訓(xùn)教程》P319源碼
程序清單:TestCheckbox.java
import java.awt.*;
import java.awt.event.*;
public class TestCheckbox
{
Checkbox cb1=new Checkbox("你喜歡我嗎?",true);
CheckboxGroup cbg=new CheckboxGroup();
Checkbox cb2=new Checkbox("喜歡",cbg,true);
Checkbox cb3=new Checkbox("不喜歡",cbg,false);
public void init()
{
Frame f=new Frame("TestCheckBox");
//創(chuàng)建FlowLayout布局管理器,關(guān)于布局管理器,本章后面有專門的講解,
看不明白//的讀者暫時(shí)可以不去下面兩句代碼的作用。
FlowLayout fl=new FlowLayout();
f.setLayout(fl);
f.add(cb1);
f.add(cb2);
f.add(cb3);
cb1.addItemListener(new CbItemListener());
cb2.addItemListener(new CbItemListener());
cb3.addItemListener(new CbItemListener());
f.setBounds(0,0,300,100);
f.setVisible(true);
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
class CbItemListener implements ItemListener
{
public void itemStateChanged(ItemEvent e)
{
Checkbox cb = (Checkbox)e.getItemSelectable();
if(cb.getLabel().equals("你喜歡我嗎?"))
{
if(cb.getState() == true)
System.out.println("我很高興");
else
System.out.println("我很傷心");
}
/*else if(cb.getLabel().equals("喜歡"))
{
if(e.getStateChange() == ItemEvent.SELECTED)
System.out.println("我也喜歡你");
else
System.out.println("我也不喜歡你");
}*/
else
{
Checkbox cbx =cbg.getSelectedCheckbox();
if(cbx != null)
System.out.println(cbx.getLabel());
}
}
}
public static void main(String[] args)
{
new TestCheckbox().init();
}
}
《Java就業(yè)培訓(xùn)教程》P321源碼
程序清單:TestChoice.java
import java.awt.*;
import java.awt.event.*;
public class TestChoice
{
Choice ch=new Choice(); //創(chuàng)建Choice對象
TestChoice()
{
ch.add("choice1"); //用add方法向列表里加入選項(xiàng)
ch.add("choice2"); //用add方法向列表里加入選項(xiàng)
ch.add("choice3"); //用add方法向列表里加入選項(xiàng)
FlowLayout fl=new FlowLayout();
Frame f=new Frame("TestChoice");
f.setLayout(fl);
f.add(ch); //把列表加入到窗口
f.setBounds(0,0,200,100);
f.setVisible(true);
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public static void main(String[] args)
{
new TestChoice();
}
}
《Java就業(yè)培訓(xùn)教程》P323源碼
程序清單:TestMenuBar.java
import java.awt.*;
import java.awt.event.*;
public class TestMenuBar
{
MenuBar menubar=new MenuBar(); //創(chuàng)建菜單條對象
Menu fileM=new Menu("File"); //創(chuàng)建各菜單
Menu editM=new Menu("Edit"); //創(chuàng)建各菜單
Menu toolsM=new Menu("Tools"); //創(chuàng)建各菜單
Menu helpM=new Menu("Help"); //創(chuàng)建各菜單
MenuItem fileMI1=new MenuItem("New"); //創(chuàng)建各菜單項(xiàng)
MenuItem fileMI2=new MenuItem("Open"); //創(chuàng)建各菜單項(xiàng)
MenuItem fileMI3=new MenuItem("Save"); //創(chuàng)建各菜單項(xiàng)
CheckboxMenuItem fileMI5=new CheckboxMenuItem("Quit",true);
//創(chuàng)建各菜單項(xiàng)
Menu filePrint = new Menu("print");//創(chuàng)建子菜單
MenuItem printM1 = new MenuItem("preview");
MenuItem printM2 = new MenuItem("setting");
TestMenuBar()
{
FlowLayout fl=new FlowLayout();
Frame f=new Frame("TestMenuBar");
f.setLayout(fl);
menubar.add(fileM); //將菜單加入菜單條
menubar.add(editM);
menubar.add(toolsM);
menubar.add(helpM);
fileM.add(fileMI1); //將菜單項(xiàng)加入file菜單中
fileM.add(fileMI2);
fileM.add(fileMI3);
filePrint.add(printM1);//將菜單項(xiàng)加入print菜單中
filePrint.add(printM2);
fileM.add(filePrint);
//將print菜單作為一個(gè)菜單項(xiàng)加入file菜單中
fileM.addSeparator(); //將一條分割線加入菜單中
fileM.add(fileMI5); //將菜單項(xiàng)加入菜單中
f.setMenuBar(menubar); //把整個(gè)菜單系統(tǒng)顯示在窗口中
f.setBounds(0,0,250,200);
f.setVisible(true);
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public static void main(String[] args)
{
new TestMenuBar();
}
}
《Java就業(yè)培訓(xùn)教程》P327源碼
程序清單:TestDialog.java
import java.awt.*;
import java.awt.event.*;
public class TestDialog
{
TextField tf = new TextField(10);
Button b1=new Button("模態(tài)顯示");
Button b2=new Button("非模態(tài)顯示");
Frame f=new Frame("TestDialog");
Button b3=new Button("確定");
Dialog dlg = new Dialog(f, "Dialog Title", true);
FlowLayout fl=new FlowLayout();
TestDialog()
{
f.setLayout(fl);
f.add(tf);
f.add(b1);
f.add(b2);
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
dlg.setModal(true);
dlg.setVisible(true);
tf.setText("www.it315.org");
}
});
b2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
dlg.setModal(false);
dlg.setVisible(true);
tf.setText("www.it315.org");
}
});
f.setBounds(0,0,400,200);
f.setVisible(true);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
dlg.setLayout(fl);
dlg.add(b3);
b3.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
dlg.dispose();
}
});
dlg.setBounds(0,0,200,150);
}
public static void main(String[] args)
{
new TestDialog();
}
}
《Java就業(yè)培訓(xùn)教程》P330源碼
程序清單:TestPane.java
import java.awt.*;
import java.awt.event.*;
public class TestPane
{
TestPane()
{
Frame f=new Frame("TestDialog");
ScrollPane sp = new ScrollPane();
TextArea ta = new TextArea("",10,50,TextArea.SCROLLBARS_NONE);
sp.add(ta);
f.add(sp);
f.setSize(200,200);
f.setVisible(true);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public static void main(String[] args)
{
new TestPane();
}
}
《Java就業(yè)培訓(xùn)教程》P336源碼
程序清單:TestCardLayout.java
import java.awt.*;
import java.awt.event.*;
public class TestCardLayout
{
CardLayout cl = new CardLayout();
Panel plCenter = new Panel();
public static void main(String [] args)
{
new TestCardLayout().init();
}
public void init()
{
Frame f=new Frame("布局管理器");
Panel plWest = new Panel();
f.add(plWest,"West");
f.add(plCenter);
plWest.setLayout(new GridLayout(3,1));
Button btnPrev = new Button("prev");
plWest.add(btnPrev);
Button btnNext = new Button("next");
plWest.add(btnNext);
Button btnThree = new Button("three");
plWest.add(btnThree);
plCenter.setLayout(cl);
plCenter.add(new Button("One"),"1");
plCenter.add(new Button("two"),"2");
plCenter.add(new Button("three"),"3");
plCenter.add(new Button("four"),"4");
plCenter.add(new Button("five"),"5");
class MyActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("prev"))
cl.previous(plCenter);
else if(e.getActionCommand().equals("next"))
cl.next(plCenter);
else if(e.getActionCommand().equals("three"))
cl.show(plCenter,"3");
}
}
MyActionListener ma = new MyActionListener();
btnPrev.addActionListener(ma);
btnNext.addActionListener(ma);
btnThree.addActionListener(ma);
f.setSize(300,300);
f.setVisible(true);
}
}
《Java就業(yè)培訓(xùn)教程》P343源碼
程序清單:Calculator.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator implements ActionListener
{
JFrame jf = new JFrame("Calculator");
JTextField tf = new JTextField();
public void init()
{
Container c = jf.getContentPane();
tf.setHorizontalAlignment(JTextField.RIGHT);
c.add(tf,"North");
JPanel pnl=new JPanel();
c.add(pnl,"Center");
pnl.setLayout(new GridLayout(4,4));
JButton b=new JButton("1");
b.addActionListener(this);
pnl.add(b);
b=new JButton("2");
b.addActionListener(this);
pnl.add(b);
b=new JButton("3");
b.addActionListener(this);
pnl.add(b);
b=new JButton("+");
b.addActionListener(this);
pnl.add(b);
b=new JButton("4");
b.addActionListener(this);
pnl.add(b);
b=new JButton("5");
b.addActionListener(this);
pnl.add(b);
b=new JButton("6");
b.addActionListener(this);
pnl.add(b);
b=new JButton("-");
b.addActionListener(this);
pnl.add(b);
b=new JButton("7");
b.addActionListener(this);
pnl.add(b);
b=new JButton("8");
b.addActionListener(this);
pnl.add(b);
b=new JButton("9");
b.addActionListener(this);
pnl.add(b);
b=new JButton("*");
b.addActionListener(this);
pnl.add(b);
b=new JButton("0");
b.addActionListener(this);
pnl.add(b);
b=new JButton(".");
b.addActionListener(this);
pnl.add(b);
b=new JButton("=");
b.addActionListener(this);
pnl.add(b);
b=new JButton("\\");
b.addActionListener(this);
pnl.add(b);
jf.setSize(200,300);
jf.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
tf.setText(tf.getText()+e.getActionCommand());
}
public static void main(String [] args)
{
new Calculator().init();
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -