[摘要]New 菜单处理器jMenuItem1_actionPerformed()的if模块的最后一行; ⑤当由于用户输入,dirty标志在干净的文件中第一次被设置为true时。每一个文本事件处理器都应...
New 菜单处理器jMenuItem1_actionPerformed()的if模块的最后一行;
⑤当由于用户输入,dirty标志在干净的文件中第一次被设置为true时。每一个文本事件处理器都应该改变为:
void document1_changedUpdate(DocumentEvent e) {
if (!dirty) {
dirty = true;
updateCaption();
}
}
void document1_insertUpdate(DocumentEvent e) {
if (!dirty) {
dirty = true;
updateCaption();
}
}
void document1_removeUpdate(DocumentEvent e) {
if (!dirty) {
dirty = true;
updateCaption();
}
}
2、TextEditclass.java的源程序代码:
package texteditor;
import javax.swing.UIManager;
import java.awt.*;
/**
* <p>Title: TextEditor</p>
* <p>Description: This is a study programme</p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: ghq</p>
* @author ghq
* @version 1.0
*/
public class TextEditClass {
boolean packFrame = false;
//Construct the application
public TextEditClass() {
TextEditFrame frame = new TextEditFrame();
//Validate frames that have preset sizes
//Pack frames that have useful preferred size info, e.g. from their layout
if (packFrame) {
frame.pack();
}
else {
frame.validate();
}
//Center the window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
frame.setVisible(true);
}
//Main method
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
}
catch(Exception e) {
e.printStackTrace();
}
new TextEditClass();
}
}
上面的这段程序主要是构建 TextEditorFrame 的主窗口和主方法入口(main ()),它有下面2点编程技巧说明:
1) 设置外观及基调
设计时的外观和基调
如果已经把 Jbuilder的外观和基调从其默认值改变了,则在开始使用UI设计器之前,在Jbuilder 内让UI 设计器使用 Metal Look & Feel(金属外观和基调),也可以使用其它外观和基调,但本例中选择金属外观和基调,这时一种适合于跨平台设计的选择。
运行时的外观和基调
在设计器的弹出菜单或Jbuilder环境选项对话框中设置的外观和基调对运行时的用户界面(UI)没有任何影响。要强制一个运行时的外观和基调,必须在应用程序(本例即为TextEditClass.java)的类的主方法(main())中设置。
作为默认,应用向导会在可运行类的main()方法中生成下列一行代码:
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
其含义是运行时的外观采用主机系统使用的外观。
若是想要CDE/Motif 或 Windows 式的外观则参数应改变为:
UIManager.setLookAndFeel
("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
或
UIManager.setLookAndFeel
("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
2) 窗口的定义
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
frame.setVisible(true);
3、TextEditFrame_AboutBox.java的源程序代码:
package texteditor;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
/**
* <p>Title: TextEditor</p>
* <p>Description: This is a study programme</p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: ghq</p>
* @author ghq
* @version 1.0
*/
public class TextEditFrame_AboutBox extends JDialog implements
ActionListener {
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel insetsPanel1 = new JPanel();
JPanel insetsPanel2 = new JPanel();
JPanel insetsPanel3 = new JPanel();
JButton button1 = new JButton();
JLabel imageControl1 = new JLabel();
ImageIcon imageIcon;
JLabel label1 = new JLabel();
JLabel label2 = new JLabel();
JLabel label3 = new JLabel();
JLabel label4 = new JLabel();
BorderLayout borderLayout1 = new BorderLayout();
BorderLayout borderLayout2 = new BorderLayout();
FlowLayout flowLayout1 = new FlowLayout();
FlowLayout flowLayout2 = new FlowLayout();
GridLayout gridLayout1 = new GridLayout();
String product = "TextEditor";
String version = "1.0";
String copyright = "Copyright (c) 2002";
String comments = "This is a study programme";
//Construct the frame
public TextEditFrame_AboutBox(Frame parent) {
super(parent);
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
pack();
}
//Component initialization
private void jbInit() throws Exception {
//imageLabel.setIcon(new ImageIcon(TextEditFrame_AboutBox.class.getResource("[Your Image]")));
this.setTitle("About");
setResizable(false);
panel1.setLayout(borderLayout1);
panel2.setLayout(borderLayout2);
insetsPanel1.setLayout(flowLayout1);
insetsPanel2.setLayout(flowLayout1);
insetsPanel2.setBorder(new EmptyBorder(10, 10, 10, 10));
gridLayout1.setRows(4);
gridLayout1.setColumns(1);
label1.setText(product);
label2.setText(version);
label3.setText(copyright);
label4.setText(comments);
insetsPanel3.setLayout(gridLayout1);
insetsPanel3.setBorder(new EmptyBorder(10, 60, 10, 10));
button1.setText("Ok");
button1.addActionListener(this);
insetsPanel2.add(imageControl1, null);
panel2.add(insetsPanel2, BorderLayout.WEST);
this.getContentPane().add(panel1, null);
insetsPanel3.add(label1, null);
insetsPanel3.add(label2, null);
insetsPanel3.add(label3, null);
insetsPanel3.add(label4, null);
panel2.add(insetsPanel3, BorderLayout.CENTER);
insetsPanel1.add(button1, null);
panel1.add(insetsPanel1, BorderLayout.SOUTH);
panel1.add(panel2, BorderLayout.NORTH);
}
//Overridden so we can exit when windows is cancel
protected void processWindowEvent(WindowEvent e) {
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
cancel();
}
super.processWindowEvent(e);
}
void cancel() {
dispose();
}
// Help
关键词:JBuilder 9 开发一个文本编辑器