package 记事本;
import java.awt.BorderLayout;
import java.awt.Frame;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.*;
import 记事本.Frame1;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class DialogFont extends JDialog {
JPanel panel1 = new JPanel();
JLabel lblFont = new JLabel();
JLabel lblType = new JLabel();
JLabel lblSize = new JLabel();
static Frame1 f;
JTextField txtFont = new JTextField();
JTextField txtType = new JTextField();
JTextField txtSize = new JTextField();
JComboBox cboFont = new JComboBox();
JComboBox cboType = new JComboBox();
JComboBox cboSize = new JComboBox();
JButton btnDecide = new JButton();
JButton btnCancel = new JButton();
JPanel jPanel1 = new JPanel();
JTextField txtExample = new JTextField();
JLabel lblExample = new JLabel();
int i,j,k,number;
Font font;
boolean decide=false;
//得到系统中的所有字体,将返回值放到fonts字符串数组中
String[] fonts=GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
String[] type={"常规","斜体","粗体","粗斜体"};
int[] size={8,9,10,11,12,14,16,18,20,22,24,26,28,36,48,72};
public DialogFont(Frame owner, String title, boolean modal) {
super(owner, title, modal);
try {
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
jbInit();
pack();
} catch (Exception exception) {
exception.printStackTrace();
}
}
public DialogFont() {
this(new Frame(), "DialogFont", false);
}
private void jbInit() throws Exception {
panel1.setLayout(null);
this.setResizable(false);
this.setTitle("字体");
lblFont.setFont(new java.awt.Font("Dialog", Font.PLAIN, 12));
lblFont.setText("字体");
lblFont.setBounds(new Rectangle(9, 12, 54, 25));
lblType.setFont(new java.awt.Font("Dialog", Font.PLAIN, 12));
lblType.setText("字形");
lblType.setBounds(new Rectangle(138, 12, 54, 25));
lblSize.setFont(new java.awt.Font("Dialog", Font.PLAIN, 12));
lblSize.setText("大小");
lblSize.setBounds(new Rectangle(261, 12, 54, 25));
txtFont.setText("");
txtFont.setBounds(new Rectangle(9, 37, 88, 21));
txtType.setText("");
txtType.setBounds(new Rectangle(138, 37, 88, 21));
txtSize.setText("");
txtSize.setBounds(new Rectangle(261, 37, 88, 21));
cboFont.setBounds(new Rectangle(9, 72, 88, 21));
cboFont.addActionListener(new DialogFont_cboFont_actionAdapter(this));
cboType.setBounds(new Rectangle(138, 72, 88, 21));
cboType.addActionListener(new DialogFont_cboType_actionAdapter(this));
cboSize.setBounds(new Rectangle(261, 72, 88, 21));
cboSize.addActionListener(new DialogFont_cboSize_actionAdapter(this));
btnDecide.setBounds(new Rectangle(281, 113, 62, 29));
btnDecide.setFont(new java.awt.Font("Dialog", Font.PLAIN, 12));
btnDecide.setText("确定");
btnDecide.addActionListener(new DialogFont_btnDecide_actionAdapter(this));
btnCancel.setBounds(new Rectangle(281, 157, 62, 29));
btnCancel.setFont(new java.awt.Font("Dialog", Font.PLAIN, 12));
btnCancel.setText("取消");
btnCancel.addActionListener(new DialogFont_btnCancel_actionAdapter(this));
jPanel1.setBorder(BorderFactory.createEtchedBorder());
jPanel1.setBounds(new Rectangle(82, 114, 165, 77));
jPanel1.setLayout(null);
txtExample.setFont(new java.awt.Font("Dialog", Font.PLAIN, 15));
txtExample.setText("我是个例子");
txtExample.setHorizontalAlignment(SwingConstants.CENTER);
txtExample.setBounds(new Rectangle(14, 8, 137, 61));
lblExample.setFont(new java.awt.Font("Dialog", Font.PLAIN, 12));
lblExample.setText("示例");
lblExample.setBounds(new Rectangle(27, 124, 39, 43));
panel1.add(lblFont);
panel1.add(lblType);
panel1.add(txtType);
panel1.add(txtFont);
panel1.add(cboFont);
panel1.add(cboType);
panel1.add(txtSize);
panel1.add(lblSize);
panel1.add(cboSize);
panel1.add(jPanel1);
jPanel1.add(txtExample);
panel1.add(btnDecide);
panel1.add(btnCancel);
panel1.add(lblExample);
for(i=0;i<fonts.length;i++){
cboFont.addItem(fonts[i]);//将fonts数组中的所有字体添加到cboFont中
}
for(j=0;j<type.length;j++){//设置字形选项
cboType.addItem(type[j]);
}
for(k=0;k<size.length;k++){ //设置字号选项
String b = Integer.toString(size[k]);
cboSize.addItem(b);
}
this.getContentPane().add(panel1, java.awt.BorderLayout.CENTER);
if(cboType.getSelectedIndex()==0){//当选择常规字形时
number=Font.PLAIN;
}else if(cboType.getSelectedIndex()==1){//当选择斜体字形时
number=Font.ITALIC;
}else if(cboType.getSelectedIndex()==2){//当选择粗体字形时
number=Font.BOLD;
}else if(cboType.getSelectedIndex()==3){//当选择粗斜体字形时
number=Font.BOLD+Font.ITALIC;
}
font=new Font(cboFont.getSelectedItem().toString(),number,size[cboSize.getSelectedIndex()]);
}
public void btnDecide_actionPerformed(ActionEvent e) {//确定事件
}
public void cboFont_actionPerformed(ActionEvent e) {//字体下拉菜单事件
i=cboFont.getSelectedIndex();
txtFont.setText(fonts[i]);
txtExample.setFont(font);
}
public void cboType_actionPerformed(ActionEvent e) {//字形下拉菜单事件
j=cboType.getSelectedIndex();
txtType.setText(type[j]);
txtExample.setFont(font);
}
public void cboSize_actionPerformed(ActionEvent e) {//字号下拉菜单事件
k=cboSize.getSelectedIndex();
String b = Integer.toString(size[k]);
txtSize.setText(b);
txtExample.setFont(font);
}
public void btnCancel_actionPerformed(ActionEvent e) {//取消事件
dispose();//关闭字体对话框
}
}
class DialogFont_btnCancel_actionAdapter implements ActionListener {
private DialogFont adaptee;
DialogFont_btnCancel_actionAdapter(DialogFont adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btnCancel_actionPerformed(e);
}
}
class DialogFont_cboSize_actionAdapter implements ActionListener {
private DialogFont adaptee;
DialogFont_cboSize_actionAdapter(DialogFont adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.cboSize_actionPerformed(e);
}
}
class DialogFont_cboType_actionAdapter implements ActionListener {
private DialogFont adaptee;
DialogFont_cboType_actionAdapter(DialogFont adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.cboType_actionPerformed(e);
}
}
class DialogFont_cboFont_actionAdapter implements ActionListener {
private DialogFont adaptee;
DialogFont_cboFont_actionAdapter(DialogFont adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.cboFont_actionPerformed(e);
}
}
class DialogFont_btnDecide_actionAdapter implements ActionListener {
private DialogFont adaptee;
DialogFont_btnDecide_actionAdapter(DialogFont adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btnDecide_actionPerformed(e);
}
}
上面是我记事本中的字体对话框,请大家帮助看看,现在只能做一次改变,然后txtExample中的文本就变成乱码了,请高手指点,我的问题处在什么地方?谢谢
----------------解决方案--------------------------------------------------------