python 做 网站,网页快速收录,百度登录个人中心官网,wordpress 文章 样式在前面的章节中#xff0c;我们开发运行的应用程序都没有图形界面#xff0c;但是很多应用软件#xff0c;如Windows下的Office办公软件、扑克牌接龙游戏软件、企业进销存ERP系统等#xff0c;都有很漂亮的图形界面。素以需要我们开发具有图形界面的软件。 Java图形界面编程…在前面的章节中我们开发运行的应用程序都没有图形界面但是很多应用软件如Windows下的Office办公软件、扑克牌接龙游戏软件、企业进销存ERP系统等都有很漂亮的图形界面。素以需要我们开发具有图形界面的软件。 Java图形界面编程内容主要包括AWTAbstract Window Kit和Swing两个部分。AWT是用来创建图形界面的基本工具Swing是JFCJava Foundation Classes的一部分是对AWT的有益补充。
1.AWT简介
AWT是抽象窗口工具类Abstrat Window ToolKit的缩写AWT库中有60多个类和接口几乎涵盖了创建Java图形界面程序的所有工具。利用AWT类库可以在Applet的显示区域创建标签、按钮、复选框第二部分还可以针对用户行为进行响应。 AWT是jdk的一部分AWT为图形界面程序提供了所需的丰富的组件同时还提供了对事件类、监听器类、图形处理工具2D图形的支持 Java AWT类提供了四个主要的类包括组件类Component、容器类Container、图形类Graphics和布局管理器。 AWT虽然提供了很多对图形界面的支持但是缺少剪贴板支持、打印支持、没有弹出式菜单和滚动窗口等问题所以产生了新的组件Swing来解决这些问题。
2.SWING基础
Swing元素比AWT元素存在更好的图形处理性能。Swing具有java的跨平台性可以在任何平台上使用Swing图形界面组件。Swing的大部分组件是轻量级组件 Swing组件在不同平台上具有一致性的表现。Swing组件比AWT组件的实用性更强。Swing采用了MVC(Model-View-Controller,模型——视图——控制器)模式。
2.1 Swing的类层次结构
在javax.swing包中包含两种类型的组件顶层容器JFrame、JApplet、JDialog和JWindow和轻量级组件。Swing轻量级组件是从AWT的Container类直接或间接派生过来的、 Swing包是JFC(Java Foundation Classes)的一部分,javax.swing是其中最大的包并且绝大部分Swing组件都包含在swing包中。 例javax.swing.event包中定义了事件和事件处理类这与java.awt.event包类似主要包括事件类和监听器接口、事件适配等。
2.2 Swing的特点 MVC模式MVC模式包括模型、视图和控制器等部分。Model模型用于保存所用到的数据模型视图View用于显示数据内容控制器Controller用于处理用户和模块交互事件。分层后各层各司其职协同工作当模型的数据改变时会通知与之相关的视图视图通过控制器指定相应机制。为了更方便Swing组件将视图和控制器 合二为一。 组件的多样化Swing组件的类名都是以字母J开头的。 可存取性支持 所有的Swing组件都实现了Accessible接口这样一些辅助功能如屏幕阅读器也可以得到支持 设置边框Swing组件不仅可以设置一个或多个边框也可以让用户组合边框或自定义属于自己的边框 支持键盘操作JComponent类的RegisterKeyboardAction()方法可以实现用键盘操作替代用户鼠标操作。 使用图标Swing组件中的许多组件可以通过图标修饰自己。
2.3 swing程序结构简介
Java Swing的程序设计流程 1使用import关键字导入swing包 2设置GUI的“外观界面风格” 3创建顶层容器 4创建按钮和标签等各种所需的组件 5将组件添加到顶层容器 6在组件周围添加边界 7进行事件处理 8结束
package swing;import javax.swing.*; //导入swing包
import java.awt.*; //导入awt包
import java.awt.event.*; //导入awt事件处理包public class FirseSwingApp {public static void main(String[] args) {try { //try语句块监视该段程序UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());} catch (Exception e) { //捕获异常e.printStackTrace(); //输出异常信息}JFrame frame new JFrame(我的第一个Swing程序);Container c frame.getContentPane();JPanel pane new JPanel();c.add(pane);pane.setLayout(new FlowLayout());JLabel label new JLabel();JButton button new JButton(Press me);pane.add(label);pane.add(button);// 为增加按钮添加监听button.addActionListener(new ActionListener() {Overridepublic void actionPerformed(ActionEvent e) {label.setText(监听事件成功);}});frame.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {System.exit(0);}});frame.setSize(400,280);frame.setVisible(true);}}
3.事件类
每一个事件类都与一个事件类接口相对应由事件引起的动作都存放在接口需要实现的方法中。Java中常用的事件类接口包括动作事件类、调整事件类、焦点事件类、项目事件类、按钮事件类、鼠标事件类、窗口事件类。
3.1 事件类的分类
所有与AWT相关的事件类都是java.awt.AWTEvent的派生类AWTEvent也是java.util.EventObject类的派生类。 AWT事件包括低级事件和高级事件低级事件时指源于组件或容器的事件当组件或容器发生事件时将触发事件。 高级事件是语义事件。这类事件与特定的具体事件不一定相对应但是会 产生特定的事件对象。
3.2 动作事件类
动作事件类是指发生组件定义的语义事件。使用动作事件时需要给组件添加一个事件监听器ActionListener。ActionListener只有唯一的actionPerformed()方法。 // 给按钮 增加 监听button.addActionListener(new ActionListener() {// 当按钮被点击时就会触发 ActionEvent事件public void actionPerformed(ActionEvent e) {//按钮被操作}});3.3 调整事件类
调整事件是滑动滚动条时发生的事件。为了监听调整事件需要给ScrollBar添加一个调整事件监听器AdjustListener
scrollbar.addAdjustmentListener(new AdjustListener()){public void adjustmentValueChanged(AdjustmentEvent e){//调整滚动条}
}3.4 焦点事件类
焦点事件类FocusEvent是指界面的组件失去焦点焦点从一个对象转移到另外一个对象时就会发生焦点事件。得到焦点事件的组件处于激活状态。
void FocusGained(FocusEvent e){// 当获得焦点时发生
}
void FocusLost(FocusEvent e){// 当失去焦点时发生
}
3.5 项目事件类
项目事件类(ItemEvent)是指某个项目Item时被选定、取消的语义事件使用项目事件必须给组件添加一个实现ItemListener接口的事件处理器。
void itemStateChange(ItemEvent e)3.6 键盘事件类
键盘事件类(KeyEvent)是指容器内任意组件获得焦点时组件发生键击时组件对象将产生该事件。 f.addKeyListener(new KeyListener() {// 键被弹起public void keyReleased(KeyEvent e) {}//键被按下public void keyPressed(KeyEvent e) {// TODO Auto-generated method stub}// 一个按下弹起的组合动作public void keyTyped(KeyEvent e) {}});3.7 鼠标事件类
鼠标事件类MouseEvent是指组件中发生鼠标动作时间当鼠标移动到某个区域或单击某个组件时会触发鼠标事件。使用鼠标事件需要给组件添加MouseListener接口的事件处理器 l.addMouseListener(new MouseListener() {// 释放鼠标public void mouseReleased(MouseEvent e) {// TODO Auto-generated method stub}// 按下鼠标public void mousePressed(MouseEvent e) {// TODO Auto-generated method stub}// 鼠标退出public void mouseExited(MouseEvent e) {// TODO Auto-generated method stub}// 鼠标进入public void mouseEntered(MouseEvent e) {}// 按下释放组合动作为点击鼠标public void mouseClicked(MouseEvent e) {// TODO Auto-generated method stub}}); 3.8 窗口事件类
窗口事件类(WindowEvent)是指窗口状态改变的事件。使用窗口事件必须为组件添加一个实现WindowListener接口的事件处理器。
windowOpenedvoid windowOpened(WindowEvent e)
窗口首次变为可见时调用。windowClosingvoid windowClosing(WindowEvent e)
用户试图从窗口的系统菜单中关闭窗口时调用。windowClosedvoid windowClosed(WindowEvent e)
因对窗口调用 dispose 而将其关闭时调用。windowIconifiedvoid windowIconified(WindowEvent e)
窗口从正常状态变为最小化状态时调用。对于很多平台而言已最小化的窗口将显示为窗口 iconImage 属性中所指定的图标。windowDeiconifiedvoid windowDeiconified(WindowEvent e)
窗口从最小化状态变为正常状态时调用。windowActivatedvoid windowActivated(WindowEvent e)
将 Window 设置为活动 Window 时调用。只有框架或对话框可以成为活动 Window。本机的窗口系统可能使用特殊装饰表示活动 Window 或其子窗口如高亮显示标题栏。活动 Window 要么是聚焦 Window要么是作为聚焦 Window 所有者的第一个 Frame 或 Dialog。
windowDeactivatedvoid windowDeactivated(WindowEvent e)
当 Window 不再是活动 Window 时调用。只有框架或对话框可以成为活动 Window。本机的窗口操作系统可能使用特殊装饰表示活动 Window 或其子窗口如高亮显示标题栏。活动 Window 要么是聚焦 Window要么是作为聚焦 Window 所有者的第一个 Frame 或 Dialog。4 事件监听器
事件监听器的主要任务是对事件进行监听并进行相应处理。事件处理是图形界面程序设计必须做的工作。 当用户使用鼠标、键盘等外部设备输入发生事件时可以使用事件监听处理器处理这些事件从而实现相应的事件响应。事件监听器是为了处理特定时间而编写的方法。
4.1 事件监听器接口
不同的事件类对应不同的的事件监听器他是事件监听器类的接口。
AWT的组件不仅可以注册监听器还可以注销监听器
public void addListenerType (ListenerType listener ) //注册监听器
public void removeListenerType(ListenerType listener)//注销监听器4.2 事件监听器特点
事件监听器接口是一个特殊的接口与普通的接口相比不同之处在于 1一个监听器接口的类可以实现一个或或多个接口并且接口之间用逗号隔开
implements MouseMotionListener,MouseLinstener,WindowListener;(2) 同一个组件可以监听一个事件源的多个事件 即在对象ob上接收多个事件并被同一个监听器接收和处理。
ob.addMouseMotionListener(this) ; //添加鼠标动作监听器
ob.addMouseListener(this); //添加鼠标监听器
ob.addWindowListener(this); //添加窗口监听器3事件处理这和事件源可以处在同一个类中。 上述程序事件中事件源是窗口对象ob,事件处理这是类TreeListener其中ob是类TreeLIstener的成员变量 4通过事件对象可以获得一些信息。例如可以获得组件中鼠标的位置坐标。
public void mouseMoved(MouseEvent e){Strng str 鼠标移动;ob1.setText(s);ob2.setText(鼠标坐标为: e.getX() e.getY());
}Java语言只支持单继承但可以通过接口实现多继承的功能。
在AWT中经常实现多个监听器接口每一个接口中已定义的方法必须实现。如果针对某事件不做任何处理就不需要实现这个方法方法体为空。
5.轻量级容器
Jframe的内部轻量级容器结构图 用户添加的组件都在contentPane上背景图片只能添加到layeredPane. GlassPane是完全透明的默认值为不可见。 覆盖content的目的是为了接收鼠标事件和组件绘图功能提供方便。
5.1 根面板JRootPanel
根面板JRootPanel由contentPane、menuBar、glassPlane构成
5.2 面板JPanel
面板JPanel是轻量级容器用法与Panel基本相同用于收纳其他的组件。
5.3 分层面板(JLayeredPane)
Swing有JLayeredPane、JDesktopPane两种类型的分层面板JDesktopPane是JLayeredPane的派生类用于创建多文档和虚拟桌面程序的容器
5.4 滚动窗口(JScrollPane)
滚动窗口JScrollPane是管理窗口(JViewport)、可选水平或垂直滚动的轻量级容器。滚动窗口有JScrollBar、JViewPort之间的链接组成
5.5 分隔板(JSplitPane)
分隔板JSplitPane用于提供可以拆分的窗口但只能分割成两个容器。支持水平和竖直两种拆分类型可带滚动条
5.6 选项板JTabbedPane
选项板JTabbedPane提供允许用户通过单击具有标题和图标的选项卡在组件之间进行来回切换的组件通过addTab()和insertTab()方式添加选项卡、组件到TabbedPane对象中。
5.7 工具栏JToolBar
工具栏JToolBar是显示常用工具组件的容器。可以将工具栏单独拖出作为一个独立的工具条。
6. Swing组件
Swing组件与AWT组件类似但又为每个组件添加了新的方法。
6.1 按钮Button
在Swing中所有类型的按钮都是javax.swing.AbstractButton类的子类。
6.2 复选框JChekBox
该类是javax.swing.JToggleButton类的子类。
6.3 单选框JRadioButton
单选框JRadioButton与AWT中的复选框功能类似 JRadioButton 和 ButtonGroup配合使用时可以创建一组数组但一次只能选择其中的一个按钮通过add()方法将JRadioButton对象添加到该组中。
6.4 组合框JComboBox
组合框是将按钮、可编辑字段及下拉菜单组合在一起的操作。用户可以从下拉菜单中选则不同的值。
6.5 进度条JProgressBar
进度条是以图形化的方式来描述任务进度的组件。在任务完成的过程中进度条显示该任务完成的百分比。
6.6 表格JTable
在界面上将数据以表格的形式展示可以采用表格控件(JTable)来实现。 表格控件是Swing新增加的组件。主要是为了将数据以表格的形式显示。
6.7 树JTree
很多时候需要使用树状控件展示数据如Windows的文件浏览器就像现行的树状控件。树JTree中特定的节点可以由TreePath标识或由其显示行标识。可查看的父类节点都是可以展开的但是他们可以显示也可以不显示。显示节点必须是可以查看的并且位于显示区域。 、
7 拓展训练
7.1 设置闪烁的标题栏
【Timer控件】 在银行股票比赛系统中经常出现同时处理并显示业务数据的情况每个窗口和窗口中的数据分类和重要性并不相通。当有的信息重要性很高必须以醒目的方式提醒读者注意的时候可以采用闪烁的标题栏来动态突出某窗口信息的重要性。 【例】在TimerFlashBar类的主方法中创建Timer控件并在空间中实现窗口闪烁的效果。该效果一直循环每隔1秒闪烁一次
package swing;import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;import javax.swing.Timer;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;public class TimerFlashBar extends JFrame{
//
// private static final long serialVersionUID private JPanel contentPane;public static void main(String[] args) {EventQueue.invokeLater(new Runnable() { //把事件添加到AWT的事件处理线程中public void run() {try {TimerFlashBar frame new TimerFlashBar();frame.setVisible(true);}catch(Exception e) {e.printStackTrace();}}});}public TimerFlashBar() {addWindowListener(new WindowAdapter() {Overridepublic void windowOpened(WindowEvent e) {do_this_windowOpened(e);} });setTitle(设置闪烁的标题栏);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setBounds(100,100,273,130);contentPane new JPanel();contentPane.setBorder(new EmptyBorder(5,5,5,5));contentPane.setLayout(new BorderLayout(0,0));setContentPane(contentPane);JLabel label new JLabel(系统内存紧缺请立即保存数据);label.setHorizontalAlignment(SwingConstants.CENTER);}protected void do_this_windowOpened(WindowEvent e) {Timer timer new Timer(500,new ActionListener() {String title getTitle();public void actionPerformed(ActionEvent e) {if(getTitle().isEmpty()) {setTitle(title);}else {setTitle();}}});timer.start();}
} 7.2 建立简单的时钟布局
【布局管理器】 Java提供了流布局、边框布局、网格布局等多种布局方式其实我们也可以自定义一个布局管理器
package swing;import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.LayoutManager;public class UseCloakLayout implements LayoutManager {public void addLayoutComponent(String name,Component comp) {}public void layoutContainer(Container parent) {double centerX parent.getBounds().getCenterX();double centerY parent.getBounds().getCenterY();Insets insets parent.getInsets();double horizon centerX - insets.left;double vertical centerY - insets.top;double radius horizon vertical ? vertical:horizon;//设置时钟半径int count parent.getComponentCount();//获得控件个数for(int i 0;icount;i) {Component component parent.getComponent(i);if(component.isVisible()) {Dimension size component.getPreferredSize();double angle 2 * Math.PI*i/count;//获得角度大小double x centerX radius*Math.sin(angle);double y centerY - radius*Math.cos(angle);//重新设置控件的位置和大小component.setBounds((int)x-size.width/2,(int)y-size.height/2, size.width, size.height);}}}//给定指定容器所包含的控件计算该容器的最小大小维度数Overridepublic Dimension minimumLayoutSize(Container parent) {// TODO Auto-generated method stubreturn parent.getMinimumSize();}Overridepublic Dimension preferredLayoutSize(Container parent) {// TODO Auto-generated method stubreturn parent.getPreferredSize();}Overridepublic void removeLayoutComponent(Component comp) {// TODO Auto-generated method stub}}
package swing;import java.awt.BorderLayout;
import java.awt.EventQueue;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;public class CloakLayoutTest extends JFrame{private JPanel contentPane;public static void main(String[] args) {try {UIManager.setLookAndFeel(com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel);}catch(Throwable e) {e.printStackTrace();}EventQueue.invokeLater(new Runnable() {Overridepublic void run() {try {ClockLayoutTest frame new ClockLayoutTest();frame.setVisible(true);}catch(Exception e) {e.printStackTrace();}}});}public CloakLayoutTest() {setTitle(建立时钟布局);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setBounds(100,100,450,300);contentPane new JPanel();contentPane.setBorder(new EmptyBorder(5,5,5,5));contentPane.setLayout(new BorderLayout(0,0));setContentPane(contentPane);setLayout(new ClockLayoutTest());for(int i 0;i12;i) {if(i10) {add(new JButton(i));}else {add(new JButton(i));}}}}
7.3简单投票系统
【多个组件的使用】 在日常生活中我们经常会遇到进行投票的情况。本例实现一个简单的投票计数软件 package swing;import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;public class VoteCount extends JFrame{private JPanel contentPane;//设置4个复选框private JCheckBox checkBox1;private JCheckBox checkBox2;private JCheckBox checkBox3;private JCheckBox checkBox4;//设置4个标签private JLabel label1;private JLabel label2;private JLabel label3;private JLabel label4;//设置4个进度条private JProgressBar progressBar1;private JProgressBar progressBar2;private JProgressBar progressBar3;private JProgressBar progressBar4;public static void main(String[] args) { try {UIManager.setLookAndFeel(com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel);}catch(Throwable e){e.printStackTrace();}EventQueue.invokeLater(new Runnable() {public void run() {try {VoteCount frame new VoteCount();frame.setVisible(true);}catch(Exception e) {e.printStackTrace();}}});}public VoteCount() {setTitle(简单投票系统);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setBounds(100,100,450,300);contentPane new JPanel();contentPane.setBorder(new EmptyBorder(5,5,5,5));setContentPane(contentPane);contentPane.setLayout(new BorderLayout(0,0));JPanel buttonPanel new JPanel();contentPane.add(buttonPanel,BorderLayout.SOUTH);JButton submitButton new JButton(提交);submitButton.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {do_submitButton_actionPerformed(e);}});buttonPanel.add(submitButton);JButton refreshButton new JButton(刷新);refreshButton.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {do_refreshButton_actionPerformed(e);}});buttonPanel.add(refreshButton);JPanel panel new JPanel();panel.setBorder(new TitledBorder(new LineBorder(new Color(0,0,0)),我最喜爱的水果,TitledBorder.LEADING,TitledBorder.TOP,null,new Color(59,59,59)));contentPane.add(panel,BorderLayout.CENTER);panel.setLayout(new GridLayout(4,1,15,15));//第一条记录JPanel panel1 new JPanel();panel.add(panel1);panel1.setLayout(new BorderLayout(0,0));checkBox1 new JCheckBox(苹果);panel1.add(checkBox1,BorderLayout.WEST);label1 new JLabel(0票);panel1.add(label1,BorderLayout.EAST);progressBar1 new JProgressBar();progressBar1.setStringPainted(true);panel1.add(progressBar1,BorderLayout.CENTER);//第二条记录JPanel panel2 new JPanel();panel.add(panel2);panel2.setLayout(new BorderLayout(0,0));checkBox2 new JCheckBox(香蕉);panel2.add(checkBox2,BorderLayout.WEST);label2 new JLabel(0票);panel2.add(label2,BorderLayout.EAST);progressBar2 new JProgressBar();progressBar2.setStringPainted(true);panel2.add(progressBar2,BorderLayout.CENTER);//第三条记录JPanel panel3 new JPanel();panel.add(panel3);panel3.setLayout(new BorderLayout(0,0));checkBox3 new JCheckBox(梨);panel3.add(checkBox3,BorderLayout.WEST);label3 new JLabel(0票);panel3.add(label3,BorderLayout.EAST);progressBar3 new JProgressBar();progressBar3.setStringPainted(true);panel3.add(progressBar3,BorderLayout.CENTER);//第四条记录JPanel panel4 new JPanel();panel.add(panel4);panel4.setLayout(new BorderLayout(0,0));checkBox4 new JCheckBox(桃子);panel4.add(checkBox4,BorderLayout.WEST);label4 new JLabel(0票);panel4.add(label4,BorderLayout.EAST);progressBar4 new JProgressBar();progressBar4.setStringPainted(true);panel4.add(progressBar4,BorderLayout.CENTER);}protected void do_submitButton_actionPerformed(ActionEvent e) {String text1 label1.getText(); //获得标签文本int number1 Integer.parseInt(text1.substring(0,text1.length()-1));//获得票数String text2 label1.getText(); //获得标签文本int number2 Integer.parseInt(text1.substring(0,text1.length()-1));//获得票数String text3 label1.getText(); //获得标签文本int number3 Integer.parseInt(text1.substring(0,text1.length()-1));//获得票数String text4 label1.getText(); //获得标签文本int number4 Integer.parseInt(text1.substring(0,text1.length()-1));//获得票数if(checkBox1.isSelected()) { //如果被选中number1; //票数1label1.setText(number1票);//更新标签}if(checkBox2.isSelected()) {number2;label2.setText(number2票);}if(checkBox3.isSelected()) {number3;label3.setText(number3票);}if(checkBox4.isSelected()) {number4;label4.setText(number4票);}//计算总票数double total number1 number2 number3 number4;progressBar1.setString(number1*100/total%); //在进度条上显示票数progressBar1.setValue(number1);progressBar2.setString(number2*100/total%); //在进度条上显示票数progressBar2.setValue(number2);progressBar3.setString(number3*100/total%); //在进度条上显示票数progressBar3.setValue(number3);progressBar4.setString(number4*100/total%); //在进度条上显示票数progressBar4.setValue(number4);}protected void do_refreshButton_actionPerformed(ActionEvent e) {//建立动作事件的响应checkBox1.setSelected(false);checkBox2.setSelected(false);checkBox3.setSelected(false);checkBox4.setSelected(false);}}
7.4 包含图片的弹出菜单
【JPopMenu的使用】 Windows应用软件单击鼠标右键会出现一个弹出菜单菜单可以进行排列桌面图片、创建文件或文件夹等操作。本例演示如何利用Swing创建包含图片的弹出菜单
package swing;import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JProgressBar;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;class PopupMenuImage extends Frame{private JPanel contentPane;public static void main(String[] args) {try {UIManager.setLookAndFeel(com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel);}catch(Throwable e) {e.printStackTrace();}EventQueue.invokeLater(new Runnable() {public void run() {try {PopupMenuImage frame new PopupMenuImage();frame.setVisible(true);}catch(Exception e){e.printStackTrace();}}});}public PopupMenuImage() {setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setBounds(100,100,300,200);contentPane new JPanel();contentPane.setBorder(new EmptyBorder(5,5,5,5));JPopupMenu popupMenu new JPopupMenu();contentPane.setComponentPopupMenu(popupMenu);JMenuItem cut new JMenuItem(剪切);cut.setIcon(new ImageIcon(PopupMenuImage.class.getResource(./cut.jpg)));cut.addActionListener(listener);popupMenu.add(cut);JMenuItem copy new JMenuItem(拷贝);copy.setIcon(new ImageIcon(PopupMenuImage.class.getResource(./cpoy.jpg)));copy.addActionListener(listener);popupMenu.add(copy);JMenuItem paste new JMenuItem(粘贴);paste.setIcon(new ImageIcon(PopupMenuImage.class.getResource(./paste.jpg)));paste.addActionListener(listener);popupMenu.add(paste);contentPane.setLayout(new BorderLayout(0,0));setContentPane(contentPane);label new JLabel(单击鼠标右键弹出菜单);contentPane.add(label,BorderLayout.NORTH);}private void setDefaultCloseOperation(int exitOnClose) {// TODO Auto-generated method stub}private void setContentPane(JPanel contentPane2) {// TODO Auto-generated method stub}private ActionListener listener new ActionListener() {public void actionPerformed(ActionEvent e) {label.setText(e.getActionCommand());}};
private JLabel label;}
7.5 震动效果提示信息 package swing;import javax.swing.Timer;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;import javax.swing.JDialog;
import javax.swing.JOptionPane;public class ShakeEffect {private JDialog dialog;private Point start;private Timer shakeTimer;public ShakeEffect(JDialog dialog) {this.dialog dialog;}public void startShake() { //开始震动方法final long startTime System.currentTimeMillis();//获得程序运行起始时间start dialog.getLocation();shakeTimer new Timer(10,new ActionListener() {public void actionPerformed(ActionEvent e) {long elapsed System.currentTimeMillis() - startTime;Random random new Random(elapsed);int change random.nextInt(50);dialog.setLocation(start.x change,start.ychange);//随机改变坐标if(elapsed 1000) {stopShake();}}});shakeTimer.start();}public void stopShake() {shakeTimer.stop();dialog.setLocation(start);dialog.repaint();}public static void main(String[] args) {JOptionPane pane new JOptionPane(特别注意,JOptionPane.WARNING_MESSAGE);JDialog d pane.createDialog(null,震动效果对话框);//获得对话框对象ShakeEffect sd new ShakeEffect(d);d.pack();d.setModal(false);d.setVisible(true);sd.startShake();}}
8 答疑解惑
swing是一个用于开发Java应用程序图形界面的开发工具包以抽象窗口工具包(AWT)为基础使跨平台应用程序可以使用任何可插拔的外观风格。 一般的桌面应用使用Java Swing是绰绰有余的。但是大型网站、ERP等大型项目不适合使用Swing。