第2章
面向对象语言的一个好处在于你可以升级部分程序而不必重写其他代码。你可以使用大部分Swing组件来作为AWT组件的替代品。
//
ToolbarFrame1.java
import java.awt. * ;
import java.awt.event. * ;
public class ToolbarFrame1 extends Frame
{
privateButtoncutBtn,copyBtn,pasteBtn;
publicToolbarFrame1()
{
super(“Toolbar例子(AWT)”);
this.setSize(450,250);
this.addWindowListener(newWindowAdapter(){
publicvoidwindowClosing(WindowEvente){
System.exit(0);
}
}
}
ActionListenerlisterner=newActionListener(){
PublicvoidactionPerformed(ActionEvente)
{
System.out.println(e.getActionCommand());
}
};
Paneltoolbar=newPanel();
toolbar.setLayout(newFlowLayout(FlowLayout.LEFT));
cutButton=newButton("Cut");
cutButton.addActionListener(printListener);
toolbar.add(cutButton);
copyButton=newButton("Copy");
copyButton.addActionListener(printListener);
toolbar.add(copyButton);
pasteButton=newButton("Paste");
pasteButton.addActionListener(printListener);
toolbar.add(pasteButton);
//The"preferred"BorderLayoutaddcall
add(toolbar,BorderLayout.NORTH);
}
public static void main(Stringargs[]) {
ToolbarFrame1tf1=newToolbarFrame1();
tf1.setVisible(true);
}
}
import java.awt. * ;
import java.awt.event. * ;
public class ToolbarFrame1 extends Frame
{
privateButtoncutBtn,copyBtn,pasteBtn;
publicToolbarFrame1()
{
super(“Toolbar例子(AWT)”);
this.setSize(450,250);
this.addWindowListener(newWindowAdapter(){
publicvoidwindowClosing(WindowEvente){
System.exit(0);
}
}
}
ActionListenerlisterner=newActionListener(){
PublicvoidactionPerformed(ActionEvente)
{
System.out.println(e.getActionCommand());
}
};
Paneltoolbar=newPanel();
toolbar.setLayout(newFlowLayout(FlowLayout.LEFT));
cutButton=newButton("Cut");
cutButton.addActionListener(printListener);
toolbar.add(cutButton);
copyButton=newButton("Copy");
copyButton.addActionListener(printListener);
toolbar.add(copyButton);
pasteButton=newButton("Paste");
pasteButton.addActionListener(printListener);
toolbar.add(pasteButton);
//The"preferred"BorderLayoutaddcall
add(toolbar,BorderLayout.NORTH);
}
public static void main(Stringargs[]) {
ToolbarFrame1tf1=newToolbarFrame1();
tf1.setVisible(true);
}
}
//
ToolbarFrame2.java
// TheSwing-ifiedbuttonexample
//
import java.awt. * ;
import java.awt.event. * ;
import javax.swing. * ;
public class ToolbarFrame2 extends Frame {
//Thistime,let'suseJButtons!
JButtoncutButton,copyButton,pasteButton;
JButtonjavaButton,macButton,motifButton,winButton;
publicToolbarFrame2(){
super("ToolbarExample(Swing)");
setSize(450,250);
addWindowListener(newWindowAdapter(){
publicvoidwindowClosing(WindowEvente){
System.exit(0);
}
});
ActionListenerprintListener=newActionListener(){
publicvoidactionPerformed(ActionEventae){
System.out.println(ae.getActionCommand());
}
};
//JPanelworkssimilarlytoPanel,sowe'lluseit.
JPaneltoolbar=newJPanel();
toolbar.setLayout(newFlowLayout(FlowLayout.LEFT));
cutButton=newJButton("Cut");
cutButton.addActionListener(printListener);
toolbar.add(cutButton);
copyButton=newJButton("Copy");
copyButton.addActionListener(printListener);
toolbar.add(copyButton);
pasteButton=newJButton("Paste");
pasteButton.addActionListener(printListener);
toolbar.add(pasteButton);
add(toolbar,BorderLayout.NORTH);
//AddtheL&Fcontrols.
JPanellnfPanel=newJPanel();
LnFListenerlnfListener=newLnFListener(this);
macButton=newJButton("Mac");
macButton.addActionListener(lnfListener);
lnfPanel.add(macButton);
javaButton=newJButton("Metal");
javaButton.addActionListener(lnfListener);
lnfPanel.add(javaButton);
motifButton=newJButton("Motif");
motifButton.addActionListener(lnfListener);
lnfPanel.add(motifButton);
winButton=newJButton("Windows");
winButton.addActionListener(lnfListener);
lnfPanel.add(winButton);
add(lnfPanel,BorderLayout.SOUTH);
}
publicstaticvoidmain(Stringargs[]){
ToolbarFrame2tf2=newToolbarFrame2();
tf2.setVisible(true);
}
}
//
// TheSwing-ifiedbuttonexample
//
import java.awt. * ;
import java.awt.event. * ;
import javax.swing. * ;
public class ToolbarFrame2 extends Frame {
//Thistime,let'suseJButtons!
JButtoncutButton,copyButton,pasteButton;
JButtonjavaButton,macButton,motifButton,winButton;
publicToolbarFrame2(){
super("ToolbarExample(Swing)");
setSize(450,250);
addWindowListener(newWindowAdapter(){
publicvoidwindowClosing(WindowEvente){
System.exit(0);
}
});
ActionListenerprintListener=newActionListener(){
publicvoidactionPerformed(ActionEventae){
System.out.println(ae.getActionCommand());
}
};
//JPanelworkssimilarlytoPanel,sowe'lluseit.
JPaneltoolbar=newJPanel();
toolbar.setLayout(newFlowLayout(FlowLayout.LEFT));
cutButton=newJButton("Cut");
cutButton.addActionListener(printListener);
toolbar.add(cutButton);
copyButton=newJButton("Copy");
copyButton.addActionListener(printListener);
toolbar.add(copyButton);
pasteButton=newJButton("Paste");
pasteButton.addActionListener(printListener);
toolbar.add(pasteButton);
add(toolbar,BorderLayout.NORTH);
//AddtheL&Fcontrols.
JPanellnfPanel=newJPanel();
LnFListenerlnfListener=newLnFListener(this);
macButton=newJButton("Mac");
macButton.addActionListener(lnfListener);
lnfPanel.add(macButton);
javaButton=newJButton("Metal");
javaButton.addActionListener(lnfListener);
lnfPanel.add(javaButton);
motifButton=newJButton("Motif");
motifButton.addActionListener(lnfListener);
lnfPanel.add(motifButton);
winButton=newJButton("Windows");
winButton.addActionListener(lnfListener);
lnfPanel.add(winButton);
add(lnfPanel,BorderLayout.SOUTH);
}
publicstaticvoidmain(Stringargs[]){
ToolbarFrame2tf2=newToolbarFrame2();
tf2.setVisible(true);
}
}
//
LnFListener.java
// AlistenerthatcanchangetheL&FofaframebasedontheactionCommandofan
// ActionEventobject.SupportedL&Fsare:Mac,Metal,Motif,andWindows.Notall
// L&Fswillbeavailableonagivenmachine.Notably,theMacandWindowsL&Fswork
// onlyontheirspecificplatforms.
import java.awt. * ;
import java.awt.event. * ;
import javax.swing. * ;
public class LnFListener implements ActionListener {
Frameframe;
publicLnFListener(Framef){
frame=f;
}
publicvoidactionPerformed(ActionEvente){
StringlnfName=null;
if(e.getActionCommand().equals("Mac")){
lnfName="com.apple.mrj.swing.MacLookAndFeel";
}elseif(e.getActionCommand().equals("Metal")){
lnfName="javax.swing.plaf.metal.MetalLookAndFeel";
}elseif(e.getActionCommand().equals("Motif")){
lnfName="com.sun.java.swing.plaf.motif.MotifLookAndFeel";
}elseif(e.getActionCommand().equals("Windows")){
lnfName="com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
}else{
System.err.println("UnrecognizedL&Frequestaction:"+
e.getActionCommand());
return;
}
try{
UIManager.setLookAndFeel(lnfName);
SwingUtilities.updateComponentTreeUI(frame);
}
catch(UnsupportedLookAndFeelExceptionex1){
System.err.println("UnsupportedLookAndFeel:"+lnfName);
}
catch(ClassNotFoundExceptionex2){
System.err.println("LookAndFeelclassnotfound:"+lnfName);
}
catch(InstantiationExceptionex3){
System.err.println("CouldnotloadLookAndFeel:"+lnfName);
}
catch(IllegalAccessExceptionex4){
System.err.println("CannotuseLookAndFeel:"+lnfName);
}
}
}
// AlistenerthatcanchangetheL&FofaframebasedontheactionCommandofan
// ActionEventobject.SupportedL&Fsare:Mac,Metal,Motif,andWindows.Notall
// L&Fswillbeavailableonagivenmachine.Notably,theMacandWindowsL&Fswork
// onlyontheirspecificplatforms.
import java.awt. * ;
import java.awt.event. * ;
import javax.swing. * ;
public class LnFListener implements ActionListener {
Frameframe;
publicLnFListener(Framef){
frame=f;
}
publicvoidactionPerformed(ActionEvente){
StringlnfName=null;
if(e.getActionCommand().equals("Mac")){
lnfName="com.apple.mrj.swing.MacLookAndFeel";
}elseif(e.getActionCommand().equals("Metal")){
lnfName="javax.swing.plaf.metal.MetalLookAndFeel";
}elseif(e.getActionCommand().equals("Motif")){
lnfName="com.sun.java.swing.plaf.motif.MotifLookAndFeel";
}elseif(e.getActionCommand().equals("Windows")){
lnfName="com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
}else{
System.err.println("UnrecognizedL&Frequestaction:"+
e.getActionCommand());
return;
}
try{
UIManager.setLookAndFeel(lnfName);
SwingUtilities.updateComponentTreeUI(frame);
}
catch(UnsupportedLookAndFeelExceptionex1){
System.err.println("UnsupportedLookAndFeel:"+lnfName);
}
catch(ClassNotFoundExceptionex2){
System.err.println("LookAndFeelclassnotfound:"+lnfName);
}
catch(InstantiationExceptionex3){
System.err.println("CouldnotloadLookAndFeel:"+lnfName);
}
catch(IllegalAccessExceptionex4){
System.err.println("CannotuseLookAndFeel:"+lnfName);
}
}
}
//
ToolbarFrame4.java
// TheSwing-ifiedbuttonexample.Thebuttonsinthistoolbarallcarryimages
// butnotext.
//
import java.awt. * ;
import java.awt.event. * ;
import javax.swing. * ;
public class ToolbarFrame4 extends Frame {
JButtoncutButton,copyButton,pasteButton;
JButtonjavaButton,macButton,motifButton,winButton;
publicToolbarFrame4(){
super("ToolbarExample(Swingnotext)");
setSize(450,250);
addWindowListener(newWindowAdapter(){
publicvoidwindowClosing(WindowEvente){
System.exit(0);
}
});
//JPanelworksmuchlikePaneldoes,sowe'lluseit.
JPaneltoolbar=newJPanel();
toolbar.setLayout(newFlowLayout(FlowLayout.LEFT));
CCPHandlerhandler=newCCPHandler();
cutButton=newJButton(newImageIcon("cut.gif"));
cutButton.setActionCommand(CCPHandler.CUT);
cutButton.addActionListener(handler);
toolbar.add(cutButton);
copyButton=newJButton(newImageIcon("copy.gif"));
copyButton.setActionCommand(CCPHandler.COPY);
copyButton.addActionListener(handler);
toolbar.add(copyButton);
pasteButton=newJButton(newImageIcon("paste.gif"));
pasteButton.setActionCommand(CCPHandler.PASTE);
pasteButton.addActionListener(handler);
toolbar.add(pasteButton);
add(toolbar,BorderLayout.NORTH);
//AddtheL&Fcontrols.
JPanellnfPanel=newJPanel();
LnFListenerlnfListener=newLnFListener(this);
macButton=newJButton("Mac");
macButton.addActionListener(lnfListener);
lnfPanel.add(macButton);
javaButton=newJButton("Metal");
javaButton.addActionListener(lnfListener);
lnfPanel.add(javaButton);
motifButton=newJButton("Motif");
motifButton.addActionListener(lnfListener);
lnfPanel.add(motifButton);
winButton=newJButton("Windows");
winButton.addActionListener(lnfListener);
lnfPanel.add(winButton);
add(lnfPanel,BorderLayout.SOUTH);
}
publicstaticvoidmain(Stringargs[]){
ToolbarFrame4tf4=newToolbarFrame4();
tf4.setVisible(true);
}
}
// TheSwing-ifiedbuttonexample.Thebuttonsinthistoolbarallcarryimages
// butnotext.
//
import java.awt. * ;
import java.awt.event. * ;
import javax.swing. * ;
public class ToolbarFrame4 extends Frame {
JButtoncutButton,copyButton,pasteButton;
JButtonjavaButton,macButton,motifButton,winButton;
publicToolbarFrame4(){
super("ToolbarExample(Swingnotext)");
setSize(450,250);
addWindowListener(newWindowAdapter(){
publicvoidwindowClosing(WindowEvente){
System.exit(0);
}
});
//JPanelworksmuchlikePaneldoes,sowe'lluseit.
JPaneltoolbar=newJPanel();
toolbar.setLayout(newFlowLayout(FlowLayout.LEFT));
CCPHandlerhandler=newCCPHandler();
cutButton=newJButton(newImageIcon("cut.gif"));
cutButton.setActionCommand(CCPHandler.CUT);
cutButton.addActionListener(handler);
toolbar.add(cutButton);
copyButton=newJButton(newImageIcon("copy.gif"));
copyButton.setActionCommand(CCPHandler.COPY);
copyButton.addActionListener(handler);
toolbar.add(copyButton);
pasteButton=newJButton(newImageIcon("paste.gif"));
pasteButton.setActionCommand(CCPHandler.PASTE);
pasteButton.addActionListener(handler);
toolbar.add(pasteButton);
add(toolbar,BorderLayout.NORTH);
//AddtheL&Fcontrols.
JPanellnfPanel=newJPanel();
LnFListenerlnfListener=newLnFListener(this);
macButton=newJButton("Mac");
macButton.addActionListener(lnfListener);
lnfPanel.add(macButton);
javaButton=newJButton("Metal");
javaButton.addActionListener(lnfListener);
lnfPanel.add(javaButton);
motifButton=newJButton("Motif");
motifButton.addActionListener(lnfListener);
lnfPanel.add(motifButton);
winButton=newJButton("Windows");
winButton.addActionListener(lnfListener);
lnfPanel.add(winButton);
add(lnfPanel,BorderLayout.SOUTH);
}
publicstaticvoidmain(Stringargs[]){
ToolbarFrame4tf4=newToolbarFrame4();
tf4.setVisible(true);
}
}
//
CCPHandler.java
// ACut,Copy,andPasteeventhandler.Nothingtoofancy,justdefinesome
// constantsthatcanbeusedtosettheactionCommandsonbuttons.
//
import java.awt.event. * ;
public class CCPHandler implements ActionListener {
publicfinalstaticStringCUT="cut";
publicfinalstaticStringCOPY="copy";
publicfinalstaticStringPASTE="paste";
publicvoidactionPerformed(ActionEvente){
Stringcommand=e.getActionCommand();
if(command==CUT){//Wecandothissincewe'recomparingconstants.
System.out.println("GotCutevent");
}
elseif(command==COPY){
System.out.println("GotCopyevent");
}
elseif(command==PASTE){
System.out.println("GotPasteevent");
}
}
}
// ACut,Copy,andPasteeventhandler.Nothingtoofancy,justdefinesome
// constantsthatcanbeusedtosettheactionCommandsonbuttons.
//
import java.awt.event. * ;
public class CCPHandler implements ActionListener {
publicfinalstaticStringCUT="cut";
publicfinalstaticStringCOPY="copy";
publicfinalstaticStringPASTE="paste";
publicvoidactionPerformed(ActionEvente){
Stringcommand=e.getActionCommand();
if(command==CUT){//Wecandothissincewe'recomparingconstants.
System.out.println("GotCutevent");
}
elseif(command==COPY){
System.out.println("GotCopyevent");
}
elseif(command==PASTE){
System.out.println("GotPasteevent");
}
}
}
内部框架(Internal Frame)特点:1)和Frame对象功能一样,但被限制在父容器中。2)能被图标化 3)能被最大化 4)能用程序窗口的标准控制来关闭 5)能分层放置
// SimpleInternalFrame.java
// Aquickdemonstrationofsettingupaninternalframeinanapplication
//
import java.awt. * ;
import java.awt.event. * ;
import javax.swing. * ;
public class SimpleInternalFrame extends Frame {
JButtonopenButton,macButton,javaButton,motifButton,winButton;
JLayeredPanedesktop;
JInternalFrameinternalFrame;
publicSimpleInternalFrame(){
super("InternalFrameDemo");
setSize(500,400);
openButton=newJButton("Open");
macButton=newJButton("Mac");
javaButton=newJButton("Metal");
motifButton=newJButton("Motif");
winButton=newJButton("Windows");
Panelp=newPanel();
p.add(openButton);
p.add(macButton);
p.add(javaButton);
p.add(motifButton);
p.add(winButton);
add(p,BorderLayout.SOUTH);
addWindowListener(newWindowAdapter(){
publicvoidwindowClosing(WindowEvente){
System.exit(0);
}
});
openButton.addActionListener(newOpenListener());
LnFListenerlnf=newLnFListener(this);
macButton.addActionListener(lnf);
javaButton.addActionListener(lnf);
motifButton.addActionListener(lnf);
winButton.addActionListener(lnf);
//Setupthelayeredpane.
desktop=newJDesktopPane();
desktop.setOpaque(true);
add(desktop,BorderLayout.CENTER);
}
//AninnerclasstohandlepressesoftheOpenbutton
classOpenListenerimplementsActionListener{
publicvoidactionPerformed(ActionEvente){
if((internalFrame==null)||(internalFrame.isClosed())){
internalFrame=newJInternalFrame("InternalFrame",
true,true,true,true);
internalFrame.setBounds(50,50,200,100);
desktop.add(internalFrame,newInteger(1));
internalFrame.setVisible(true);
}
}
}
publicstaticvoidmain(Stringargs[]){
SimpleInternalFramesif=newSimpleInternalFrame();
sif.setVisible(true);
}
}