DrawFrame类---摘自《Java软件开发》Russel Winder & Graham Roberts
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
* This class provides a basic window, with a quit button, that can
* be used to create simple GUIs.
*
* @version 1.1 2000.01.07
* @author Graham Roberts
* @author Russel Winder
*/
public class DrawFrame extends JFrame {
/**
* Override of the method to add a <CODE>JPanel</CODE> to the
* <CODE>DrawFrame</CODE> so that the <CODE>JPanel</CODE> is
* centred.
*/
public void add(final JPanel panel) {
getContentPane().add(panel, BorderLayout.CENTER);
}
/**
* Terminate the program when the user wants to quit.
*/
private void quit() {
System.exit(0);
}
/**
* construct a <CODE>DrawFrame</CODE>
*/
public DrawFrame(final String title) {
// Initialize the JFrame ensuring the titlebar is set.
super(title);
// Set up the quit button with it's listener.
Button quitButton = new Button("Quit");
quitButton.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
quit();
}
});
// put all the buttons into a JPanel.
JPanel buttonPanel = new JPanel(new FlowLayout());
buttonPanel.add(quitButton);
// Create the contents of the frame. Use BorderLayout with the
// top (Center) part being the drawing area and the bottom
// (South) strip holding a quit button.
getContentPane().setLayout(new BorderLayout());
getContentPane().add(buttonPanel, BorderLayout.SOUTH);
// Ensure that windows close events from the window manager are
// caught and acted upon.
addWindowListener(new WindowAdapter() {
public void windowClosing(final WindowEvent evt) {
quit();
}
});
}
/**
* Position a window in the centre of the screen.
*/
public void centreOnScreen() {
Dimension displaySize = getToolkit().getScreenSize();
Dimension windowSize = getSize();
int x = (displaySize.width - windowSize.width) / 2;
int y = (displaySize.height - windowSize.height) / 2;
if(x < 0) {
x = 0;
}
if(y < 0) {
y = 0;
}
setLocation(x,y);
}
}
import java.awt.event.*;
import javax.swing.*;
/**
* This class provides a basic window, with a quit button, that can
* be used to create simple GUIs.
*
* @version 1.1 2000.01.07
* @author Graham Roberts
* @author Russel Winder
*/
public class DrawFrame extends JFrame {
/**
* Override of the method to add a <CODE>JPanel</CODE> to the
* <CODE>DrawFrame</CODE> so that the <CODE>JPanel</CODE> is
* centred.
*/
public void add(final JPanel panel) {
getContentPane().add(panel, BorderLayout.CENTER);
}
/**
* Terminate the program when the user wants to quit.
*/
private void quit() {
System.exit(0);
}
/**
* construct a <CODE>DrawFrame</CODE>
*/
public DrawFrame(final String title) {
// Initialize the JFrame ensuring the titlebar is set.
super(title);
// Set up the quit button with it's listener.
Button quitButton = new Button("Quit");
quitButton.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
quit();
}
});
// put all the buttons into a JPanel.
JPanel buttonPanel = new JPanel(new FlowLayout());
buttonPanel.add(quitButton);
// Create the contents of the frame. Use BorderLayout with the
// top (Center) part being the drawing area and the bottom
// (South) strip holding a quit button.
getContentPane().setLayout(new BorderLayout());
getContentPane().add(buttonPanel, BorderLayout.SOUTH);
// Ensure that windows close events from the window manager are
// caught and acted upon.
addWindowListener(new WindowAdapter() {
public void windowClosing(final WindowEvent evt) {
quit();
}
});
}
/**
* Position a window in the centre of the screen.
*/
public void centreOnScreen() {
Dimension displaySize = getToolkit().getScreenSize();
Dimension windowSize = getSize();
int x = (displaySize.width - windowSize.width) / 2;
int y = (displaySize.height - windowSize.height) / 2;
if(x < 0) {
x = 0;
}
if(y < 0) {
y = 0;
}
setLocation(x,y);
}
}
浙公网安备 33010602011771号