下面的程序一运行就有异常,请问是为什么,哪里出了问题?请高手指点
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.*;
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;
Font font;
//得到系统中的所有字体,将返回值放到fonts字符串数组中
String[] fonts=GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
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));
cboType.setBounds(new Rectangle(138, 72, 88, 21));
cboSize.setBounds(new Rectangle(261, 72, 88, 21));
btnDecide.setBounds(new Rectangle(281, 113, 62, 29));
btnDecide.setFont(new java.awt.Font("Dialog", Font.PLAIN, 12));
btnDecide.setText("确定");
btnCancel.setBounds(new Rectangle(281, 157, 62, 29));
btnCancel.setFont(new java.awt.Font("Dialog", Font.PLAIN, 12));
btnCancel.setText("取消");
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中
}
cboType.addItem("常规"); //设置字形选项
cboType.addItem("斜体");
cboType.addItem("粗体");
cboType.addItem("粗斜体");
for(j=0;j<size.length;j++){ //设置字号选项
String b = Integer.toString(size[j]);
cboSize.addItem(b);
}
this.getContentPane().add(panel1, java.awt.BorderLayout.CENTER);
if(cboType.getSelectedIndex()==0){
txtExample.setFont(new Font(fonts[i],Font.PLAIN,size[j]));
}
}
}
[此贴子已经被作者于2007-7-31 15:51:03编辑过]
----------------解决方案--------------------------------------------------------
没有main方法``````````
----------------解决方案--------------------------------------------------------
我这段是一个对话框 有其他窗体调用它
----------------解决方案--------------------------------------------------------
那你把异常贴出来啊,不然人家怎么知道.
----------------解决方案--------------------------------------------------------
public DialogFont(Frame owner, String title, boolean modal) {
super(owner, title, modal);你调到这里不会成死循环了吗?
public DialogFont() { //应该放在这里吧.
this(new Frame(), "DialogFont", false);
}
----------------解决方案--------------------------------------------------------
记事本设计,学习学习。。。
----------------解决方案--------------------------------------------------------
跟那段代码应该没有关系,那是jbuilder自动生成的
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;
Font font;
boolean decide=false;
//得到系统中的所有字体,将返回值放到fonts字符串数组中
String[] fonts=GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
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));
cboType.setBounds(new Rectangle(138, 72, 88, 21));
cboSize.setBounds(new Rectangle(261, 72, 88, 21));
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("取消");
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中
}
cboType.addItem("常规"); //设置字形选项
cboType.addItem("斜体");
cboType.addItem("粗体");
cboType.addItem("粗斜体");
for(j=0;j<size.length;j++){ //设置字号选项
String b = Integer.toString(size[j]);
cboSize.addItem(b);
}
this.getContentPane().add(panel1, java.awt.BorderLayout.CENTER);
i=cboFont.getSelectedIndex();
j = cboSize.getSelectedIndex();
if (cboType.getSelectedIndex() == 0) {
txtExample.setFont(new Font(fonts[i], Font.PLAIN, size[j]));
} else if (cboType.getSelectedIndex() == 1) {
txtExample.setFont(new Font(fonts[i], Font.ITALIC, size[j]));
} else if (cboType.getSelectedIndex() == 2) {
txtExample.setFont(new Font(fonts[i], Font.BOLD, size[j]));
} else if (cboType.getSelectedIndex() == 3) {
txtExample.setFont(new Font(fonts[i], Font.BOLD + Font.ITALIC,size[j]));
}
}
public void btnDecide_actionPerformed(ActionEvent e) {//确定事件
decide=true;
}
}
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不发生变化
----------------解决方案--------------------------------------------------------