控制台程序。
最简单的对话框仅仅显示一些信息。为了说明这一点,可以为Sketcher添加Help菜单项和About菜单项,之后再显示About对话框来提供有关应用程序的信息。
要新建的对话框类从JDialog中派生以便创建About对话框,这个新类的名称是AboutDialog。
把AboutDialog类作为OK按钮的动作监听器,就可以使这个类变成自包含的。另外,因为所有操作都只在SketcherFrame类中进行,所以可以将之定义为SketcherFrame的嵌套类。
1 import javax.swing.JComponent; 2 import java.util.*; 3 import java.awt.*; 4 import java.awt.event.MouseEvent; 5 import javax.swing.event.MouseInputAdapter; 6 7 @SuppressWarnings("serial") 8 public class SketcherView extends JComponent implements Observer { 9 public SketcherView(Sketcher theApp) { 10 this.theApp = theApp; 11 MouseHandler handler = new MouseHandler(); // create the mouse listener 12 addMouseListener(handler); // Listen for button events 13 addMouseMotionListener(handler); // Listen for motion events 14 } 15 16 // Method called by Observable object when it changes 17 public void update(Observable o, Object rectangle) { 18 if(rectangle != null) { 19 repaint((java.awt.Rectangle)rectangle); 20 } else { 21 repaint(); 22 } 23 } 24 25 // Method to draw on the view 26 @Override 27 public void paint(Graphics g) { 28 Graphics2D g2D = (Graphics2D)g; // Get a 2D device context 29 for(Element element: theApp.getModel()) { // For each element in the model 30 element.draw(g2D); // ...draw the element 31 } 32 } 33 34 class MouseHandler extends MouseInputAdapter { 35 @Override 36 public void mousePressed(MouseEvent e) { 37 start = e.getPoint(); // Save the cursor position in start 38 buttonState = e.getButton(); // Record which button was pressed 39 if(buttonState == MouseEvent.BUTTON1) { 40 g2D = (Graphics2D)getGraphics(); // Get graphics context 41 g2D.setXORMode(getBackground()); // Set XOR mode 42 } 43 } 44 45 @Override 46 public void mouseDragged(MouseEvent e) { 47 last = e.getPoint(); // Save cursor position 48 49 if(buttonState == MouseEvent.BUTTON1) { 50 if(tempElement == null) { // Is there an element? 51 tempElement = Element.createElement( // No, so create one 52 theApp.getWindow().getElementType(), 53 theApp.getWindow().getElementColor(), 54 start, last); 55 } else { 56 tempElement.draw(g2D); // Yes draw to erase it 57 tempElement.modify(start, last); // Now modify it 58 } 59 tempElement.draw(g2D); // and draw it 60 } 61 } 62 63 @Override 64 public void mouseReleased(MouseEvent e) { 65 if(e.getButton() == MouseEvent.BUTTON1) { 66 buttonState = MouseEvent.NOBUTTON; // Reset the button state 67 68 if(tempElement != null) { // If there is an element... 69 theApp.getModel().add(tempElement); // ...add it to the model... 70 tempElement = null; // ...and reset the field 71 } 72 if(g2D != null) { // If there抯 a graphics context 73 g2D.dispose(); // ...release the resource... 74 g2D = null; // ...and reset field to null 75 } 76 start = last = null; // Remove any points 77 } 78 } 79 80 @Override 81 public void mouseEntered(MouseEvent e) { 82 setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR)); 83 } 84 85 @Override 86 public void mouseExited(MouseEvent e) { 87 setCursor(Cursor.getDefaultCursor()); 88 } 89 90 private Point start; // Stores cursor position on press 91 private Point last; // Stores cursor position on drag 92 private Element tempElement = null; // Stores a temporary element 93 private int buttonState = MouseEvent.NOBUTTON; // Records button state 94 private Graphics2D g2D = null; // Temporary graphics context 95 } 96 97 private Sketcher theApp; // The application object 98 }
在AboutDialog构造函数中,AboutDialog对象应是OK按钮的监听器,所以把this变量作为参数传送给按钮的addActionListener()方法。
pack()方法继承自Window类。这个方法封装了窗口中的组件,把窗口设置为相对其中的组件而言最优的尺寸,之后布置组件。注意,如果这里没有调用pack()方法,对话框的尺寸就不会设置,对话框也不能显示出来。
选择OK按钮时调用actionPerformed()方法。这个方法通过调用AboutDialog对象的dispose()方法来销毁对话框,这样对话框窗口就会从屏幕上消失并释放占用的资源。
特别声明:以上只是为了演示,实际上javax.swing包中的JOptionPane类定义了一些静态方法,用来创建和显示标准的模态对话框。