📄 fontchooserdialog.java
字号:
/** * FontChooser.java * Copyright 2005 Carlos Silva A. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */package com.csa.lib.swing;import java.awt.BorderLayout;import java.awt.Color;import java.awt.FlowLayout;import java.awt.Font;import java.awt.Frame;import java.awt.GraphicsEnvironment;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.util.Vector;import javax.swing.BorderFactory;import javax.swing.BoxLayout;import javax.swing.JButton;import javax.swing.JDialog;import javax.swing.JLabel;import javax.swing.JList;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.ListModel;import javax.swing.event.ListSelectionEvent;import javax.swing.event.ListSelectionListener;/** * Ventana para escoger Fonts. Se puede usar facilmente. * * <pre> * FontChooser fc = new FontChooser(ownerFrame); * if (fc.execute()) * System.out.println(fc.getFont()); * </pre> * * @author csilva */public class FontChooserDialog implements ListSelectionListener, ActionListener { private JDialog dialog; private JList fontNames; private JList fontSizes; private JList fontStyles; private JLabel demoFont; private JButton botonOk; private JButton botonCancel; private Font font; private boolean ok = true; private boolean ignoreValueChanges = false; public FontChooserDialog(Frame owner) { buildUI(owner, "Seleccion de font"); } public FontChooserDialog(Frame owner, String title) { buildUI(owner, title); } public FontChooserDialog(Frame owner, String title, Font f) { buildUI(owner, title); setFont(f); } public void setFont(Font f) { font = f; System.out.println("setfont:"+font); System.out.println("setfont style:"+font.getStyle()); } /** * Construye un dialogo (interno) para dar soporte a la eleccion del font * * @param owner */ void buildUI(Frame owner, String title) { dialog = new JDialog(owner, title); GraphicsEnvironment ge = GraphicsEnvironment .getLocalGraphicsEnvironment(); String familyNames[] = ge.getAvailableFontFamilyNames(); Vector sizes = new Vector(); for (int sz = 8; sz < 24; sz++) sizes.add("" + sz); Vector styles = new Vector(); styles.add("plain"); styles.add("bold"); styles.add("italic"); styles.add("bold italic"); fontNames = new JList(familyNames); fontNames.setBorder(BorderFactory.createLineBorder(Color.gray)); fontNames.addListSelectionListener(this); fontSizes = new JList(sizes); fontSizes.addListSelectionListener(this); fontStyles = new JList(styles); fontStyles.addListSelectionListener(this); JPanel configPanel = new JPanel(null); configPanel.setLayout(new BoxLayout(configPanel, BoxLayout.X_AXIS)); configPanel.add(new JScrollPane(fontNames)); configPanel.add(new JScrollPane(fontStyles)); configPanel.add(new JScrollPane(fontSizes)); demoFont = new JLabel( "ABCDEFGHIJKLMN袿PQRSTUVWXYZ abcdefghijklmn駉pqrstuvwxyz"); demoFont.setBorder(BorderFactory.createTitledBorder(null, "Ejemplo")); if (font != null) demoFont.setFont(font); else font = demoFont.getFont(); botonOk = new JButton("Ok"); botonCancel = new JButton("Cancel"); botonOk.addActionListener(this); botonCancel.addActionListener(this); JPanel botonera = new JPanel(new FlowLayout()); botonera.add(botonOk); botonera.add(botonCancel); JPanel mainPanel = new JPanel(new BorderLayout()); mainPanel.add(configPanel, BorderLayout.CENTER); mainPanel.add(demoFont, BorderLayout.SOUTH); dialog.getContentPane().setLayout(new BorderLayout()); dialog.getContentPane().add(mainPanel, BorderLayout.CENTER); dialog.getContentPane().add(botonera, BorderLayout.SOUTH); } public void valueChanged(ListSelectionEvent e) { if (ignoreValueChanges) return; buildFont(); } public void buildFont() { String fontName = (String) fontNames.getSelectedValue(); if (fontName == null) return; String sizeStr = (String) fontSizes.getSelectedValue(); if (sizeStr == null) return; int fontSize = Integer.parseInt(sizeStr); int fontStyle = Font.PLAIN; switch (fontStyles.getSelectedIndex()) { case 1: fontStyle = Font.BOLD; break; case 2: fontStyle = Font.ITALIC; break; case 3: fontStyle = Font.BOLD | Font.ITALIC; break; default: ; } font = new Font(fontName, fontStyle, fontSize); demoFont.setFont(font); } public void actionPerformed(ActionEvent e) { if (e.getSource() == botonOk) { ok = true; } dialog.setVisible(false); } /** * Ejecuta el dialogo y retorna true si el usuario selecciono un Font. * * @return */ public boolean execute() { System.out.println("font:"+font); System.out.println("style:"+font.getStyle()); ok = false; ignoreValueChanges=true; // Asignar los valores iniciales de cada lista de acuerdo // al font seleccionado. ListModel model = fontNames.getModel(); String value = (font!=null ? font.getFamily():null); for (int i = 0; i < model.getSize(); i++) { if (model.getElementAt(i).equals(value)) { fontNames.setSelectedIndex(i); fontNames.ensureIndexIsVisible(i); break; } } model = fontSizes.getModel(); value = (font!=null ? String.valueOf(font.getSize()):null); for (int i = 0; i < model.getSize(); i++) { if (model.getElementAt(i).equals(value)) { fontSizes.setSelectedIndex(i); fontSizes.ensureIndexIsVisible(i); break; } } int style = (font!=null ? font.getStyle() : 0); System.out.println("font:"+font); System.out.println("style:"+style); boolean isBold = (style & Font.BOLD) != 0; boolean isItalic = (style & Font.ITALIC) != 0; fontStyles.setSelectedIndex((isBold ? 1 : 0) + (isItalic ? 2 : 0)); ignoreValueChanges=false; dialog.pack(); dialog.setModal(true); dialog.setVisible(true); dialog.dispose(); return ok; } public Font getFont() { return demoFont.getFont(); } public String getFamilyName() { return demoFont.getFont().getFamily(); } public int getFontStyle() { return demoFont.getFont().getStyle(); } public int getFontSize() { return demoFont.getFont().getSize(); } public static void main(String args[]) { FontChooserDialog fc = new FontChooserDialog(null); if (fc.execute()) System.out.println(fc.getFont()); System.exit(0); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -