我现在做的是画了三个下拉框,第一个取得所有字体,第二个位所有字形,第三个为字号,我想在文本框中实现预览,请问该在哪里写事件呢
请高手给点思路,我现在能得到三个下拉框的值
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.setEnabled(false);
txtFont.setEditable(false);
txtFont.setText("");
txtFont.setBounds(new Rectangle(9, 37, 88, 21));
txtType.setEnabled(false);
txtType.setEditable(false);
txtType.setText("");
txtType.setBounds(new Rectangle(138, 37, 88, 21));
txtSize.setEnabled(false);
txtSize.setEditable(false);
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);
//font=new Font(cboFont.getSelectedItem().toString(),number,size[cboSize.getSelectedIndex()]);
}
public void btnDecide_actionPerformed(ActionEvent e) {//确定事件
String font=txtFont.getText();//获得用户选定的字体类型
if(txtType.getText().equals("常规")){//获得用户选定的字形类型
number=Font.PLAIN;
}else if(txtType.getText().equals("斜体")){
number=Font.ITALIC;
}else if(txtType.getText().equals("粗体")){
number=Font.BOLD;
}else{
number=Font.BOLD+Font.ITALIC;
}
Integer temp=new Integer(txtSize.getText());
int size=temp.intValue();//获得用户选定的字体大小
f.font=font;//将构造Font的三个参数传给Frame1
f.type=number;
f.size=size;
f.fontSet();//调用Frame1中的字体设置方法,将文本区域中的字体改变
dispose();//关闭字体对话框
}
public void cboFont_actionPerformed(ActionEvent e) {//字体下拉菜单事件
i=cboFont.getSelectedIndex();
txtFont.setText(fonts[i]);
font=new Font(cboFont.getSelectedItem().toString(),number,size[cboSize.getSelectedIndex()]);
txtExample.setFont(font);
}
public void cboType_actionPerformed(ActionEvent e) {//字形下拉菜单事件
j=cboType.getSelectedIndex();
txtType.setText(type[j]);
font=new Font(cboFont.getSelectedItem().toString(),number,size[cboSize.getSelectedIndex()]);
txtExample.setFont(font);
}
public void cboSize_actionPerformed(ActionEvent e) {//字号下拉菜单事件
k=cboSize.getSelectedIndex();
String b = Integer.toString(size[k]);
txtSize.setText(b);
font=new Font(cboFont.getSelectedItem().toString(),number,size[cboSize.getSelectedIndex()]);
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);
}
}
这位仁兄 你看看我这么写有什么不妥 我现在运行字体对话框有异常 我实在不知怎么改了