2009-06-04读书记录:《Core Java》第八版卷I之User Interface Components with swing
来源:互联网 发布:儿童在线画图软件 编辑:程序博客网 时间:2024/04/28 01:47
仍然是补飞Java基础部分,这里就是桌面程序所必须的了,虽然现在大部分主流技术都是用Java写Web程序,但学一下桌面程序自己写个小工具也是挺有趣的。都很基础,练习一下每个程序就差不多了。只是用java写桌面应用真的比较麻烦,没有可视化的form,只能凭自己想像力。
菜单这一节的例子不错,挺全面的,虽然打着有点累,但运行起来效果出来的时候还是不错的。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
*
* @author Administrator
*/
public class MenuTest {
public static void main(String[] args){
EventQueue.invokeLater(new Runnable(){
public void run() {
MenuFrame frame = new MenuFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
class MenuFrame extends JFrame {
private static final int DEFAULT_WIDTH = 300;
private static final int DEFAULT_HEIGHT = 200;
private Action saveAction;
private Action saveAsAction;
private JCheckBoxMenuItem readonlyItem;
private JPopupMenu popup;
public MenuFrame() {
setTitle("MenuTest");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
//fileMenu start
JMenu fileMenu = new JMenu("File");
fileMenu.add(new TestAction("New"));
JMenuItem openItem = fileMenu.add(new TestAction("Open"));
openItem.setAccelerator(KeyStroke.getKeyStroke("ctrl O")); //设置修改键,它能直接调用菜单项的操作侦听器而不必显示菜单的层次结构。
fileMenu.addSeparator(); //加入分隔符
saveAction = new TestAction("Save");
JMenuItem saveItem = fileMenu.add(saveAction);
saveItem.setAccelerator(KeyStroke.getKeyStroke("ctrl S"));
saveAsAction = new TestAction("Save As");
fileMenu.add(saveAsAction);
fileMenu.addSeparator();
JMenuItem exitItem = new JMenuItem("Exit");
exitItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
fileMenu.add(exitItem);
/*
fileMenu.add(new AbstractAction("Exit") {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
* */
//fileMenu end
//editMenu start
readonlyItem = new JCheckBoxMenuItem("Read-Only");
readonlyItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
boolean saveOk = !readonlyItem.isSelected();
saveAction.setEnabled(saveOk);
saveAsAction.setEnabled(saveOk);
}
});
ButtonGroup group = new ButtonGroup();
JRadioButtonMenuItem insertItem = new JRadioButtonMenuItem("Insert");
insertItem.setSelected(true);
JRadioButtonMenuItem overtypeItem = new JRadioButtonMenuItem("Overtype");
group.add(insertItem);
group.add(overtypeItem);
Action cutAction = new TestAction("Cut");
cutAction.putValue(Action.SMALL_ICON, new ImageIcon("cut.gif"));
Action copyAction = new TestAction("Copy");
copyAction.putValue(Action.SMALL_ICON, new ImageIcon("copy.gif"));
Action pasteAction = new TestAction("Paste");
pasteAction.putValue(Action.SMALL_ICON, new ImageIcon("paste.gif"));
JMenu editMenu = new JMenu("Edit");
editMenu.add(cutAction);
editMenu.add(copyAction);
editMenu.add(pasteAction);
JMenu optionMenu = new JMenu("Option");
optionMenu.add(readonlyItem);
optionMenu.addSeparator();
optionMenu.add(insertItem);
optionMenu.add(overtypeItem);
editMenu.addSeparator();
editMenu.add(optionMenu);
//editMenu end
//helpMenu start
JMenu helpMenu = new JMenu("Help");
helpMenu.setMnemonic('H'); //设置助记符
JMenuItem indexItem = new JMenuItem("Index");
indexItem.setMnemonic('I');
helpMenu.add(indexItem);
Action aboutAction = new TestAction("About");
aboutAction.putValue(Action.MNEMONIC_KEY, new Integer('A')); //使用关联的键设置此对象的一个属性。如果值发生了更改,则将一个 PropertyChangeEvent 发送到侦听器
helpMenu.add(aboutAction);
//helpMenu end
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(helpMenu);
popup = new JPopupMenu();
popup.add(cutAction);
popup.add(copyAction);
popup.add(pasteAction);
JPanel panel = new JPanel();
panel.setComponentPopupMenu(popup);
add(panel);
panel.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
public void mousePressed(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
public void mouseReleased(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
public void mouseEntered(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
public void mouseExited(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
});
}
}
class TestAction extends AbstractAction {
public TestAction(String name) {
super(name);
}
public void actionPerformed(ActionEvent event) {
System.out.println(getValue(Action.NAME) + " selected.");
}
}
- 2009-06-04读书记录:《Core Java》第八版卷I之User Interface Components with swing
- 2009-06-09读书记录:《Core Java》卷I第八章——swing
- 2009-06-01读书记录:《Core Java》第八版卷II之XML
- 2009-04-08读书记录:《Core Java》(第八版)前两章
- Java Swing之Layout Components
- JAVA 核心技术 卷I 第八版 例2-2
- 【JAVA学习】Core Java读书记录
- java核心技术卷一卷二,第七版,第八版,core java 2
- Java core 卷一第八章泛型程序设计 (Generic programming)
- Java核心技术卷I:基础知识(原书第8版):14.11 线程与Swing
- 纠错 : 关于书籍<java核心技术 卷I 基础知识 第八版> 第五章 Employee/Manager 两个例子
- 2009-04-19读书记录:《Servlet与JSP核心编程》第八章——cookie管理
- [2014-10-29]JAVA笔记_GUI(Graphical User Interface)AWT、Swing
- Implements interface using java swing.
- Creating an Extensible User Interface with .NET
- Model Dialog with free JQuery User Interface
- Creating Android Applications with HTML User Interface
- Android user interface testing with Robotium - Tutorial
- 函数指针与回调函数
- Annotated Lucene:第一节 Lucene是什么
- uCOS II在SkyEye上的移植分析
- assert() 函数用法
- GIT命令中加一些颜色
- 2009-06-04读书记录:《Core Java》第八版卷I之User Interface Components with swing
- 导致iphone os sdk对象不能使用的原因
- 给Repeater、Datalist和Datagrid增加自动编号列
- winsock2.h与afxsock.h的冲突
- 瀑布模型(Waterfall Model)
- iphone bus error的原因
- 电信行业应该如何借助CRM实现精准营销
- bouncycastle mail api实现加密和签名邮件
- 利用.NET中的反射机制实现IList到DataTable的转换