当前位置: 代码迷 >> Java相关 >> 我的字体选择器的错误
  详细解决方案

我的字体选择器的错误

热度:211   发布时间:2006-12-03 23:36:50.0
我的字体选择器的错误


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的值从这句话看来似乎没变

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

搜索更多相关的解决方案: 字体  选择  

----------------解决方案--------------------------------------------------------
我以前写过一个字体选择器,你可以搜索一下,参考一下

----------------解决方案--------------------------------------------------------
我参考了你的程序之后才发上来的...待我再去研究一下
----------------解决方案--------------------------------------------------------
  相关解决方案