📄 fontfield.java
字号:
package com.csa.lib.swing;import java.awt.Dimension;import java.awt.Event;import java.awt.FlowLayout;import java.awt.Font;import java.awt.Image;import java.awt.MediaTracker;import java.awt.Toolkit;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.net.URL;import java.text.MessageFormat;import java.util.Vector;import javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;public class FontField extends JPanel { private static final long serialVersionUID = 1L; public static final String IMG_NAME="fontfield.gif"; Font currentFont; JLabel label; JButton openButton; /** * Lista de componentes que reciben notificaciones desde este objeto */ Vector actionListeners = new Vector(); public String getFontDescription() { if (currentFont == null) return ""; return MessageFormat .format("{0}, {1}pt ", new Object[] { currentFont.getFontName(), new Integer(currentFont.getSize()) }); } /** * Crear un FontField * * @param sfmt * formato para la fecha en el texto. */ public FontField(Font f) { setLayout(new FlowLayout(0, 0, 0)); label = new JLabel(getFontDescription()); label.setMinimumSize(new Dimension(70, 14)); setFont(f); URL res = FontField.class.getResource(IMG_NAME); Image img = Toolkit.getDefaultToolkit().getImage(res); MediaTracker m = new MediaTracker(this); m.addImage(img, 0); try { m.waitForAll(); } catch (Exception e) { System.err.println("Error al cargar imagen "); e.printStackTrace(); } ImageIcon icon = new ImageIcon(img); openButton = new JButton(icon); // new JButton("..."); openButton.setPreferredSize(new Dimension(18, 20)); add(label); add(openButton); openButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (e.getSource() == openButton) showHelpWindow(); } }); label.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { if (e.getClickCount() >= 2) { showHelpWindow(); } } }); } public Font getFont() { return currentFont; } public void setFont(Font f) { currentFont = f; if (label==null) return; String text = getFontDescription(); label.setFont(f); label.setText(text); //Graphics g = label.getGraphics(); //int w = g.getFontMetrics().stringWidth(text); //int h = g.getFontMetrics().getHeight(); //g.dispose(); //label.setPreferredSize(new Dimension(w, h)); } public JButton getOpenButton() { return openButton; } // Soporte a eventos public void addActionListener(ActionListener l) { actionListeners.add(l); } public void removeActionListener(ActionListener l) { actionListeners.remove(l); } protected void triggerAction() { for (int i = 0; i < actionListeners.size(); i++) { ActionListener l = (ActionListener) actionListeners.get(i); ActionEvent a = new ActionEvent(this, Event.ACTION_EVENT, ""); try { l.actionPerformed(a); } catch (Exception e) { e.printStackTrace(); } } } protected void assignFont(Font f) { setFont(f); triggerAction(); } // Soporte a la ventana /** * Muestra la ventana de ayuda de seleccion de fecha. */ void showHelpWindow() { FontChooserDialog fc = new FontChooserDialog(null); fc.setFont(currentFont); if (fc.execute()) { assignFont(fc.getFont()); } } /** * Ejemplo * * @param args */ public static void main(String[] args) { JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().setLayout(new FlowLayout()); f.getContentPane().add(new JLabel("Fecha")); f.getContentPane().add(new FontField(null)); f.pack(); f.setVisible(true); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -