| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 493 人关注过本帖
标题:我的字体选择器的错误
只看楼主 加入收藏
myfor
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:357
专家分:6
注 册:2006-3-13
收藏
 问题点数:0 回复次数:2 
我的字体选择器的错误


import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;

public class FontChooser extends JFrame{
private static final long serialVersionUID = 4164341643068481555L;
private String [] fontnames = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
private String [] fontshape = {"常规", "斜体", "粗体", "粗斜体"};
private String [] fontsizes = {"8","9","10","12","14","16","18","20","22","24","26","28","36","48","72"};
private JLabel lfont, ldes, lsize, show;
private JTextField []jt = new JTextField[3];
private JList font, des, size;
private JButton ok, cancel;
private JPanel p;
private static Font f = null;
//public static void main(String args[]){
//}
private void addc(Component c,
GridBagLayout b,GridBagConstraints g){
b.setConstraints(c,g);
add(c);
}
FontChooser(JFrame j){

lfont = new JLabel("字体:");
ldes = new JLabel("字型:");
lsize = new JLabel("大小:");
show = new JLabel("Java记事本");
p = new JPanel();
ok = new JButton("确定");
cancel = new JButton("取消");
jt[0] = new JTextField();
jt[1] = new JTextField();
jt[2] = new JTextField();

show.setHorizontalAlignment(JLabel.CENTER);
show.setPreferredSize(new Dimension(340,40));
show.setBorder(new BevelBorder(BevelBorder.LOWERED));
p.add(show);
p.setBorder(new TitledBorder(new LineBorder(Color.DARK_GRAY), "示例"));
font = new JList(fontnames);
font.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
font.setVisibleRowCount(6);font.setFixedCellWidth(120);
font.setSelectedIndex(0);
des = new JList(fontshape);
des.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
des.setVisibleRowCount(6);des.setFixedCellWidth(80);
des.setSelectedIndex(0);
size = new JList(fontsizes);
size.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
size.setVisibleRowCount(6);size.setFixedCellWidth(40);
size.setSelectedIndex(0);

GridBagLayout bag = new GridBagLayout();
GridBagConstraints bgc = new GridBagConstraints();
setLayout(bag);
bgc.insets = new Insets(3,5,3,5);
bgc.fill = GridBagConstraints.BOTH;
bgc.gridwidth = 3;
lfont.setLabelFor(font);
addc(lfont, bag, bgc);
bgc.gridwidth = 2;
ldes.setLabelFor(des);
addc(ldes, bag, bgc);
bgc.gridwidth = 1;
bgc.gridwidth = GridBagConstraints.REMAINDER;
lsize.setLabelFor(size);
addc(lsize, bag, bgc);
bgc.gridwidth = 3;
bgc.gridheight = 5;
addc(new JScrollPane(font), bag, bgc);
bgc.gridwidth = 2;
addc(new JScrollPane(des), bag, bgc);
bgc.gridwidth = 1;
addc(new JScrollPane(size), bag, bgc);
bgc.gridheight = 1;
bgc.gridwidth = GridBagConstraints.REMAINDER;
addc(ok, bag, bgc);
bgc.gridwidth = GridBagConstraints.REMAINDER;
addc(cancel, bag, bgc);
bgc.gridx = 0;
bgc.gridwidth = GridBagConstraints.REMAINDER;
addc(p, bag, bgc);

setLocationRelativeTo(j);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
f = null;
dispose();
}
});
ListSelectionListener lsl = new ListSelectionListener(){
public void valueChanged(ListSelectionEvent e) {
changefont();
}
};
font.addListSelectionListener(lsl);
des.addListSelectionListener(lsl);
size.addListSelectionListener(lsl);
ok.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
changefont();
dispose();
}
});
cancel.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
f = null;
dispose();
}
});

validate();
pack();
setResizable(false);
setVisible(true);
}
private void changefont(){
String cfont;
int csize;
int style;
int cdes;
cfont = (String) font.getSelectedValue();
cdes = des.getSelectedIndex();
csize = Integer.parseInt((String) size.getSelectedValue());
switch(cdes){
case 1:style = Font.ITALIC;break;
case 2:style = Font.BOLD;break;
case 3:style = Font.BOLD|Font.ITALIC;break;
default:style = Font.PLAIN;break;
}
f = new Font(cfont, style, csize);
show.setFont(f);
show.repaint();
}
public static Font getFontc(JFrame j){
FontChooser fc = new FontChooser(new JFrame());
System.out.println(fc.f.getFamily()+fc.f.getSize()+fc.f.getStyle());
return fc.f;
}
}


出错行
System.out.println(fc.f.getFamily()+fc.f.getSize()+fc.f.getStyle());
说是空对象错误

如果f初始为一个合法的font
那么我经过一顿操作之后 f的值从这句话看来似乎没变

但是显示面板显示的字是变化的
请大家指点

搜索更多相关主题的帖子: 字体 选择 
2006-12-03 23:36
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 
我以前写过一个字体选择器,你可以搜索一下,参考一下

可惜不是你,陪我到最后
2006-12-04 09:06
myfor
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:357
专家分:6
注 册:2006-3-13
收藏
得分:0 
我参考了你的程序之后才发上来的...待我再去研究一下

广告位招租
2006-12-04 10:19
快速回复:我的字体选择器的错误
数据加载中...
 
   



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

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