[翻译]Java Swing(2)
第2章
面向对象语言的一个好处在于你可以升级部分程序而不必重写其他代码。你可以使用大部分Swing组件来作为AWT组件的替代品。
//ToolbarFrame1.java
import java.awt.*;
import java.awt.event.*;
public class ToolbarFrame1 extends Frame
{
private Button cutBtn,copyBtn,pasteBtn;
public ToolbarFrame1()
{
super(“Toolbar例子(AWT)”);
this.setSize(450,250);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
}
ActionListener listerner = new ActionListener(){
Public void actionPerformed(ActionEvent e)
{
System.out.println(e.getActionCommand());
}
};
Panel toolbar = new Panel( );
toolbar.setLayout(new FlowLayout(FlowLayout.LEFT));
cutButton = new Button("Cut");
cutButton.addActionListener(printListener);
toolbar.add(cutButton);
copyButton = new Button("Copy");
copyButton.addActionListener(printListener);
toolbar.add(copyButton);
pasteButton = new Button("Paste");
pasteButton.addActionListener(printListener);
toolbar.add(pasteButton);
// The "preferred" BorderLayout add call
add(toolbar, BorderLayout.NORTH);
}
public static void main(String args[]) {
ToolbarFrame1 tf1 = new ToolbarFrame1( );
tf1.setVisible(true);
}
}
import java.awt.*;
import java.awt.event.*;
public class ToolbarFrame1 extends Frame
{
private Button cutBtn,copyBtn,pasteBtn;
public ToolbarFrame1()
{
super(“Toolbar例子(AWT)”);
this.setSize(450,250);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
}
ActionListener listerner = new ActionListener(){
Public void actionPerformed(ActionEvent e)
{
System.out.println(e.getActionCommand());
}
};
Panel toolbar = new Panel( );
toolbar.setLayout(new FlowLayout(FlowLayout.LEFT));
cutButton = new Button("Cut");
cutButton.addActionListener(printListener);
toolbar.add(cutButton);
copyButton = new Button("Copy");
copyButton.addActionListener(printListener);
toolbar.add(copyButton);
pasteButton = new Button("Paste");
pasteButton.addActionListener(printListener);
toolbar.add(pasteButton);
// The "preferred" BorderLayout add call
add(toolbar, BorderLayout.NORTH);
}
public static void main(String args[]) {
ToolbarFrame1 tf1 = new ToolbarFrame1( );
tf1.setVisible(true);
}
}
// ToolbarFrame2.java
// The Swing-ified button example
//
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ToolbarFrame2 extends Frame {
// This time, let's use JButtons!
JButton cutButton, copyButton, pasteButton;
JButton javaButton, macButton, motifButton, winButton;
public ToolbarFrame2( ) {
super("Toolbar Example (Swing)");
setSize(450, 250);
addWindowListener(new WindowAdapter( ) {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
ActionListener printListener = new ActionListener( ) {
public void actionPerformed(ActionEvent ae) {
System.out.println(ae.getActionCommand( ));
}
};
// JPanel works similarly to Panel, so we'll use it.
JPanel toolbar = new JPanel( );
toolbar.setLayout(new FlowLayout(FlowLayout.LEFT));
cutButton = new JButton("Cut");
cutButton.addActionListener(printListener);
toolbar.add(cutButton);
copyButton = new JButton("Copy");
copyButton.addActionListener(printListener);
toolbar.add(copyButton);
pasteButton = new JButton("Paste");
pasteButton.addActionListener(printListener);
toolbar.add(pasteButton);
add(toolbar, BorderLayout.NORTH);
// Add the L&F controls.
JPanel lnfPanel = new JPanel( );
LnFListener lnfListener = new LnFListener(this);
macButton = new JButton("Mac");
macButton.addActionListener(lnfListener);
lnfPanel.add(macButton);
javaButton = new JButton("Metal");
javaButton.addActionListener(lnfListener);
lnfPanel.add(javaButton);
motifButton = new JButton("Motif");
motifButton.addActionListener(lnfListener);
lnfPanel.add(motifButton);
winButton = new JButton("Windows");
winButton.addActionListener(lnfListener);
lnfPanel.add(winButton);
add(lnfPanel, BorderLayout.SOUTH);
}
public static void main(String args[]) {
ToolbarFrame2 tf2 = new ToolbarFrame2( );
tf2.setVisible(true);
}
}
//
// The Swing-ified button example
//
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ToolbarFrame2 extends Frame {
// This time, let's use JButtons!
JButton cutButton, copyButton, pasteButton;
JButton javaButton, macButton, motifButton, winButton;
public ToolbarFrame2( ) {
super("Toolbar Example (Swing)");
setSize(450, 250);
addWindowListener(new WindowAdapter( ) {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
ActionListener printListener = new ActionListener( ) {
public void actionPerformed(ActionEvent ae) {
System.out.println(ae.getActionCommand( ));
}
};
// JPanel works similarly to Panel, so we'll use it.
JPanel toolbar = new JPanel( );
toolbar.setLayout(new FlowLayout(FlowLayout.LEFT));
cutButton = new JButton("Cut");
cutButton.addActionListener(printListener);
toolbar.add(cutButton);
copyButton = new JButton("Copy");
copyButton.addActionListener(printListener);
toolbar.add(copyButton);
pasteButton = new JButton("Paste");
pasteButton.addActionListener(printListener);
toolbar.add(pasteButton);
add(toolbar, BorderLayout.NORTH);
// Add the L&F controls.
JPanel lnfPanel = new JPanel( );
LnFListener lnfListener = new LnFListener(this);
macButton = new JButton("Mac");
macButton.addActionListener(lnfListener);
lnfPanel.add(macButton);
javaButton = new JButton("Metal");
javaButton.addActionListener(lnfListener);
lnfPanel.add(javaButton);
motifButton = new JButton("Motif");
motifButton.addActionListener(lnfListener);
lnfPanel.add(motifButton);
winButton = new JButton("Windows");
winButton.addActionListener(lnfListener);
lnfPanel.add(winButton);
add(lnfPanel, BorderLayout.SOUTH);
}
public static void main(String args[]) {
ToolbarFrame2 tf2 = new ToolbarFrame2( );
tf2.setVisible(true);
}
}
//
LnFListener.java
// A listener that can change the L&F of a frame based on the actionCommand of an
// ActionEvent object. Supported L&Fs are: Mac, Metal, Motif, and Windows. Not all
// L&Fs will be available on a given machine. Notably, the Mac and Windows L&Fs work
// only on their specific platforms.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class LnFListener implements ActionListener {
Frame frame;
public LnFListener(Frame f) {
frame = f;
}
public void actionPerformed(ActionEvent e) {
String lnfName = null;
if (e.getActionCommand( ).equals("Mac")) {
lnfName = "com.apple.mrj.swing.MacLookAndFeel";
} else if (e.getActionCommand( ).equals("Metal")) {
lnfName = "javax.swing.plaf.metal.MetalLookAndFeel";
} else if (e.getActionCommand( ).equals("Motif")) {
lnfName = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
} else if (e.getActionCommand( ).equals("Windows")) {
lnfName = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
} else {
System.err.println("Unrecognized L&F request action: " +
e.getActionCommand( ));
return;
}
try {
UIManager.setLookAndFeel(lnfName);
SwingUtilities.updateComponentTreeUI(frame);
}
catch (UnsupportedLookAndFeelException ex1) {
System.err.println("Unsupported LookAndFeel: " + lnfName);
}
catch (ClassNotFoundException ex2) {
System.err.println("LookAndFeel class not found: " + lnfName);
}
catch (InstantiationException ex3) {
System.err.println("Could not load LookAndFeel: " + lnfName);
}
catch (IllegalAccessException ex4) {
System.err.println("Cannot use LookAndFeel: " + lnfName);
}
}
}
// A listener that can change the L&F of a frame based on the actionCommand of an
// ActionEvent object. Supported L&Fs are: Mac, Metal, Motif, and Windows. Not all
// L&Fs will be available on a given machine. Notably, the Mac and Windows L&Fs work
// only on their specific platforms.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class LnFListener implements ActionListener {
Frame frame;
public LnFListener(Frame f) {
frame = f;
}
public void actionPerformed(ActionEvent e) {
String lnfName = null;
if (e.getActionCommand( ).equals("Mac")) {
lnfName = "com.apple.mrj.swing.MacLookAndFeel";
} else if (e.getActionCommand( ).equals("Metal")) {
lnfName = "javax.swing.plaf.metal.MetalLookAndFeel";
} else if (e.getActionCommand( ).equals("Motif")) {
lnfName = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
} else if (e.getActionCommand( ).equals("Windows")) {
lnfName = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
} else {
System.err.println("Unrecognized L&F request action: " +
e.getActionCommand( ));
return;
}
try {
UIManager.setLookAndFeel(lnfName);
SwingUtilities.updateComponentTreeUI(frame);
}
catch (UnsupportedLookAndFeelException ex1) {
System.err.println("Unsupported LookAndFeel: " + lnfName);
}
catch (ClassNotFoundException ex2) {
System.err.println("LookAndFeel class not found: " + lnfName);
}
catch (InstantiationException ex3) {
System.err.println("Could not load LookAndFeel: " + lnfName);
}
catch (IllegalAccessException ex4) {
System.err.println("Cannot use LookAndFeel: " + lnfName);
}
}
}
// ToolbarFrame4.java
// The Swing-ified button example. The buttons in this toolbar all carry images
// but no text.
//
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ToolbarFrame4 extends Frame {
JButton cutButton, copyButton, pasteButton;
JButton javaButton, macButton, motifButton, winButton;
public ToolbarFrame4( ) {
super("Toolbar Example (Swing no text)");
setSize(450, 250);
addWindowListener(new WindowAdapter( ) {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
// JPanel works much like Panel does, so we'll use it.
JPanel toolbar = new JPanel( );
toolbar.setLayout(new FlowLayout(FlowLayout.LEFT));
CCPHandler handler = new CCPHandler( );
cutButton = new JButton(new ImageIcon("cut.gif"));
cutButton.setActionCommand(CCPHandler.CUT);
cutButton.addActionListener(handler);
toolbar.add(cutButton);
copyButton = new JButton(new ImageIcon("copy.gif"));
copyButton.setActionCommand(CCPHandler.COPY);
copyButton.addActionListener(handler);
toolbar.add(copyButton);
pasteButton = new JButton(new ImageIcon("paste.gif"));
pasteButton.setActionCommand(CCPHandler.PASTE);
pasteButton.addActionListener(handler);
toolbar.add(pasteButton);
add(toolbar, BorderLayout.NORTH);
// Add the L&F controls.
JPanel lnfPanel = new JPanel( );
LnFListener lnfListener = new LnFListener(this);
macButton = new JButton("Mac");
macButton.addActionListener(lnfListener);
lnfPanel.add(macButton);
javaButton = new JButton("Metal");
javaButton.addActionListener(lnfListener);
lnfPanel.add(javaButton);
motifButton = new JButton("Motif");
motifButton.addActionListener(lnfListener);
lnfPanel.add(motifButton);
winButton = new JButton("Windows");
winButton.addActionListener(lnfListener);
lnfPanel.add(winButton);
add(lnfPanel, BorderLayout.SOUTH);
}
public static void main(String args[]) {
ToolbarFrame4 tf4 = new ToolbarFrame4( );
tf4.setVisible(true);
}
}
// The Swing-ified button example. The buttons in this toolbar all carry images
// but no text.
//
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ToolbarFrame4 extends Frame {
JButton cutButton, copyButton, pasteButton;
JButton javaButton, macButton, motifButton, winButton;
public ToolbarFrame4( ) {
super("Toolbar Example (Swing no text)");
setSize(450, 250);
addWindowListener(new WindowAdapter( ) {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
// JPanel works much like Panel does, so we'll use it.
JPanel toolbar = new JPanel( );
toolbar.setLayout(new FlowLayout(FlowLayout.LEFT));
CCPHandler handler = new CCPHandler( );
cutButton = new JButton(new ImageIcon("cut.gif"));
cutButton.setActionCommand(CCPHandler.CUT);
cutButton.addActionListener(handler);
toolbar.add(cutButton);
copyButton = new JButton(new ImageIcon("copy.gif"));
copyButton.setActionCommand(CCPHandler.COPY);
copyButton.addActionListener(handler);
toolbar.add(copyButton);
pasteButton = new JButton(new ImageIcon("paste.gif"));
pasteButton.setActionCommand(CCPHandler.PASTE);
pasteButton.addActionListener(handler);
toolbar.add(pasteButton);
add(toolbar, BorderLayout.NORTH);
// Add the L&F controls.
JPanel lnfPanel = new JPanel( );
LnFListener lnfListener = new LnFListener(this);
macButton = new JButton("Mac");
macButton.addActionListener(lnfListener);
lnfPanel.add(macButton);
javaButton = new JButton("Metal");
javaButton.addActionListener(lnfListener);
lnfPanel.add(javaButton);
motifButton = new JButton("Motif");
motifButton.addActionListener(lnfListener);
lnfPanel.add(motifButton);
winButton = new JButton("Windows");
winButton.addActionListener(lnfListener);
lnfPanel.add(winButton);
add(lnfPanel, BorderLayout.SOUTH);
}
public static void main(String args[]) {
ToolbarFrame4 tf4 = new ToolbarFrame4( );
tf4.setVisible(true);
}
}
// CCPHandler.java
// A Cut, Copy, and Paste event handler. Nothing too fancy, just define some
// constants that can be used to set the actionCommands on buttons.
//
import java.awt.event.*;
public class CCPHandler implements ActionListener {
public final static String CUT = "cut";
public final static String COPY = "copy";
public final static String PASTE = "paste";
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand( );
if (command == CUT) { // We can do this since we're comparing constants.
System.out.println("Got Cut event");
}
else if (command == COPY) {
System.out.println("Got Copy event");
}
else if (command == PASTE) {
System.out.println("Got Paste event");
}
}
}
// A Cut, Copy, and Paste event handler. Nothing too fancy, just define some
// constants that can be used to set the actionCommands on buttons.
//
import java.awt.event.*;
public class CCPHandler implements ActionListener {
public final static String CUT = "cut";
public final static String COPY = "copy";
public final static String PASTE = "paste";
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand( );
if (command == CUT) { // We can do this since we're comparing constants.
System.out.println("Got Cut event");
}
else if (command == COPY) {
System.out.println("Got Copy event");
}
else if (command == PASTE) {
System.out.println("Got Paste event");
}
}
}
内部框架(Internal Frame)特点:1)和Frame对象功能一样,但被限制在父容器中。2)能被图标化 3)能被最大化 4)能用程序窗口的标准控制来关闭 5)能分层放置
// SimpleInternalFrame.java
// A quick demonstration of setting up an internal frame in an application
//
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SimpleInternalFrame extends Frame {
JButton openButton, macButton, javaButton, motifButton, winButton;
JLayeredPane desktop;
JInternalFrame internalFrame;
public SimpleInternalFrame( ) {
super("Internal Frame Demo");
setSize(500,400);
openButton = new JButton("Open");
macButton = new JButton("Mac");
javaButton = new JButton("Metal");
motifButton = new JButton("Motif");
winButton = new JButton("Windows");
Panel p = new Panel( );
p.add(openButton);
p.add(macButton);
p.add(javaButton);
p.add(motifButton);
p.add(winButton);
add(p, BorderLayout.SOUTH);
addWindowListener(new WindowAdapter( ) {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
openButton.addActionListener(new OpenListener( ));
LnFListener lnf = new LnFListener(this);
macButton.addActionListener(lnf);
javaButton.addActionListener(lnf);
motifButton.addActionListener(lnf);
winButton.addActionListener(lnf);
// Set up the layered pane.
desktop = new JDesktopPane( );
desktop.setOpaque(true);
add(desktop, BorderLayout.CENTER);
}
// An inner class to handle presses of the Open button
class OpenListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if ((internalFrame == null) || (internalFrame.isClosed( ))) {
internalFrame = new JInternalFrame("Internal Frame",
true, true, true, true);
internalFrame.setBounds(50, 50, 200, 100);
desktop.add(internalFrame, new Integer(1));
internalFrame.setVisible(true);
}
}
}
public static void main(String args[]) {
SimpleInternalFrame sif = new SimpleInternalFrame( );
sif.setVisible(true);
}
}
作者:洞庭散人
出处:http://phinecos.cnblogs.com/
本博客遵从Creative Commons Attribution 3.0 License,若用于非商业目的,您可以自由转载,但请保留原作者信息和文章链接URL。
posted on 2006-08-13 14:55 Phinecos(洞庭散人) 阅读(807) 评论(0) 编辑 收藏 举报