Java基础之扩展GUI——显示About对话框(Sketcher 2 displaying an About dialog)
控制台程序。
最简单的对话框仅仅显示一些信息。为了说明这一点,可以为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类定义了一些静态方法,用来创建和显示标准的模态对话框。
Java基础之扩展GUI——显示About对话框(Sketcher 2 displaying an About dialog)的更多相关文章
- Java基础之扩展GUI——使用字体对话框(Sketcher 5 displaying a font dialog)
控制台程序. 为了可以选择系统支持的字体,我们定义了一个FontDialog类: // Class to define a dialog to choose a font import java.aw ...
- Java基础之扩展GUI——添加状态栏(Sketcher 1 with a status bar)
控制台程序. 为了显示各个应用程序参数的状态,并且将各个参数显示在各自的面板中,在应用程序窗口的底部添加状态栏是常见且非常方便的方式. 定义状态栏时没有Swing类可用,所以必须自己建立StatusB ...
- Java基础之扩展GUI——使用对话框创建文本元素(Sketcher 4 creating text elements)
控制台程序. 为了与Sketcher中的其他元素类型保持一致,需要为Elements菜单添加Text菜单项和工具栏按钮.还需要定义用来表示文本元素的类Element.Text. 1.修改Sketche ...
- Java基础之扩展GUI——高亮元素、上下文菜单、移动旋转元素、自定义颜色(Sketcher 10)
窗口应用程序. 本例在上一版的基础上实现了高亮元素.移动元素.上下文菜单.旋转元素.设置自定义颜色. 1.自定义常量包: // Defines application wide constants p ...
- Java基础--Java---IO流------GUI(布局)、Frame、事件监听机制、窗体事件、Action事件、鼠标事件、对话框Dialog、键盘事件、菜单
* 创建图形化界面 * 1.创建frame窗体 * 2.对窗体进行基本设置 * 比如大小.位置.布局 * 3.定义组件 * 4.将组件通过窗体的add方法添加到窗体 * 5.让窗体显 ...
- java基础学习总结——GUI编程(一)
一.AWT介绍
- java基础学习总结——GUI编程(一) 还未仔细阅读
一.AWT介绍
- 【Java基础总结】GUI
GUI(Graphical User Interface),图形用户接口 CLI(Command Line User Interface),命令行用户接口 1. 容器 Container GUI主要位 ...
- java基础学习总结——GUI编程(二)
一.事件监听
随机推荐
- 读书笔记:javascript高级技巧(二)
四.惰性载入函数 因为浏览器兼容的原因,我们的javascript代码会有大量的if语句,将执行引导到正确的代码中,看如下函数: function createXHR(){ if (typeof XM ...
- 【HDU】3516 Tree Construction
http://acm.hdu.edu.cn/showproblem.php?pid=3516 题意:平面n个点且满足xi<xj, yi>yj, i<j.xi,yi均为整数.求一棵树边 ...
- BZOJ3294: [Cqoi2011]放棋子
Description Input 输入第一行为两个整数n, m, c,即行数.列数和棋子的颜色数.第二行包含c个正整数,即每个颜色的棋子数.所有颜色的棋子总数保证不超过nm. Output 输出 ...
- Timestame类型和String 类型的转化
Timestame类型和String 类型的转化 String转化为Timestamp: SimpleDateFormat df = new SimpleDateFormat("yyyy-M ...
- C#实现动态页面静态化
制作一个aspx页面,专门用来生成各个动态aspx页面的静态页面.如下图所示,仅将内容页和主页面生成静态页面,当然本例只是一个简单的范例,实际情况如很复杂,但原理都是相同的. 生成内容页: 本例中的不 ...
- asp.net中验证控件的使用方法
用于检查是否有输入值 :RequiredFieldValidator(必须字段验证) 按设定比较两个输入 :CompareValidator(比较验证) 输入是否在指定范围 :RangeValidat ...
- ListView的HeaderView和Footer
HeaderView介绍 HeaderView用法 属性中添加 ListView中属性listHeader和overScrollHeader区别: android:overScrollHeader=& ...
- Odoo 动态设置树形视图列表中的字段
依赖第三方模块实现,实现效果图如下: 原模块只有9.0版本 ,今改成了8.0版本. git地址见群内公告.
- .net 文件下载【转】
方式一:TransmitFile实现下载.将指定的文件直接写入 HTTP 响应输出流,而不在内存中缓冲该文件. protected void Button1_Click(object send ...
- php吧字符串直接转换成数组处理
$str ='123456'; echo strlen($str) - 1; echo '<br>'; echo $str{strlen($str) - 1}; echo '<br& ...