📄 textfielddemo.java
字号:
/* TextFieldDemo.java -- An example showing various textfields in Swing. Copyright (C) 2005, 2006, Free Software Foundation, Inc.This file is part of GNU Classpath examples.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING. If not, write to theFree Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 USA.*/package gnu.classpath.examples.swing;import java.awt.BorderLayout;import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.GridLayout;import java.awt.Rectangle;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.BorderFactory;import javax.swing.Box;import javax.swing.BoxLayout;import javax.swing.JButton;import javax.swing.JCheckBox;import javax.swing.JComponent;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JTextField;import javax.swing.SwingUtilities;import javax.swing.text.BadLocationException;import javax.swing.text.DefaultCaret;import javax.swing.text.JTextComponent;/** * A simple textfield demo showing various textfields in different states. */public class TextFieldDemo extends JPanel implements ActionListener{ /** * A custom caret for demonstration purposes. This class is inspired by the * CornerCaret from the OReilly Swing book. * * @author Roman Kennke (kennke@aicas.com) */ static class CornerCaret extends DefaultCaret { public CornerCaret() { super(); setBlinkRate(500); } protected synchronized void damage(Rectangle r) { if (r == null) return; x = r.x; y = r.y + (r.height * 4 / 5 - 3); width = 5; height = 5; repaint(); } public void paint(Graphics g) { JTextComponent comp = getComponent(); if (comp == null) return; int dot = getDot(); Rectangle r = null; try { r = comp.modelToView(dot); } catch (BadLocationException e) { return; } if (r == null) return; int dist = r.height * 4 / 5 - 3; if ((x != r.x) || (y != r.y + dist)) { repaint(); x = r.x; y = r.y + dist; width = 5; height = 5; } if (isVisible()) { g.drawLine(r.x, r.y + dist, r.x, r.y + dist + 4); g.drawLine(r.x, r.y + dist + 4, r.x + 4, r.y + dist + 4); } } } /** * The left aligned textfields and state buttons. */ JTextField textfield1; JTextField textfield2; JTextField textfield3; JCheckBox enabled1; JCheckBox editable1; JPanel textFieldPanel1; /** * The right aligned textfields and state buttons. */ JTextField textfield4; JTextField textfield5; JTextField textfield6; JCheckBox enabled2; JCheckBox editable2; /** * The centered textfields and state buttons. */ JTextField textfield7; JTextField textfield8; JTextField textfield9; JCheckBox enabled3; JCheckBox editable3; /** * The custom colored textfields and state buttons. */ JTextField textfield10; JTextField textfield11; JTextField textfield12; JTextField textfield13; JTextField textfield14; JCheckBox enabled4; JCheckBox editable4; /** * Some miscallenous textfield demos. */ JTextField textfield15; JTextField textfield16; JCheckBox enabled5; JCheckBox editable5; /** * Creates a new demo instance. */ public TextFieldDemo() { super(); createContent(); } /** * When the demo is run independently, the frame is displayed, so we should * initialise the content panel (including the demo content and a close * button). But when the demo is run as part of the Swing activity board, * only the demo content panel is used, the frame itself is never displayed, * so we can avoid this step. */ void initFrameContent() { JPanel closePanel = new JPanel(); JButton closeButton = new JButton("Close"); closeButton.setActionCommand("CLOSE"); closeButton.addActionListener(this); closePanel.add(closeButton); add(closePanel, BorderLayout.SOUTH); } /** * Returns a panel with the demo content. The panel * uses a BorderLayout(), and the BorderLayout.SOUTH area * is empty, to allow callers to add controls to the * bottom of the panel if they want to (a close button is * added if this demo is being run as a standalone demo). */ private void createContent() { setLayout(new BorderLayout()); JPanel panel = new JPanel(new GridLayout(5, 1)); panel.add(createLeftAlignedPanel()); panel.add(createRightAlignedPanel()); panel.add(createCenteredPanel()); panel.add(createCustomColoredPanel()); panel.add(createMiscPanel()); add(panel); } private JPanel createLeftAlignedPanel() { JPanel panel = new JPanel(new BorderLayout()); panel.setBorder(BorderFactory.createTitledBorder("Left aligned")); textFieldPanel1 = new JPanel(); textFieldPanel1.setLayout(new BoxLayout(textFieldPanel1, BoxLayout.X_AXIS)); textfield1 = new JTextField("Hello World!"); textfield1.setHorizontalAlignment(JTextField.LEFT); textfield1.setFont(new Font("Dialog", Font.PLAIN, 8)); textFieldPanel1.add(textfield1); textfield2 = new JTextField("Hello World!"); textfield2.setHorizontalAlignment(JTextField.LEFT); textfield2.setFont(new Font("Dialog", Font.ITALIC, 12)); textFieldPanel1.add(textfield2); textfield3 = new JTextField("Hello World!"); textfield3.setHorizontalAlignment(JTextField.LEFT); textfield3.setFont(new Font("Dialog", Font.BOLD, 14)); textFieldPanel1.add(textfield3); panel.add(textFieldPanel1); JPanel statePanel = new JPanel(); statePanel.setLayout(new BoxLayout(statePanel, BoxLayout.Y_AXIS)); statePanel.add(Box.createVerticalGlue()); enabled1 = new JCheckBox("enabled"); enabled1.setSelected(true); enabled1.addActionListener(this); enabled1.setActionCommand("ENABLED1"); statePanel.add(enabled1); editable1 = new JCheckBox("editable"); editable1.setSelected(true); editable1.addActionListener(this); editable1.setActionCommand("EDITABLE1"); statePanel.add(editable1); statePanel.add(Box.createVerticalGlue()); panel.add(statePanel, BorderLayout.EAST); return panel; } private JPanel createRightAlignedPanel() { JPanel panel = new JPanel(new BorderLayout()); panel.setBorder(BorderFactory.createTitledBorder("Right aligned")); JPanel textFieldPanel = new JPanel(); textFieldPanel.setLayout(new BoxLayout(textFieldPanel, BoxLayout.X_AXIS)); textfield4 = new JTextField("Hello World!"); textfield4.setHorizontalAlignment(JTextField.RIGHT); textfield4.setFont(new Font("Dialog", Font.PLAIN, 8)); textFieldPanel.add(textfield4); textfield5 = new JTextField("Hello World!"); textfield5.setHorizontalAlignment(JTextField.RIGHT); textfield5.setFont(new Font("Dialog", Font.ITALIC, 12));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -