我要做一个字体选择的窗口,请大家帮忙!

xxisxx 2003-11-28 10:42:20
我要做一个字体选择的窗口,请大家帮忙!
就和windows风格一样的,就可以,谁又简单的例子,让我参考参考
多谢
...全文
34 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
dfmo 2004-01-15
  • 打赏
  • 举报
回复
对了,上面的代码在JBuilder8中不太好用,但是在JBuilder7中正常。
dfmo 2004-01-14
  • 打赏
  • 举报
回复
//我也在做,我做出来了一个,不过独立运行好用,一用到其它工程中
//在用到中文字体时就不好用了,你研究一下吧。
//研究出来告诉我一声哦。
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.text.*;

/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/

public class FontColorChooser extends JDialog {
SimpleAttributeSet attributes; //属性集
JPanel panel1 = new JPanel();
BorderLayout borderLayout1 = new BorderLayout();
JPanel jPanel1 = new JPanel();
JPanel jPanel2 = new JPanel();
JPanel jPanel3 = new JPanel();
JPanel jPanel4 = new JPanel();
JPanel jPanel5 = new JPanel();
JColorChooser colorChooser = new JColorChooser(Color.black);
BorderLayout borderLayout2 = new BorderLayout();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JLabel jLabel1 = new JLabel();
JComboBox jComboBox1;
JLabel jLabel2 = new JLabel();
JLabel jLabel3 = new JLabel();
JComboBox jComboBox2;
JCheckBox jCheckBox1 = new JCheckBox();
JCheckBox jCheckBox2 = new JCheckBox();
BorderLayout borderLayout3 = new BorderLayout();

public FontColorChooser(Frame frame, String title, boolean modal) {
super(frame, title, modal);
try {
jbInit();
pack();
}
catch (Exception ex) {
ex.printStackTrace();
}
}

public FontColorChooser() {
this(null, "", false);
}

private void jbInit() throws Exception {
//添加所有字体
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
//String[] defaultFonts = (java.awt.Toolkit.getDefaultToolkit().getFontList());
String[] systemFonts = ge.getAvailableFontFamilyNames();
//String[] allFonts = new String[defaultFonts.length + systemFonts.length];
jComboBox1 = new JComboBox(systemFonts);

//设置字体大小
String[] strFontSize = {"8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25"};
jComboBox2 = new JComboBox(strFontSize);

attributes = new SimpleAttributeSet();

this.setSize(400, 500);
panel1.setLayout(borderLayout1);
jPanel1.setBorder(BorderFactory.createEtchedBorder());
jPanel2.setBorder(BorderFactory.createEtchedBorder());
jPanel2.setLayout(borderLayout3);
jPanel3.setBorder(BorderFactory.createEtchedBorder());
jPanel3.setDebugGraphicsOptions(0);
jPanel3.setLayout(borderLayout2);
jButton1.setText("确定");
jButton2.setText("取消");
jLabel1.setText("这是字体的样本。Here is a sample of this font.");
jLabel2.setText("字体");
jLabel3.setRequestFocusEnabled(true);
jLabel3.setText("大小");
jCheckBox1.setText("加粗");
jCheckBox1.addActionListener(new FontColorChooser_jCheckBox1_actionAdapter(this));
jCheckBox2.setText("倾斜");
jCheckBox2.addActionListener(new FontColorChooser_jCheckBox2_actionAdapter(this));
this.setModal(true);
jComboBox1.addActionListener(new FontColorChooser_jComboBox1_actionAdapter(this));
jComboBox2.addActionListener(new FontColorChooser_jComboBox2_actionAdapter(this));
getContentPane().add(panel1);
panel1.add(jPanel1, BorderLayout.NORTH);
jPanel1.add(jLabel2, null);
jPanel1.add(jComboBox1, null);
jPanel1.add(jLabel3, null);
jPanel1.add(jComboBox2, null);
jPanel1.add(jCheckBox1, null);
jPanel1.add(jCheckBox2, null);
panel1.add(jPanel2, BorderLayout.CENTER);
jPanel2.add(colorChooser, BorderLayout.CENTER);
this.getContentPane().add(jPanel3, BorderLayout.SOUTH);
jPanel3.add(jPanel4, BorderLayout.NORTH);
jPanel4.add(jLabel1, null);
jPanel3.add(jPanel5, BorderLayout.CENTER);
jPanel5.add(jButton1, null);
jPanel5.add(jButton2, null);
colorChooser.getSelectionModel().addChangeListener(new javax.swing.
event.ChangeListener() {
public void stateChanged(javax.swing.event.ChangeEvent e) {
updatePreviewColor();
}
});
}

public void updatePreviewColor(){
jLabel1.setForeground(colorChooser.getColor());
}

public void updatePreviewFont() {
String name = StyleConstants.getFontFamily(attributes);
boolean bold = StyleConstants.isBold(attributes);
boolean ital = StyleConstants.isItalic(attributes);
int size = StyleConstants.getFontSize(attributes);

//Bold and italic don't work properly in beta 4.
Font f = new Font(name, (bold ? Font.BOLD : 0) +
(ital ? Font.ITALIC : 0), size);
jLabel1.setFont(f);
jLabel1.setFont(f);
jLabel1.repaint();
}


public static void main(String[] arg) {
FontColorChooser fontColorChooser = new FontColorChooser();
fontColorChooser.show();
}

void jComboBox1_actionPerformed(ActionEvent e) {
StyleConstants.setFontFamily(attributes, (String)jComboBox1.getSelectedItem());
updatePreviewFont();
}

void jComboBox2_actionPerformed(ActionEvent e) {
StyleConstants.setFontSize(attributes, Integer.parseInt((String)jComboBox2.getSelectedItem()));
updatePreviewFont();
}

void jCheckBox1_actionPerformed(ActionEvent e) {
StyleConstants.setBold(attributes, jCheckBox1.isSelected());
updatePreviewFont();
}

void jCheckBox2_actionPerformed(ActionEvent e) {
StyleConstants.setItalic(attributes, jCheckBox2.isSelected());
updatePreviewFont();
}
}

class FontColorChooser_jComboBox1_actionAdapter implements java.awt.event.ActionListener {
FontColorChooser adaptee;

FontColorChooser_jComboBox1_actionAdapter(FontColorChooser adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jComboBox1_actionPerformed(e);
}
}

class FontColorChooser_jComboBox2_actionAdapter implements java.awt.event.ActionListener {
FontColorChooser adaptee;

FontColorChooser_jComboBox2_actionAdapter(FontColorChooser adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jComboBox2_actionPerformed(e);
}
}

class FontColorChooser_jCheckBox1_actionAdapter implements java.awt.event.ActionListener {
FontColorChooser adaptee;

FontColorChooser_jCheckBox1_actionAdapter(FontColorChooser adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jCheckBox1_actionPerformed(e);
}
}

class FontColorChooser_jCheckBox2_actionAdapter implements java.awt.event.ActionListener {
FontColorChooser adaptee;

FontColorChooser_jCheckBox2_actionAdapter(FontColorChooser adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jCheckBox2_actionPerformed(e);
}
}
xxisxx 2003-11-28
  • 打赏
  • 举报
回复
感谢各位,我自己顶一下

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧