控制台程序。
最简单的对话框仅仅显示一些信息。为了说明这一点,可以为Sketcher添加Help菜单项和About菜单项,之后再显示About对话框来提供有关应用程序的信息。
要新建的对话框类从JDialog中派生以便创建About对话框,这个新类的名称是AboutDialog。
把AboutDialog类作为OK按钮的动作监听器,就可以使这个类变成自包含的。另外,因为所有操作都只在SketcherFrame类中进行,所以可以将之定义为SketcherFrame的嵌套类。
import javax.swing.JComponent;
import java.util.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import javax.swing.event.MouseInputAdapter; @SuppressWarnings("serial")
public class SketcherView extends JComponent implements Observer {
public SketcherView(Sketcher theApp) {
this.theApp = theApp;
MouseHandler handler = new MouseHandler(); // create the mouse listener
addMouseListener(handler); // Listen for button events
addMouseMotionListener(handler); // Listen for motion events
} // Method called by Observable object when it changes
public void update(Observable o, Object rectangle) {
if(rectangle != null) {
repaint((java.awt.Rectangle)rectangle);
} else {
repaint();
}
} // Method to draw on the view
@Override
public void paint(Graphics g) {
Graphics2D g2D = (Graphics2D)g; // Get a 2D device context
for(Element element: theApp.getModel()) { // For each element in the model
element.draw(g2D); // ...draw the element
}
} class MouseHandler extends MouseInputAdapter {
@Override
public void mousePressed(MouseEvent e) {
start = e.getPoint(); // Save the cursor position in start
buttonState = e.getButton(); // Record which button was pressed
if(buttonState == MouseEvent.BUTTON1) {
g2D = (Graphics2D)getGraphics(); // Get graphics context
g2D.setXORMode(getBackground()); // Set XOR mode
}
} @Override
public void mouseDragged(MouseEvent e) {
last = e.getPoint(); // Save cursor position if(buttonState == MouseEvent.BUTTON1) {
if(tempElement == null) { // Is there an element?
tempElement = Element.createElement( // No, so create one
theApp.getWindow().getElementType(),
theApp.getWindow().getElementColor(),
start, last);
} else {
tempElement.draw(g2D); // Yes draw to erase it
tempElement.modify(start, last); // Now modify it
}
tempElement.draw(g2D); // and draw it
}
} @Override
public void mouseReleased(MouseEvent e) {
if(e.getButton() == MouseEvent.BUTTON1) {
buttonState = MouseEvent.NOBUTTON; // Reset the button state if(tempElement != null) { // If there is an element...
theApp.getModel().add(tempElement); // ...add it to the model...
tempElement = null; // ...and reset the field
}
if(g2D != null) { // If there抯 a graphics context
g2D.dispose(); // ...release the resource...
g2D = null; // ...and reset field to null
}
start = last = null; // Remove any points
}
} @Override
public void mouseEntered(MouseEvent e) {
setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
} @Override
public void mouseExited(MouseEvent e) {
setCursor(Cursor.getDefaultCursor());
} private Point start; // Stores cursor position on press
private Point last; // Stores cursor position on drag
private Element tempElement = null; // Stores a temporary element
private int buttonState = MouseEvent.NOBUTTON; // Records button state
private Graphics2D g2D = null; // Temporary graphics context
} private Sketcher theApp; // The application object
}
在AboutDialog构造函数中,AboutDialog对象应是OK按钮的监听器,所以把this变量作为参数传送给按钮的addActionListener()方法。
pack()方法继承自Window类。这个方法封装了窗口中的组件,把窗口设置为相对其中的组件而言最优的尺寸,之后布置组件。注意,如果这里没有调用pack()方法,对话框的尺寸就不会设置,对话框也不能显示出来。
选择OK按钮时调用actionPerformed()方法。这个方法通过调用AboutDialog对象的dispose()方法来销毁对话框,这样对话框窗口就会从屏幕上消失并释放占用的资源。
特别声明:以上只是为了演示,实际上javax.swing包中的JOptionPane类定义了一些静态方法,用来创建和显示标准的模态对话框。