| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 660 人关注过本帖
标题:[求助]字体对话框异常
取消只看楼主 加入收藏
非凡DZ
Rank: 1
等 级:新手上路
帖 子:231
专家分:0
注 册:2007-4-6
收藏
 问题点数:0 回复次数:2 
[求助]字体对话框异常

下面的程序一运行就有异常,请问是为什么,哪里出了问题?请高手指点
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编辑过]

搜索更多相关主题的帖子: 对话框 字体 
2007-07-31 15:31
非凡DZ
Rank: 1
等 级:新手上路
帖 子:231
专家分:0
注 册:2007-4-6
收藏
得分:0 
我这段是一个对话框 有其他窗体调用它

2007-07-31 15:50
非凡DZ
Rank: 1
等 级:新手上路
帖 子:231
专家分:0
注 册:2007-4-6
收藏
得分:0 

跟那段代码应该没有关系,那是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不发生变化


2007-07-31 17:06
快速回复:[求助]字体对话框异常
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017373 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved