else if(st.equals("确定")){//弹出字体对话框 if (jtf1.getText().equals("PLAIN"))//如果选择了PLAIN { int fontsize = Integer.parseInt(jtf2.getText());//字体大小 int fontstyle = 0; //0就是一种类型 Font f = new Font("字体设置", fontstyle, fontsize); //设置字体 t.setFont(f);//设置文本域里的字体为f里的参数 } if (jtf1.getText().equals("BOLD")) //同上 { int fontsize = Integer.parseInt(jtf2.getText()); int fontstyle = 1; Font f = new Font("字体设置", fontstyle, fontsize); t.setFont(f); } if (jtf2.getText().equals("ITALIC"))//同上 { int fontsize = Integer.parseInt(jtf2.getText()); int fontstyle = 2; Font f = new Font("字体设置", fontstyle, fontsize); t.setFont(f); } dfornt.dispose(); }有没有不知道,当年我自己是这样写的.
JComboBox fontList = new JComboBox();
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] fonts = ge.getAvailableFontFamilyNames();DefaultComboBoxModel fontModel = new DefaultComboBoxModel(fonts);
fontList.setModel(fontModel);/*
//进行事件处理
......
......
*///import java.awt.*;
//import javax.swing.*;
//import java.awt.event.*;
我也是写了一半就觉的没意思了
放弃了import java.awt.*;
import java.awt.event.*;
import java.io.*;public class MyNote extends Frame{
private MenuBar mb = null;
private TextArea ta = null;
private Menu m1 = null;
private Menu m2 = null;
private Panel p = null;
BufferedReader fr = null;
BufferedWriter fw = null;
MenuItem[] filem = {new MenuItem("新建"),new MenuItem("打开"),new MenuItem("保存"),new MenuItem("另存为"),new MenuItem("退出")};
MenuItem[] editmenu = {new MenuItem("剪切"),new MenuItem("复制"),new MenuItem("粘贴"),new MenuItem("删除"),new MenuItem("全选")};
public MyNote(){
mb = new MenuBar();
ta = new TextArea();
m1 = new Menu("文件");
m2 = new Menu("编辑");
p = new Panel();
for(int i = 0;i < filem.length;i++){
filem[i].addActionListener(new MyActionListener());
m1.add(filem[i]);
}
for(int i = 0;i < editmenu.length;i++){
m2.add(editmenu[i]);
}
mb.add(m1);
mb.add(m2);
this.setMenuBar(mb);
p.setSize(10,20);
add(p,BorderLayout.EAST);
this.add(ta,BorderLayout.CENTER);
}
public void launchFrame(){
this.setTitle("我的记事本");
this.setBounds(400,400,400,400);
pack();
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
}
});
this.setVisible(true);
}
private class MyActionListener implements ActionListener{
public void actionPerformed(ActionEvent e){
String text = ((MenuItem)e.getSource()).getActionCommand();
if(text == "退出"){
System.exit(0);
}else if(text == "打开"){
openFile();
} else if(text == "保存"){
saveFile();
} else if(text == "另存为"){
anotherSaveFile();
}
}
}
public static void main(String[] args){
MyNote mn = new MyNote();
mn.launchFrame();
}
public void saveFile(){
FileDialog fd = new FileDialog(this,"保存文件",FileDialog.SAVE);
File file = null;
fd.setVisible(true);
System.out.println(fd.getDirectory()+fd.getFile());
file = new File(fd.getDirectory()+fd.getFile());
String []str = ta.getText().split("\n");
try{
fw = new BufferedWriter(new FileWriter(file));
for(int i = 0;i < str.length; i++){
fw.write(str[i]);
fw.newLine();
fw.flush();
}
}catch(FileNotFoundException e2){
e2.printStackTrace();
}catch(IOException e1){
e1.printStackTrace();
}finally{
try {
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void openFile(){
FileDialog fd = new FileDialog(this,"打开文件",FileDialog.LOAD);
fd.setVisible(true);
String str = null;
System.out.println(fd.getDirectory()+fd.getFile());
ta.setText("");
try {
fr = new BufferedReader(new FileReader(fd.getDirectory()+fd.getFile()));
while((str =fr.readLine()) != null){
if (ta.getText() == null){
ta.setText(str);
}else {
ta.setText(ta.getText()+"\n"+str);
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
}
public void anotherSaveFile(){
FileDialog fd = new FileDialog(this,"另存为",FileDialog.SAVE);
File file = null;
fd.setVisible(true);
System.out.println(fd.getDirectory()+fd.getFile());
file = new File(fd.getDirectory()+fd.getFile());
String []str = ta.getText().split("\n");
try{
fw = new BufferedWriter(new FileWriter(file));
for(int i = 0;i < str.length; i++){
fw.write(str[i]);
fw.newLine();
fw.flush();
}
}catch(FileNotFoundException e2){
e2.printStackTrace();
}catch(IOException e1){
e1.printStackTrace();
}finally{
try {
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
/*
* 以下代码在 Windows XP + JDK 6 测试通过,2008年11月14日,oracle_lover
*/
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;public class FontTest extends JFrame implements ActionListener { // 主类直接继承了 JFrame,实现了 ActionListener 接口 private static final long serialVersionUID = -2344998755780835481L;
// 获取当前的环境变量
private java.awt.GraphicsEnvironment env = java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment();
// 字体名字,风格,大小的下拉框
private JComboBox cbFontName, cbFontStyle, cbFontHeight;
// 文本编辑框
private JTextArea taEditor; public FontTest() {
super("FontTest"); // 继承父类的构造函数
this.setSize(640, 480); // 设置窗口大小
this.setLocationRelativeTo(null); // 窗口屏幕居中
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 窗口关闭,则程序退出
Container container = this.getContentPane(); // 获取系统中所有可用字体的名字,并加入下拉框
String[] fontNames = env.getAvailableFontFamilyNames();
cbFontName = new JComboBox(fontNames);
// 将三种字体风格加入下拉框
String[] fontStyles = { "普通", "粗体", "斜体" };
cbFontStyle = new JComboBox(fontStyles);
// 将 12~21 字号加入下拉框
String[] fontHeights = new String[10];
for (int i = 12; i < 22; i++)
fontHeights[i - 12] = new String(Integer.toString(i));
cbFontHeight = new JComboBox(fontHeights); // 创建执行按钮,并加入监听
JButton btnGo = new JButton("应用字体");
btnGo.setActionCommand("Apply Font");
btnGo.addActionListener(this); // 创建一个面板,把三个下拉框,按钮放进去,面板默认是 FlowLayout
JPanel paneTop = new JPanel();
paneTop.add(cbFontName);
paneTop.add(cbFontStyle);
paneTop.add(cbFontHeight);
paneTop.add(btnGo); // 创建文本框,并放入滚动条
taEditor = new JTextArea();
taEditor.append("试试字体会不会变");
JScrollPane sp = new JScrollPane(taEditor); // 将各组件加入窗口
container.add(paneTop, BorderLayout.PAGE_START);
container.add(sp, BorderLayout.CENTER);
} public void actionPerformed(ActionEvent event) {
if (event.getActionCommand().equals("Apply Font")) { // 判断是否来自按钮的动作
// 获取字体名字
String fontName = cbFontName.getSelectedItem().toString();
// 获取字体风格,默认是“普通”
int fontStyle;
switch (cbFontStyle.getSelectedIndex()) {
case 0:
fontStyle = Font.PLAIN;
break;
case 1:
fontStyle = Font.BOLD;
break;
case 2:
fontStyle = Font.ITALIC;
break;
default:
fontStyle = Font.PLAIN;
break;
}
// 获取字体大小
int fontHeight = Integer.parseInt(cbFontHeight.getSelectedItem().toString());
// 创建一个新字体
Font font = new Font(fontName, fontStyle, fontHeight);
// 文本框应用新字体
taEditor.setFont(font);
}
} // 创建GUI
private static void createAndShowGUI() {
FontTest xframe = new FontTest();
xframe.setVisible(true);
} public static void main(String[] args) {
// 为事件处理线程安排一个任务:创建并显示程序的GUI
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}}
g2d.setPaint(new Color(R,G,B));//可以设置字体颜色,R,G,B已经赋值国
g2d.setFont(new Font(name,italic+bold,((int)stroke)*18));//name的数组在下面,italic为斜体,bold为粗体,italic+bold 就是2者都有 最后的参数设置粗细,stroke已经赋值,它是单精度型
if(word!=null){
g2d.drawString(word,x1,y1);//这是在面板上画出文本,x1,y1表示坐标
}
} String name[]={
//显示文字的样式
"宋体",
"隶书",
"华文彩云",
"华文行楷",
"方正舒体",
"Times New Roman",
"Serif",
"Monospaced",
"仿宋_GB2312"
};以上是我编写画图版里面用来改变字体格式的一部分,希望能对你有用
这个是完整的记事本代码
图片地址http://hiphotos.baidu.com/sageking2/pic/item/6d658fd9e2f655f538012fc9.jpg
我尝试了,不过我这里怎么都显示不出来,以前设计的好象今天不能用,见鬼了.我用了setVisible(true);也设置了大小.
if (jtf1.getText().equals("PLAIN"))//如果选择了PLAIN
{
int fontsize = Integer.parseInt(jtf2.getText());//字体大小
int fontstyle = 0; //0就是一种类型
Font f = new Font("字体设置", fontstyle, fontsize); //设置字体
t.setFont(f);//设置文本域里的字体为f里的参数
}
if (jtf1.getText().equals("BOLD")) //同上
{
int fontsize = Integer.parseInt(jtf2.getText());
int fontstyle = 1;
Font f = new Font("字体设置", fontstyle, fontsize);
t.setFont(f);
}
if (jtf2.getText().equals("ITALIC"))//同上
{
int fontsize = Integer.parseInt(jtf2.getText());
int fontstyle = 2;
Font f = new Font("字体设置", fontstyle, fontsize);
t.setFont(f);
}
dfornt.dispose();
}有没有不知道,当年我自己是这样写的.