读书人

Java记事本,该如何解决

发布时间: 2012-03-16 16:34:56 作者: rapoo

Java记事本
各位大哥,大姐们!
我想问一下如何用javax.swing编一个记事本里的字体设置功能,还有替换功能,我想了好久也没有想到,希望大家帮我一下。

[解决办法]
swing中有控件JEditorPane这个可以.
而且JAVA的安装目录下有记事本的例子程序

[解决办法]
JAVA 字体控件

import java.awt.Frame;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JLabel;
import java.awt.Rectangle;
import javax.swing.JTextField;
import javax.swing.JList;
import javax.swing.BorderFactory;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import java.awt.Color;
import javax.swing.JButton;
import java.awt.SystemColor;
import java.awt.GraphicsEnvironment;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.event.*;
import javax.swing.event.*;
import java.awt.Font;

public class FrmFont
extends JDialog {

JLabel jLabel1 = new JLabel();
JTextField txtFontFace = new JTextField();
JLabel jLabel2 = new JLabel();
JTextField txtFontStyle = new JTextField();
JLabel jLabel3 = new JLabel();
JTextField txtFontSize = new JTextField();

JPanel jPanel1 = new JPanel();
Border border1 = BorderFactory.createEmptyBorder();
Border border2 = new TitledBorder(border1, "事例 ");
JButton btnOK = new JButton();
JButton btnCancle = new JButton();
JLabel lblDemo = new JLabel();
Border border3 = BorderFactory.createLineBorder(SystemColor.controlText, 2);
JScrollPane jScrollPane1 = new JScrollPane();
JScrollPane jScrollPane2 = new JScrollPane();
JScrollPane jScrollPane3 = new JScrollPane();
/////////////////////////////////////////
JList lstFontFace;
JList lstFontStyle;
JList lstFontSize;
/////////////////////////////////////////
/////////////////////////////////////////
String curFontFace = "宋体 ";
int curFontStyle = Font.PLAIN;
int curFontSize = 12;

Font font;
JTextArea txtArea;
////////////////////////////////////////
public FrmFont(Frame owner, String title, boolean modal, JTextArea txtArea) {
super(owner, title, modal);
this.txtArea = txtArea;
try {
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
jbInit();
pack();
}
catch (Exception exception) {
exception.printStackTrace();
}
}

public FrmFont(Frame owner, JTextArea txtArea) {
this(owner, "DialogFont ", true, txtArea);

}

private void jbInit() throws Exception {
//取得系统中所有的字体
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] fontFaces = env.getAvailableFontFamilyNames();
String[] fontStyle = {
"常规 ", "斜体 ", "粗体 "};
String[] fontSize = {
"8 ", "9 ", "10 ", "11 ", "12 ", "14 ", "16 ", "18 ", "20 ", "22 ", "24 ", "26 ",
"28 ", "36 ", "48 ", "72 "};
lstFontFace = new JList(fontFaces);
lstFontStyle = new JList(fontStyle);
lstFontSize = new JList(fontSize);

border2 = new TitledBorder(BorderFactory.createEtchedBorder(Color.white,
new Color(148, 145, 140)), "示例 ");


this.getContentPane().setLayout(null);
jLabel1.setText( "字体: ");
jLabel1.setBounds(new Rectangle(11, 37, 51, 24));
jLabel2.setText( "字形: ");
jLabel2.setBounds(new Rectangle(168, 36, 51, 24));
txtFontStyle.setBounds(new Rectangle(168, 64, 90, 21));
jLabel3.setText( "字号: ");
jLabel3.setBounds(new Rectangle(281, 34, 51, 24));
txtFontSize.setBounds(new Rectangle(281, 63, 90, 21));
jPanel1.setBorder(border2);
jPanel1.setBounds(new Rectangle(26, 190, 276, 94));
jPanel1.setLayout(null);
btnCancle.setBounds(new Rectangle(322, 246, 62, 23));
btnCancle.setText( "取消 ");
btnCancle.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
btnCancle_actionPerformed(e);
}
});
lblDemo.setBorder(border3);
lblDemo.setText( " SUN MicroSystem(R) ");
lblDemo.setBounds(new Rectangle(16, 27, 243, 48));
btnOK.setBounds(new Rectangle(321, 203, 62, 23));
btnOK.setText( "确定 ");
btnOK.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
btnOK_actionPerformed(e);
}
});
jScrollPane1.setBounds(new Rectangle(10, 83, 132, 98));
jScrollPane2.setBounds(new Rectangle(167, 87, 91, 98));
jScrollPane3.setBounds(new Rectangle(281, 85, 91, 98));
lstFontFace.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
lstFontFace_valueChanged(e);
}
});
lstFontStyle.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
lstFontStyle_valueChanged(e);
}
});
lstFontSize.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
lstFontSize_valueChanged(e);
}
});
this.getContentPane().add(jLabel1);
this.getContentPane().add(txtFontFace);
this.getContentPane().add(jPanel1);
jPanel1.add(lblDemo);
this.getContentPane().add(btnOK);
this.getContentPane().add(btnCancle);
this.getContentPane().add(jScrollPane1);
jScrollPane1.getViewport().add(lstFontFace);
this.getContentPane().add(jLabel3);
this.getContentPane().add(txtFontStyle);
this.getContentPane().add(txtFontSize);
this.getContentPane().add(jLabel2);
this.getContentPane().add(jScrollPane3);
this.getContentPane().add(jScrollPane2);
jScrollPane2.getViewport().add(lstFontStyle);
jScrollPane3.getViewport().add(lstFontSize);
txtFontFace.setBounds(new Rectangle(10, 63, 132, 21));
this.setBounds(100, 200, 400, 300);

this.setVisible(true);
}

public void lstFontFace_valueChanged(ListSelectionEvent e) {
this.txtFontFace.setText(lstFontFace.getSelectedValue().toString());
this.curFontFace = lstFontFace.getSelectedValue().toString();
font = new Font(this.curFontFace, this.curFontStyle, this.curFontSize);
this.lblDemo.setFont(font);

}

public void lstFontStyle_valueChanged(ListSelectionEvent e) {
String style = lstFontStyle.getSelectedValue().toString();
this.txtFontStyle.setText(style);

if (style.equals( "常规 ")) {
this.curFontStyle = Font.PLAIN;
}
if (style.equals( "粗体 ")) {
this.curFontStyle = Font.BOLD;
}
if (style.equals( "斜体 ")) {
this.curFontStyle = Font.ITALIC;


}
font = new Font(this.curFontFace, this.curFontStyle, this.curFontSize);
this.lblDemo.setFont(font);

}

public void lstFontSize_valueChanged(ListSelectionEvent e) {

int size = Integer.parseInt(lstFontSize.getSelectedValue().toString());
this.txtFontSize.setText(lstFontSize.getSelectedValue().toString());
this.curFontSize = size;
font = new Font(this.curFontFace, this.curFontStyle, this.curFontSize);
this.lblDemo.setFont(font);
}

public void btnOK_actionPerformed(ActionEvent e) {
txtArea.setFont(font);
this.dispose();
}

public void btnCancle_actionPerformed(ActionEvent e) {
this.dispose();
}
}


读书人网 >J2SE开发

热点推荐