📄 zfontchooser.java
字号:
/*
* Copyright 2002 EZCell , Inc. All rights reserved.
* Version 1.0.
* Author W.John
*/
/*
* put your module comment here
* formatted with JxBeauty (c) johann.langhofer@nextra.at
*/
package ezcell;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsEnvironment;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListModel;
import javax.swing.border.EtchedBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2001</p>
* <p>Company: </p>
* @author unascribed
* @version 1.0
*/
public class ZFontChooser extends JDialog {
static private ZFontChooser sharedInstance;
protected int option = JOptionPane.CLOSED_OPTION;
protected OpenList fontNames;
protected OpenList fontSizes;
protected JCheckBox boldCheck;
protected JCheckBox italicCheck;
protected JCheckBox outlineCheck;
protected ZColorPicker outlineColor;
protected _Preview preview;
public ZFontChooser(JFrame owner) {
super(owner, "Font", true);
getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
// create font names and sizes
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] names = ge.getAvailableFontFamilyNames();
String[] sizes = new String[] {
"8", "9", "10", "11", "12", "14", "16", "18", "20", "22", "24", "26", "28", "36", "48", "72"
};
JPanel p = new JPanel(new GridLayout(1, 2, 10, 2));
p.setBorder(new TitledBorder(new EtchedBorder(), "Font"));
fontNames = new OpenList(names, "Name:");
p.add(fontNames);
fontSizes = new OpenList(sizes, "Size:");
p.add(fontSizes);
getContentPane().add(p);
p = new JPanel(new GridLayout(2, 3, 10, 5));
p.setBorder(new TitledBorder(new EtchedBorder(), "Effects"));
boldCheck = new JCheckBox("Bold");
p.add(boldCheck);
italicCheck = new JCheckBox("Italic");
p.add(italicCheck);
outlineCheck = new JCheckBox("Outline");
p.add(outlineCheck);
outlineColor = new ZColorPicker();
p.add(outlineColor);
getContentPane().add(p);
p = new JPanel(new BorderLayout());
p.setBorder(new TitledBorder(new EtchedBorder(), "Preview"));
preview = new _Preview();
preview.setBackground(Color.white);
preview.setForeground(Color.black);
preview.setOpaque(true);
preview.setBorder(new LineBorder(Color.black));
preview.setPreferredSize(new Dimension(120, 40));
p.add(preview, BorderLayout.CENTER);
getContentPane().add(p);
p = new JPanel(new FlowLayout());
JPanel p1 = new JPanel(new GridLayout(1, 2, 10, 2));
JButton btOK = new JButton("OK");
ActionListener lst = new ActionListener() {
public void actionPerformed(ActionEvent e) {
option = JOptionPane.OK_OPTION;
setVisible(false);
}
};
btOK.addActionListener(lst);
p1.add(btOK);
JButton btCancel = new JButton("Cancel");
lst = new ActionListener() {
public void actionPerformed(ActionEvent e) {
option = JOptionPane.CANCEL_OPTION;
setVisible(false);
}
};
btCancel.addActionListener(lst);
p1.add(btCancel);
p.add(p1);
getContentPane().add(p);
pack();
setResizable(false);
ListSelectionListener lsel = new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
updatePreview();
}
};
fontNames.addListSelectionListener(lsel);
fontSizes.addListSelectionListener(lsel);
lst = new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == outlineCheck) {
outlineColor.setEnabled(outlineCheck.isSelected());
}
updatePreview();
}
};
boldCheck.addActionListener(lst);
italicCheck.addActionListener(lst);
outlineCheck.addActionListener(lst);
ZToolkit.moveCenter(this);
}
/**
*
* @param font
*/
public void setFont(ZFont font) {
fontNames.setSelected(font.name);
fontSizes.setSelectedInt(font.size);
boldCheck.setSelected(font.isBold());
italicCheck.setSelected(font.isItalic());
Object color = font.getOutlineColor();
if (color == null) {
color = Color.red;
}
outlineColor.setSelectedItem(color);
outlineCheck.setSelected(font.isOutline());
updatePreview();
}
/**
*
* @return
*/
public ZFont getSelectedFont() {
ZFont font = new ZFont(fontNames.getSelected(), 0, Integer.parseInt(fontSizes.getSelected()));
font.setBold(boldCheck.isSelected());
font.setItalic(italicCheck.isSelected());
font.setOutline(outlineCheck.isSelected());
font.setOutlineColor(outlineColor.getColor());
return font;
}
/**
*
* @param font
*
* @return
*/
public int show(ZFont font) {
setFont(font);
setVisible(true);
return option;
}
public static ZFontChooser getSharedInstace(JFrame owner) {
if (sharedInstance == null) {
sharedInstance = new ZFontChooser(owner);
}
return sharedInstance;
}
/**
*/
private void updatePreview() {
preview.repaint();
}
/**
* DOCUMENT ME!
*
* @version 1.00
* @author W.John
*/
class _Preview extends JPanel {
/**
*
* @param g
*/
public void paintComponent(Graphics g) {
super.paintComponent(g);
ZFont font = getSelectedFont();
font.paint((Graphics2D) g, new ZRect(0, 0, this.getWidth(), this.getHeight()), " ZCell Chooser ", ZBase.XCENTER,
ZBase.YCENTER,false);
}
}
}
/**
* DOCUMENT ME!
*
* @version 1.00
* @author W.John
*/
class OpenList extends JPanel implements ListSelectionListener, ActionListener {
protected JLabel title;
protected JTextField text;
protected JList list;
protected JScrollPane scroll;
public OpenList(String[] data, String t) {
setLayout(null);
title = new JLabel(t, JLabel.LEFT);
add(title);
text = new JTextField();
text.addActionListener(this);
add(text);
list = new JList(data);
list.setVisibleRowCount(4);
list.addListSelectionListener(this);
scroll = new JScrollPane(list);
add(scroll);
}
/**
*
* @return
*/
public Dimension getMaximumSize() {
Insets ins = getInsets();
Dimension d1 = title.getMaximumSize();
Dimension d2 = text.getMaximumSize();
Dimension d3 = scroll.getMaximumSize();
int w = Math.max(Math.max(d1.width, d2.width), d3.width);
int h = d1.height + d2.height + d3.height;
return new Dimension(w + ins.left + ins.right, h + ins.top + ins.bottom);
}
/**
*
* @return
*/
public Dimension getMinimumSize() {
Insets ins = getInsets();
Dimension d1 = title.getMinimumSize();
Dimension d2 = text.getMinimumSize();
Dimension d3 = scroll.getMinimumSize();
int w = Math.max(Math.max(d1.width, d2.width), d3.width);
int h = d1.height + d2.height + d3.height;
return new Dimension(w + ins.left + ins.right, h + ins.top + ins.bottom);
}
/**
*
* @return
*/
public Dimension getPreferredSize() {
Insets ins = getInsets();
Dimension d1 = title.getPreferredSize();
Dimension d2 = text.getPreferredSize();
Dimension d3 = scroll.getPreferredSize();
int w = Math.max(Math.max(d1.width, d2.width), d3.width);
int h = d1.height + d2.height + d3.height;
return new Dimension(w + ins.left + ins.right, h + ins.top + ins.bottom);
}
/**
*
* @param sel
*/
public void setSelected(String sel) {
list.setSelectedValue(sel, true);
text.setText(sel);
}
/**
*
* @return
*/
public String getSelected() {
return text.getText();
}
/**
*
* @param value
*/
public void setSelectedInt(int value) {
setSelected(Integer.toString(value));
}
/**
*
* @return
*/
public int getSelectedInt() {
try {
return Integer.parseInt(getSelected());
} catch (NumberFormatException ex) {
return -1;
}
}
/**
*
* @param e
*/
public void actionPerformed(ActionEvent e) {
ListModel model = list.getModel();
String key = text.getText().toLowerCase();
for (int k = 0; k < model.getSize(); k++) {
String data = (String) model.getElementAt(k);
if (data.toLowerCase().startsWith(key)) {
list.setSelectedValue(data, true);
break;
}
}
}
/**
*
* @param lst
*/
public void addListSelectionListener(ListSelectionListener lst) {
list.addListSelectionListener(lst);
}
/**
*/
public void doLayout() {
Insets ins = getInsets();
Dimension d = getSize();
int x = ins.left;
int y = ins.top;
int w = d.width - ins.left - ins.right;
int h = d.height - ins.top - ins.bottom;
Dimension d1 = title.getPreferredSize();
title.setBounds(x, y, w, d1.height);
y += d1.height;
Dimension d2 = text.getPreferredSize();
text.setBounds(x, y, w, d2.height);
y += d2.height;
scroll.setBounds(x, y, w, h - y);
}
/**
*
* @param e
*/
public void valueChanged(ListSelectionEvent e) {
Object obj = list.getSelectedValue();
if (obj != null) {
text.setText(obj.toString());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -