Mini 记事本源程序
目录
1.程序入口 Notepad.java ........................................................................................................ 1 2.工具栏按钮类 ToolBarButton.java ........................................................................................ 1 3.字体对话框类 JFontChooser.java ......................................................................................... 2 4.记事本核心类 FormMain.java ............................................................................................... 6 5.在项目中创建属性文件 notepad.properties ................................................................. 21
说明:本程序是学习 Swing、文件流的练习程序,部分代码没有 没有实现,发现后自己补充;如果测试出 bug,自行改正。
1.程序入口 Notepad.java
package notepad; public class NotePad { public static void main(String[] args) { FormMain win = new FormMain("Mini记事本"); win.setVisible(true); } }
2.工具栏按钮类 ToolBarButton.java
package notepad;
import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JButton; /** * @author 孙丰伟 E‐mail: sunfengweimail@163 * @version 创建时间:May 3, 2008 12:49:50 PM * @see */ public class ToolBarButton extends JButton { public ToolBarButton(Icon icon) { super(icon); setVerticalTextPosition(BOTTOM); setHorizontalTextPosition(CENTER); } public ToolBarButton(String imageFile) { this(new ImageIcon(imageFile)); // this 调用构造方法 } } // end class ToolBarButton
3.字体对话框类 JFontChooser.java
package notepad; import java.awt.BorderLayout; import java.awt.Container; import java.awt.FlowLayout; import java.awt.Font; import java.awt.GraphicsEnvironment; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import javax.swing.*; /****************************************** * 类 名: JFontChooser * 作 者:孙丰伟 E‐mail: sunfengweimail@163 * 时 间:2008‐4‐24 * 描 述:实现简单的字体设置
******************************************/ public class JFontChooser extends JDialog implements ActionListener { private Font font; private
Container contentPane; private JButton btnOK,btnCancel; private JComboBox comboFont,comboSize,comboStyle; private JTextField txtFontName,txtFontSize,txtFontStyle; private Box baseBoxV,fontBox,boxV1,boxV2,boxV3; private JPanel btnPanel,labPanel; private JLabel labFont,labStyle,labSize,labDemo; public JFontChooser(JFrame frame,String title,boolean flag,Font font) { super(frame,title,flag); this.font=font; this.initForm(); } private void initForm() { tPane =&ContentPane(); // // 取系统中支持字体 // GraphicsEnvironment gl =&LocalGraphicsEnvironment(); String[] fontName =&AvailableFontFamilyNames(); comboFont=new JComboBox(fontName); labFont=new JLabel("字体:"); labStyle=new JLabel("样式:"); labSize=new JLabel("大小:"); txtFontName=new Family()); txtFontSize=new JTextField(String.Size())); txtFontStyle=new JTextField(String.Style())); labDemo=new JLabel("Hello,这是字体样例!"); labDemo.setFont(font); labDemo.setHorizontalAlignment(JLabel.LEFT); String[] fontStyle = { "常规", "粗体", "斜体"}; comboStyle=new JComboBox(fontStyle); //大小 comboSize=new JComboBox(); for(int size=8;size<=72;size++) {
notepad++comboSize.addItem(size); } // // 为组合框设置事件 // HandleItemListener itemListener=new HandleItemListener(); comboFont.addItemListener(itemListener); comboStyle.addItemListener(itemListener); comboSize.addItemListener(itemListener); ateVerticalBox(); boxV1.add(labFont); boxV1.add(txtFontName); boxV1.add(comboFont); ateHorizontalBox(); fontBox.add(boxV1); ateVerticalBox(); boxV2.add(labStyle); boxV2.add(txtFontStyle); boxV2.add(comboStyle); ateVerticalBox(); boxV3.add(labSize); boxV3.add(txtFontSize); boxV3.add(comboSize); fontBox.add(boxV2); fontBox.add(boxV3); ateVerticalBox(); baseBoxV.add(fontBox); baseBoxV.ateVerticalStrut(20)); labPanel=new JPanel(); FlowLayout flow=new FlowLayout(); flow.setAlignment(FlowLayout.LEFT); labPanel.setLayout(flow); labPanel.add(labDemo); baseBoxV.add(labPanel);
tPane.add(baseBoxV); btnPanel=new JPanel(); btnOK=new JButton("确定"); btnCancel=new JButton("取消"); btnOK.addActionListener(this); btnCancel.addActionListener(this); btnPanel.add(btnOK); btnPanel.add(btnCancel); tPane.add(btnPanel, BorderLayout.SOUTH); this.setLocationRelativeTo(null); this.set
Resizable(false); this.setSize(450, 200); this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); this.setVisible(true); } class HandleItemListener implements ItemListener { @Override public void itemStateChanged(ItemEvent e) { // TODO Auto‐generated method stub Source()==comboFont) { txtFontName.SelectedItem().toString()); } Source()==comboStyle) { txtFontStyle.SelectedItem().toString()); } Source()==comboSize) { txtFontSize.SelectedItem().toString()); } //setTitle(String.SelectedIndex())); font=font=new Text(),SelectedIndex(),Integer.T ext())); labDemo.setFont(font); }
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论