「java记事本字体选择器」java记事本字体怎么实现
今天给各位分享java记事本字体选择器的知识,其中也会对java记事本字体怎么实现进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、哪位能在这个java记事本中添加字体设置选项功能?
- 2、哪位能在这个java记事本中添加字体设置选项功能?(上个代码没弄上去)
- 3、用JAVA 编写一个记事本,要有保存和设置字体的功能
- 4、用java编写记事本程序,可以实现新建、打开、保存、退出、复制、粘贴、剪切、全选。
哪位能在这个java记事本中添加字体设置选项功能?
我这有 以前 做的 记事本(实现了 MVC 基本和 notpad功能一样) . 要的话说一声
哪位能在这个java记事本中添加字体设置选项功能?(上个代码没弄上去)
java中没有自带的字体对话框,这需要自己来编写。
text.setFond("字体名字",字形(如,fond.bold),大小)
import java.awt.*;
import java.awt.event.*;
import javax.swing.JColorChooser;
//import javax.swing.border.*;
class ff extends Frame implements ActionListener
{
Choice font,size,bolder;
Button bb;
ff(String s)
{
setTitle(s);
font=new Choice();
bolder=new Choice();
size=new Choice();
//bolder.add加监视器
//font.add加监视器
//size.add加监视器
Panel p1=new Panel();
Panel p2=new Panel();
bb=new Button("点击打开");
bb.addActionListener(this);
p1.setLayout(new GridLayout(4,1));
p2.setLayout(new GridLayout(4,1));
GraphicsEnvironment gg=GraphicsEnvironment.getLocalGraphicsEnvironment();
String ss[]=gg.getAvailableFontFamilyNames();
String bold[]={"Font.BOLD","Font.CENTER_BASELINE","Font.CENTER_BASELINE","Font.ITALIC",
"Font.PLAIN","Font.ROMAN_BASELINE","Font.TRUETYPE_FONT"};
for(int i=126;iss.length;i++)
font.add(ss[i]);
for(int i=12;i=64;i+=2)
{
String w=String.valueOf(i);
size.add(w);
}
for(int i=0;ibold.length;i++)
{
bolder.add(bold[i]);
}
p1.add(new Label("请选择字体"));
p1.add(font);
p1.add(new Label("请选择大小"));
p1.add(size);
p2.add(new Label("请选择字型"));
p2.add(bolder);
p2.add(new Label("请选择字体颜色"));
p2.add(bb);
add(p2,BorderLayout.WEST);
add(p1,BorderLayout.EAST);
setSize(250,150);
setVisible(true);
pack();
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent ee)
{
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e)
{
Color cc=JColorChooser.showDialog(this,"颜色对话框",null);
bb.setBackground(cc);//应用举例
}
}
public class font
{
public static void main(String[] args)
{
new ff("字体对话框");
}
}
用JAVA 编写一个记事本,要有保存和设置字体的功能
import java.awt.*;
import java.io.*;
public class MyClipboard extends Frame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 5541943532647624059L;
private TextArea editor = new TextArea();
private MyFile mf = new MyFile(this);
private MyClipboard1 cb =new MyClipboard1();
private MyFindDialog findDlg = new MyFindDialog(this, editor);
public MyClipboard(String title) {
super(title);
MyMenuBar mb = new MyMenuBar(this);
mb.addMenus(new String[] { "文件", "编辑", "查找", "帮助" });
mb.addMenuItems(0, new String[] { "新建", "打开", "保存", null, "退出" });
mb.addMenuItems(1, new String[] { "剪贴", "复制", "粘贴", "清除", null, "全选" });
mb.addMenuItems(2, new String[] { "查找", null, "查找替换" });
mb.addMenuItems(3, new String[] { "我的记事本信息" });
add(editor);
mb.addActionListener(this);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
MyClipboard.this.dispose();
}
});
}
public void actionPerformed(ActionEvent e) {
String selected = e.getActionCommand();
if (selected.equals("新建"))
editor.setText("");
else if (selected.equals("打开")) {
try {
editor.setText(mf.getData());
} catch (IOException ie) {
}
} else if (selected.equals("保存")) {
try {
mf.setData(editor.getText());
} catch (IOException ie) {
}
} else if (selected.equals("退出")) {
dispose();
} else if (selected.equals("剪贴")) {
cb.setData(editor.getSelectedText());
editor.replaceRange("", editor.getSelectionStart(), editor
.getSelectionEnd());
} else if (selected.equals("复制")) {
cb.setData(editor.getSelectedText());
} else if (selected.equals("粘贴")) {
String str = cb.getData();
editor.replaceRange(str, editor.getSelectionStart(), editor
.getSelectionEnd());
} else if (selected.equals("清除")) {
editor.replaceRange("", editor.getSelectionStart(), editor
.getSelectionEnd());
} else if (selected.equals("全选")) {
editor.setSelectionStart(0);
editor.setSelectionEnd(editor.getText().length());
} else if (selected.equals("查找")) {
findDlg.showFind();
} else if (selected.equals("查找替换")) {
findDlg.showReplace();
}
}
public static void main(String[] args) {
MyClipboard memo = new MyClipboard("我的记事本");
memo.setSize(500, 500);
memo.setLocation(239, 120);
memo.setVisible(true);
}
}
class MyClipboard1 {
private Clipboard cb;
public MyClipboard1() {
cb = Toolkit.getDefaultToolkit().getSystemClipboard();
}
public void setData(String data) {
cb.setContents(new StringSelection(data), null);
}
public String getData() {
Transferable content = cb.getContents(null);
try {
return (String) content.getTransferData(DataFlavor.stringFlavor);
} catch (Exception ue) {
}
return null;
}
}
class MyFile {
private FileDialog fDlg;
public MyFile(Frame parent) {
fDlg = new FileDialog(parent, "", FileDialog.LOAD);
}
private String getPath() {
return fDlg.getDirectory() + "\\" + fDlg.getFile();
}
public String getData() throws IOException {
fDlg.setTitle("打开");
fDlg.setMode(FileDialog.LOAD);
fDlg.setVisible(true);
BufferedReader br = new BufferedReader(new FileReader(getPath()));
StringBuffer sb = new StringBuffer();
String aline;
while ((aline = br.readLine()) != null)
sb.append(aline + '\n');
br.close();
return sb.toString();
}
public void setData(String data) throws IOException {
fDlg.setTitle("保存");
fDlg.setMode(FileDialog.SAVE);
fDlg.setVisible(true);
BufferedWriter bw = new BufferedWriter(new FileWriter(getPath()));
bw.write(data);
bw.close();
}
}
class MyFindDialog extends Dialog implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 4380007102323378083L;
private Label lFind = new Label("查找字符串:");
private Label lReplace = new Label("替换字符串:");
private TextField tFind = new TextField(10);
private TextField tReplace = new TextField(10);
private Button bFind = new Button("查找");
private Button bReplace = new Button("替换");
private TextArea ta;
public MyFindDialog(Frame owner, TextArea ta) {
super(owner, "查找", false);
this.ta = ta;
setLayout(null);
lFind.setBounds(10, 30, 80, 20);
lReplace.setBounds(10, 70, 80, 20);
tFind.setBounds(90, 30, 90, 20);
tReplace.setBounds(90, 70, 90, 20);
bFind.setBounds(190, 30, 80, 20);
bReplace.setBounds(190, 70, 80, 20);
add(lFind);
add(tFind);
add(bFind);
add(lReplace);
add(tReplace);
add(bReplace);
setResizable(false);
bFind.addActionListener(this);
bReplace.addActionListener(this);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
MyFindDialog.this.dispose();
}
});
}
public void showFind() {
setTitle("查找");
setSize(280, 60);
setVisible(true);
}
public void showReplace() {
setTitle("查找替换");
setSize(280, 110);
setVisible(true);
}
private void find() {
String text = ta.getText();
String str = tFind.getText();
int end = text.length();
int len = str.length();
int start = ta.getSelectionEnd();
if (start == end)
start = 0;
for (; start = end - len; start++) {
if (text.substring(start, start + len).equals(str)) {
ta.setSelectionStart(start);
ta.setSelectionEnd(start + len);
return;
}
}
ta.setSelectionStart(end);
ta.setSelectionEnd(end);
}
private void replace() {
String str = tReplace.getText();
if (ta.getSelectedText().equals(tFind.getText()))
ta.replaceRange(str, ta.getSelectionStart(), ta.getSelectionEnd());
else
find();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == bFind)
find();
else if (e.getSource() == bReplace)
replace();
}
}
class MyMenuBar extends MenuBar implements Serializable {
/**
*
*/
private static final long serialVersionUID = 2311645080753585104L;
public MyMenuBar(Frame parent) {
parent.setMenuBar(this);
}
public void addMenus(String[] menus) {
for (int i = 0; i menus.length; i++)
add(new Menu(menus[i]));
}
public void addMenuItems(int menuNumber, String[] items) {
for (int i = 0; i items.length; i++) {
if (items[i] != null)
getMenu(menuNumber).add(new MenuItem(items[i]));
else
getMenu(menuNumber).addSeparator();
}
}
public void addActionListener(ActionListener al) {
for (int i = 0; i getMenuCount(); i++)
for (int j = 0; j getMenu(i).getItemCount(); j++)
getMenu(i).getItem(j).addActionListener(al);
}
}
用java编写记事本程序,可以实现新建、打开、保存、退出、复制、粘贴、剪切、全选。
import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import java.awt.event.*;
import java.awt.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Hashtable;
public class MyText extends JFrame implements ActionListener{
private JLabel lb1;
private JMenuBar mb;
private JMenu 文件, 编辑, 格式, 帮助;
private JMenuItem 新建, 打开, 保存, 退出, 复制, 粘贴, 剪切, 全选, 字体, 关于;
private JTextArea editorArea;
private boolean isDirty = false;
private String strFileName = "未命名";
private static final String EDITOR_NAME = "MyText";
public MyText() {
super();
mb = new JMenuBar();
文件 = new JMenu("文件");
编辑 = new JMenu("编辑");
格式 = new JMenu("格式");
帮助 = new JMenu("帮助");
新建 = new JMenuItem("新建");
新建.addActionListener(this);
打开 = new JMenuItem("打开");
打开.addActionListener(this);
保存 = new JMenuItem("保存");
保存.addActionListener(this);
退出 = new JMenuItem("退出");
退出.addActionListener(this);
复制 = new JMenuItem("复制");
复制.addActionListener(this);
粘贴 = new JMenuItem("粘贴");
粘贴.addActionListener(this);
剪切 = new JMenuItem("剪切");
剪切.addActionListener(this);
全选 = new JMenuItem("全选");
全选.addActionListener(this);
字体 = new JMenuItem("字体");
字体.addActionListener(this);
关于 = new JMenuItem("关于");
关于.addActionListener(this);
mb.add(文件);
mb.add(编辑);
mb.add(格式);
mb.add(帮助);
文件.add(新建);
文件.add(打开);
文件.add(保存);
文件.add(退出);
编辑.add(复制);
编辑.add(粘贴);
编辑.add(剪切);
编辑.add(全选);
格式.add(字体);
帮助.add(关于);
setJMenuBar(mb);
Container container = getContentPane();
editorArea = new JTextArea();
editorArea.setLineWrap(true);
editorArea.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if(!isDirty()){
setDirty(true);
}
}
});
JScrollPane scrollPane = new JScrollPane(editorArea);
container.add(scrollPane);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
dispose();
}
});
setTitle(formatEditorTitle());
setSize(600, 400);
setVisible(true);
}
private boolean isDirty() {
return isDirty;
}
private void setDirty(boolean isDirty) {
this.isDirty = isDirty;
setTitle(formatEditorTitle());
}
public static void main(String args[]) {
@SuppressWarnings("unused")
MyText app = new MyText();
}
public void actionPerformed(ActionEvent e) {
JMenuItem item = (JMenuItem)e.getSource();
if(item.equals(新建)){
if(isDirty()){
int ret = JOptionPane.showConfirmDialog(getContentPane(), "文件内容已经变动,是否保存?", "MyText", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE);
if(ret == JOptionPane.OK_OPTION){
saveFile();
}else if(ret == JOptionPane.CANCEL_OPTION || ret == JOptionPane.CLOSED_OPTION){
return;
}
}
clearEditorArea();
setDirty(false);
}else if(item.equals(打开)){
if(isDirty()){
int ret = JOptionPane.showConfirmDialog(getContentPane(), "文件内容已经变动,是否保存?", "MyText", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE);
if(ret == JOptionPane.OK_OPTION){
saveFile();
}else if(ret == JOptionPane.CANCEL_OPTION || ret == JOptionPane.CLOSED_OPTION){
return;
}
}
openFile();
}else if(item.equals(保存)){
saveFile();
}else if(item.equals(退出)){
dispose();
}else if(item.equals(复制)){
editorArea.copy();
}else if(item.equals(剪切)){
editorArea.cut();
}else if(item.equals(粘贴)){
editorArea.paste();
}else if(item.equals(全选)){
editorArea.selectAll();
}else if(item.equals(字体)){
FontDialog font = new FontDialog(this, editorArea.getFont());
editorArea.setFont(font.getSelectedFont());
}else if(item.equals(关于)){
AboutDialog about = new AboutDialog(this);
about.setVisible(true);
}
}
private String getFileName() {
return strFileName;
}
private void setFileName(String strFileName) {
this.strFileName = strFileName;
}
public String formatEditorTitle(){
StringBuffer strFileNm = new StringBuffer(getFileName());
strFileNm.append(isDirty()?"*":"");
strFileNm.append(" - ");
strFileNm.append(EDITOR_NAME);
return strFileNm.toString();
}
private void clearEditorArea(){
editorArea.selectAll();
editorArea.replaceSelection("");
}
private void openFile(){
JFileChooser openDialog = new JFileChooser();
openDialog.setFileFilter(new TxtFileFilter());
if(openDialog.showOpenDialog(getContentPane()) == JFileChooser.APPROVE_OPTION){
File file = openDialog.getSelectedFile();
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(file));
String buff = br.readLine();
clearEditorArea();
while(buff != null){
editorArea.append(buff);
editorArea.append("\n");
buff = br.readLine();
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally{
try{
if(br != null)
br.close();
} catch (IOException ioe){
ioe.printStackTrace();
}
}
}
}
private void saveFile(){
JFileChooser saveDialog = new JFileChooser();
saveDialog.setFileFilter(new TxtFileFilter());
if(saveDialog.showSaveDialog(getContentPane()) == JFileChooser.APPROVE_OPTION){
File file = saveDialog.getSelectedFile();
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter(file));
String buff = editorArea.getText();
bw.write(buff);
} catch (IOException ioe) {
ioe.printStackTrace();
} finally{
try{
if(bw != null)
bw.close();
} catch (IOException ioe){
ioe.printStackTrace();
}
}
}
}
class TxtFileFilter extends FileFilter{
@Override
public boolean accept(File f) {
return f.isDirectory() || f.getName().toLowerCase().endsWith(".txt");
}
@Override
public String getDescription() {
return "*.txt(文本文件)";
}
}
class FontDialog extends JDialog{
private JComboBox cb_FontSize;
private JComboBox cb_FontStyle;
private JComboBox cb_FontNm;
private Font font;
HashtableInteger, String style = new HashtableInteger, String();
public FontDialog(){
this(null, null);
}
public FontDialog(Frame owner, Font font){
super(owner);
this.font = font == null?getFont():font;
setTitle("字体选择框");
setModal(true);
setResizable(false);
setSize(326, 164);
getContentPane().setLayout(null);
final JLabel lb_FontNm = new JLabel();
lb_FontNm.setText("字体名称");
lb_FontNm.setBounds(10, 10, 66, 16);
getContentPane().add(lb_FontNm);
cb_FontNm = new JComboBox();
cb_FontNm.setBounds(10, 28, 133, 25);
getContentPane().add(cb_FontNm);
cb_FontStyle = new JComboBox();
cb_FontStyle.setBounds(169, 28, 66, 25);
getContentPane().add(cb_FontStyle);
cb_FontSize = new JComboBox();
cb_FontSize.setBounds(258, 28, 53, 25);
getContentPane().add(cb_FontSize);
final JButton btn_OK = new JButton();
btn_OK.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int styleCode = 0;
for(EnumerationInteger i = style.keys();i.hasMoreElements();){
styleCode = i.nextElement();
if(style.get(styleCode).equals(cb_FontStyle.getSelectedItem()))
break;
}
Font font = new Font(cb_FontNm.getSelectedItem().toString(), styleCode, ((Integer)cb_FontSize.getSelectedItem()).intValue());
setSelectedFont(font);
dispose();
}
});
btn_OK.setText("确定");
btn_OK.setBounds(58, 83, 76, 26);
getContentPane().add(btn_OK);
final JButton btn_Cancel = new JButton();
btn_Cancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose();
}
});
btn_Cancel.setText("取消");
btn_Cancel.setBounds(169, 83, 76, 26);
getContentPane().add(btn_Cancel);
final JLabel lb_FontStyle = new JLabel();
lb_FontStyle.setText("字体样式");
lb_FontStyle.setBounds(169, 10, 66, 16);
getContentPane().add(lb_FontStyle);
final JLabel lb_FontSize = new JLabel();
lb_FontSize.setText("字体大小");
lb_FontSize.setBounds(258, 10, 66, 16);
getContentPane().add(lb_FontSize);
init();
setVisible(true);
}
private void init(){
GraphicsEnvironment gg=GraphicsEnvironment.getLocalGraphicsEnvironment();
String ss[]=gg.getAvailableFontFamilyNames();
for(String s : ss)
cb_FontNm.addItem(s);
if(font != null)
cb_FontNm.setSelectedItem(font.getFamily());
style.put(Font.PLAIN, "标准");
style.put(Font.BOLD, "粗体");
style.put(Font.ITALIC, "斜体");
style.put(Font.BOLD+Font.ITALIC, "粗体斜体");
cb_FontStyle.addItem(style.get(Font.PLAIN));
cb_FontStyle.addItem(style.get(Font.BOLD));
cb_FontStyle.addItem(style.get(Font.ITALIC));
cb_FontStyle.addItem(style.get(Font.BOLD+Font.ITALIC));
if(font != null)
cb_FontStyle.setSelectedItem(style.get(font.getStyle()));
for(int i=8;i23;i++)
cb_FontSize.addItem(i);
if(font != null)
cb_FontSize.setSelectedItem(font.getSize());
}
public Font getSelectedFont() {
return font;
}
public void setSelectedFont(Font font) {
this.font = font;
}
}
class AboutDialog extends JDialog{
public AboutDialog(JFrame owner){
super(owner);
setTitle("关于");
setSize(new Dimension(322, 163));
getContentPane().setLayout(null);
final JLabel version = new JLabel();
version.setText("MyText 1.0");
version.setBounds(74, 37, 66, 16);
getContentPane().add(version);
final JLabel copyright = new JLabel();
copyright.setText("Copyright (C) 2010");
copyright.setBounds(74, 59, 188, 16);
getContentPane().add(copyright);
final JSeparator separator = new JSeparator();
separator.setBounds(70, 90, 210, 2);
getContentPane().add(separator);
final JButton okButton = new JButton();
okButton.setBounds(235, 95, 50, 26);
getContentPane().add(okButton);
okButton.setText("Ok");
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose();
}
});
}
}
}
关于java记事本字体选择器和java记事本字体怎么实现的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
发布于:2022-12-23,除非注明,否则均为
原创文章,转载请注明出处。