[翻译]Java Swing(2)

2

面向对象语言的一个好处在于你可以升级部分程序而不必重写其他代码。你可以使用大部分Swing组件来作为AWT组件的替代品。

None.gif // ToolbarFrame1.java
None.gif
import java.awt. * ;
None.gif
import java.awt.event. * ;
None.gif
None.gif
public class ToolbarFrame1 extends Frame
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
privateButtoncutBtn,copyBtn,pasteBtn;
InBlock.gif
publicToolbarFrame1()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
super(“Toolbar例子(AWT)”);
InBlock.gif
this.setSize(450,250);
ExpandedSubBlockStart.gifContractedSubBlock.gif
this.addWindowListener(newWindowAdapter()dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif
publicvoidwindowClosing(WindowEvente)dot.gif{
InBlock.gifSystem.exit(
0);
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

ExpandedSubBlockStart.gifContractedSubBlock.gifActionListenerlisterner
=newActionListener()dot.gif{
InBlock.gifPublic
voidactionPerformed(ActionEvente)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifSystem.out.println(e.getActionCommand());
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}
;
InBlock.gifPaneltoolbar
=newPanel();
InBlock.giftoolbar.setLayout(
newFlowLayout(FlowLayout.LEFT));
InBlock.gif
InBlock.gifcutButton
=newButton("Cut");
InBlock.gifcutButton.addActionListener(printListener);
InBlock.giftoolbar.add(cutButton);
InBlock.gif
InBlock.gifcopyButton
=newButton("Copy");
InBlock.gifcopyButton.addActionListener(printListener);
InBlock.giftoolbar.add(copyButton);
InBlock.gif
InBlock.gifpasteButton
=newButton("Paste");
InBlock.gifpasteButton.addActionListener(printListener);
InBlock.giftoolbar.add(pasteButton);
InBlock.gif
InBlock.gif
//The"preferred"BorderLayoutaddcall
InBlock.gif
add(toolbar,BorderLayout.NORTH);
ExpandedBlockEnd.gif}

ExpandedBlockStart.gifContractedBlock.gif
public static void main(Stringargs[]) dot.gif {
InBlock.gifToolbarFrame1tf1
=newToolbarFrame1();
InBlock.giftf1.setVisible(
true);
ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif}
None.gif

None.gif // ToolbarFrame2.java
None.gif
// TheSwing-ifiedbuttonexample
None.gif
//
None.gif
import java.awt. * ;
None.gif
import java.awt.event. * ;
None.gif
import javax.swing. * ;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
public class ToolbarFrame2 extends Frame dot.gif {
InBlock.gif
InBlock.gif
//Thistime,let'suseJButtons!
InBlock.gif
JButtoncutButton,copyButton,pasteButton;
InBlock.gifJButtonjavaButton,macButton,motifButton,winButton;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
publicToolbarFrame2()dot.gif{
InBlock.gif
super("ToolbarExample(Swing)");
InBlock.gifsetSize(
450,250);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifaddWindowListener(
newWindowAdapter()dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif
publicvoidwindowClosing(WindowEvente)dot.gif{
InBlock.gifSystem.exit(
0);
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}
);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifActionListenerprintListener
=newActionListener()dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif
publicvoidactionPerformed(ActionEventae)dot.gif{
InBlock.gifSystem.out.println(ae.getActionCommand());
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}
;
InBlock.gif
InBlock.gif
//JPanelworkssimilarlytoPanel,sowe'lluseit.
InBlock.gif
JPaneltoolbar=newJPanel();
InBlock.giftoolbar.setLayout(
newFlowLayout(FlowLayout.LEFT));
InBlock.gif
InBlock.gifcutButton
=newJButton("Cut");
InBlock.gifcutButton.addActionListener(printListener);
InBlock.giftoolbar.add(cutButton);
InBlock.gif
InBlock.gifcopyButton
=newJButton("Copy");
InBlock.gifcopyButton.addActionListener(printListener);
InBlock.giftoolbar.add(copyButton);
InBlock.gif
InBlock.gifpasteButton
=newJButton("Paste");
InBlock.gifpasteButton.addActionListener(printListener);
InBlock.giftoolbar.add(pasteButton);
InBlock.gif
InBlock.gifadd(toolbar,BorderLayout.NORTH);
InBlock.gif
InBlock.gif
//AddtheL&Fcontrols.
InBlock.gif
JPanellnfPanel=newJPanel();
InBlock.gifLnFListenerlnfListener
=newLnFListener(this);
InBlock.gifmacButton
=newJButton("Mac");
InBlock.gifmacButton.addActionListener(lnfListener);
InBlock.giflnfPanel.add(macButton);
InBlock.gifjavaButton
=newJButton("Metal");
InBlock.gifjavaButton.addActionListener(lnfListener);
InBlock.giflnfPanel.add(javaButton);
InBlock.gifmotifButton
=newJButton("Motif");
InBlock.gifmotifButton.addActionListener(lnfListener);
InBlock.giflnfPanel.add(motifButton);
InBlock.gifwinButton
=newJButton("Windows");
InBlock.gifwinButton.addActionListener(lnfListener);
InBlock.giflnfPanel.add(winButton);
InBlock.gifadd(lnfPanel,BorderLayout.SOUTH);
ExpandedSubBlockEnd.gif}

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
publicstaticvoidmain(Stringargs[])dot.gif{
InBlock.gifToolbarFrame2tf2
=newToolbarFrame2();
InBlock.giftf2.setVisible(
true);
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif
//
None.gif

None.gif LnFListener.java
None.gif
// AlistenerthatcanchangetheL&FofaframebasedontheactionCommandofan
None.gif
// ActionEventobject.SupportedL&Fsare:Mac,Metal,Motif,andWindows.Notall
None.gif
// L&Fswillbeavailableonagivenmachine.Notably,theMacandWindowsL&Fswork
None.gif
// onlyontheirspecificplatforms.
None.gif
import java.awt. * ;
None.gif
import java.awt.event. * ;
None.gif
import javax.swing. * ;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
public class LnFListener implements ActionListener dot.gif {
InBlock.gifFrameframe;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
publicLnFListener(Framef)dot.gif{
InBlock.gifframe
=f;
ExpandedSubBlockEnd.gif}

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
publicvoidactionPerformed(ActionEvente)dot.gif{
InBlock.gifStringlnfName
=null;
ExpandedSubBlockStart.gifContractedSubBlock.gif
if(e.getActionCommand().equals("Mac"))dot.gif{
InBlock.giflnfName
="com.apple.mrj.swing.MacLookAndFeel";
ExpandedSubBlockStart.gifContractedSubBlock.gif}
elseif(e.getActionCommand().equals("Metal"))dot.gif{
InBlock.giflnfName
="javax.swing.plaf.metal.MetalLookAndFeel";
ExpandedSubBlockStart.gifContractedSubBlock.gif}
elseif(e.getActionCommand().equals("Motif"))dot.gif{
InBlock.giflnfName
="com.sun.java.swing.plaf.motif.MotifLookAndFeel";
ExpandedSubBlockStart.gifContractedSubBlock.gif}
elseif(e.getActionCommand().equals("Windows"))dot.gif{
InBlock.giflnfName
="com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
ExpandedSubBlockStart.gifContractedSubBlock.gif}
elsedot.gif{
InBlock.gifSystem.err.println(
"UnrecognizedL&Frequestaction:"+
InBlock.gife.getActionCommand());
InBlock.gif
return;
ExpandedSubBlockEnd.gif}

ExpandedSubBlockStart.gifContractedSubBlock.gif
trydot.gif{
InBlock.gifUIManager.setLookAndFeel(lnfName);
InBlock.gifSwingUtilities.updateComponentTreeUI(frame);
ExpandedSubBlockEnd.gif}

ExpandedSubBlockStart.gifContractedSubBlock.gif
catch(UnsupportedLookAndFeelExceptionex1)dot.gif{
InBlock.gifSystem.err.println(
"UnsupportedLookAndFeel:"+lnfName);
ExpandedSubBlockEnd.gif}

ExpandedSubBlockStart.gifContractedSubBlock.gif
catch(ClassNotFoundExceptionex2)dot.gif{
InBlock.gifSystem.err.println(
"LookAndFeelclassnotfound:"+lnfName);
ExpandedSubBlockEnd.gif}

ExpandedSubBlockStart.gifContractedSubBlock.gif
catch(InstantiationExceptionex3)dot.gif{
InBlock.gifSystem.err.println(
"CouldnotloadLookAndFeel:"+lnfName);
ExpandedSubBlockEnd.gif}

ExpandedSubBlockStart.gifContractedSubBlock.gif
catch(IllegalAccessExceptionex4)dot.gif{
InBlock.gifSystem.err.println(
"CannotuseLookAndFeel:"+lnfName);
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif

None.gif // ToolbarFrame4.java
None.gif
// TheSwing-ifiedbuttonexample.Thebuttonsinthistoolbarallcarryimages
None.gif
// butnotext.
None.gif
//
None.gif
import java.awt. * ;
None.gif
import java.awt.event. * ;
None.gif
import javax.swing. * ;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
public class ToolbarFrame4 extends Frame dot.gif {
InBlock.gif
InBlock.gifJButtoncutButton,copyButton,pasteButton;
InBlock.gifJButtonjavaButton,macButton,motifButton,winButton;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
publicToolbarFrame4()dot.gif{
InBlock.gif
super("ToolbarExample(Swingnotext)");
InBlock.gifsetSize(
450,250);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifaddWindowListener(
newWindowAdapter()dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif
publicvoidwindowClosing(WindowEvente)dot.gif{
InBlock.gifSystem.exit(
0);
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}
);
InBlock.gif
InBlock.gif
//JPanelworksmuchlikePaneldoes,sowe'lluseit.
InBlock.gif
JPaneltoolbar=newJPanel();
InBlock.giftoolbar.setLayout(
newFlowLayout(FlowLayout.LEFT));
InBlock.gif
InBlock.gifCCPHandlerhandler
=newCCPHandler();
InBlock.gif
InBlock.gifcutButton
=newJButton(newImageIcon("cut.gif"));
InBlock.gifcutButton.setActionCommand(CCPHandler.CUT);
InBlock.gifcutButton.addActionListener(handler);
InBlock.giftoolbar.add(cutButton);
InBlock.gif
InBlock.gifcopyButton
=newJButton(newImageIcon("copy.gif"));
InBlock.gifcopyButton.setActionCommand(CCPHandler.COPY);
InBlock.gifcopyButton.addActionListener(handler);
InBlock.giftoolbar.add(copyButton);
InBlock.gif
InBlock.gifpasteButton
=newJButton(newImageIcon("paste.gif"));
InBlock.gifpasteButton.setActionCommand(CCPHandler.PASTE);
InBlock.gifpasteButton.addActionListener(handler);
InBlock.giftoolbar.add(pasteButton);
InBlock.gif
InBlock.gifadd(toolbar,BorderLayout.NORTH);
InBlock.gif
InBlock.gif
//AddtheL&Fcontrols.
InBlock.gif
JPanellnfPanel=newJPanel();
InBlock.gifLnFListenerlnfListener
=newLnFListener(this);
InBlock.gifmacButton
=newJButton("Mac");
InBlock.gifmacButton.addActionListener(lnfListener);
InBlock.giflnfPanel.add(macButton);
InBlock.gifjavaButton
=newJButton("Metal");
InBlock.gifjavaButton.addActionListener(lnfListener);
InBlock.giflnfPanel.add(javaButton);
InBlock.gifmotifButton
=newJButton("Motif");
InBlock.gifmotifButton.addActionListener(lnfListener);
InBlock.giflnfPanel.add(motifButton);
InBlock.gifwinButton
=newJButton("Windows");
InBlock.gifwinButton.addActionListener(lnfListener);
InBlock.giflnfPanel.add(winButton);
InBlock.gifadd(lnfPanel,BorderLayout.SOUTH);
ExpandedSubBlockEnd.gif}

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
publicstaticvoidmain(Stringargs[])dot.gif{
InBlock.gifToolbarFrame4tf4
=newToolbarFrame4();
InBlock.giftf4.setVisible(
true);
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif

None.gif // CCPHandler.java
None.gif
// ACut,Copy,andPasteeventhandler.Nothingtoofancy,justdefinesome
None.gif
// constantsthatcanbeusedtosettheactionCommandsonbuttons.
None.gif
//
None.gif
import java.awt.event. * ;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
public class CCPHandler implements ActionListener dot.gif {
InBlock.gif
InBlock.gif
publicfinalstaticStringCUT="cut";
InBlock.gif
publicfinalstaticStringCOPY="copy";
InBlock.gif
publicfinalstaticStringPASTE="paste";
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
publicvoidactionPerformed(ActionEvente)dot.gif{
InBlock.gifStringcommand
=e.getActionCommand();
ExpandedSubBlockStart.gifContractedSubBlock.gif
if(command==CUT)dot.gif{//Wecandothissincewe'recomparingconstants.
InBlock.gif
System.out.println("GotCutevent");
ExpandedSubBlockEnd.gif}

ExpandedSubBlockStart.gifContractedSubBlock.gif
elseif(command==COPY)dot.gif{
InBlock.gifSystem.out.println(
"GotCopyevent");
ExpandedSubBlockEnd.gif}

ExpandedSubBlockStart.gifContractedSubBlock.gif
elseif(command==PASTE)dot.gif{
InBlock.gifSystem.out.println(
"GotPasteevent");
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif

内部框架(Internal Frame)特点:1)和Frame对象功能一样,但被限制在父容器中。2)能被图标化 3)能被最大化 4)能用程序窗口的标准控制来关闭 5)能分层放置

None.gif
None.gif
// SimpleInternalFrame.java
None.gif
// Aquickdemonstrationofsettingupaninternalframeinanapplication
None.gif
//
None.gif
import java.awt. * ;
None.gif
import java.awt.event. * ;
None.gif
import javax.swing. * ;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
public class SimpleInternalFrame extends Frame dot.gif {
InBlock.gif
InBlock.gifJButtonopenButton,macButton,javaButton,motifButton,winButton;
InBlock.gifJLayeredPanedesktop;
InBlock.gifJInternalFrameinternalFrame;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
publicSimpleInternalFrame()dot.gif{
InBlock.gif
super("InternalFrameDemo");
InBlock.gifsetSize(
500,400);
InBlock.gifopenButton
=newJButton("Open");
InBlock.gifmacButton
=newJButton("Mac");
InBlock.gifjavaButton
=newJButton("Metal");
InBlock.gifmotifButton
=newJButton("Motif");
InBlock.gifwinButton
=newJButton("Windows");
InBlock.gifPanelp
=newPanel();
InBlock.gifp.add(openButton);
InBlock.gifp.add(macButton);
InBlock.gifp.add(javaButton);
InBlock.gifp.add(motifButton);
InBlock.gifp.add(winButton);
InBlock.gifadd(p,BorderLayout.SOUTH);
ExpandedSubBlockStart.gifContractedSubBlock.gifaddWindowListener(
newWindowAdapter()dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif
publicvoidwindowClosing(WindowEvente)dot.gif{
InBlock.gifSystem.exit(
0);
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}
);
InBlock.gifopenButton.addActionListener(
newOpenListener());
InBlock.gifLnFListenerlnf
=newLnFListener(this);
InBlock.gifmacButton.addActionListener(lnf);
InBlock.gifjavaButton.addActionListener(lnf);
InBlock.gifmotifButton.addActionListener(lnf);
InBlock.gifwinButton.addActionListener(lnf);
InBlock.gif
InBlock.gif
//Setupthelayeredpane.
InBlock.gif
desktop=newJDesktopPane();
InBlock.gifdesktop.setOpaque(
true);
InBlock.gifadd(desktop,BorderLayout.CENTER);
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
//AninnerclasstohandlepressesoftheOpenbutton
ExpandedSubBlockStart.gifContractedSubBlock.gif
classOpenListenerimplementsActionListenerdot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif
publicvoidactionPerformed(ActionEvente)dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif
if((internalFrame==null)||(internalFrame.isClosed()))dot.gif{
InBlock.gifinternalFrame
=newJInternalFrame("InternalFrame",
InBlock.gif
true,true,true,true);
InBlock.gifinternalFrame.setBounds(
50,50,200,100);
InBlock.gifdesktop.add(internalFrame,
newInteger(1));
InBlock.gifinternalFrame.setVisible(
true);
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
publicstaticvoidmain(Stringargs[])dot.gif{
InBlock.gifSimpleInternalFramesif
=newSimpleInternalFrame();
InBlock.gifsif.setVisible(
true);
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif
None.gif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值