📄 textareademo.java
字号:
/* TextAreaDemo.java -- An example showing various textareas 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 modify it under the terms of the GNU General Public License as published by the 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, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Classpath; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-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.Point;import java.awt.Rectangle;import java.awt.Shape;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.JScrollPane;import javax.swing.JTabbedPane;import javax.swing.JTextArea;import javax.swing.SwingUtilities;import javax.swing.text.BadLocationException;import javax.swing.text.DefaultCaret;import javax.swing.text.Highlighter;import javax.swing.text.JTextComponent;import javax.swing.text.View;import javax.swing.text.LayeredHighlighter.LayerPainter;/** * A simple textare demo showing various textareas in different states. */public class TextAreaDemo 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); } } } static class DemoHighlightPainter extends LayerPainter { static DemoHighlightPainter INSTANCE = new DemoHighlightPainter(); static Color[] colors = { Color.BLUE, Color.CYAN, Color.GRAY, Color.GREEN, Color.MAGENTA, Color.ORANGE, Color.PINK, Color.ORANGE, Color.RED, Color.BLUE, Color.YELLOW }; public DemoHighlightPainter() { super(); } private void paintHighlight(Graphics g, Rectangle rect) { g.fillRect(rect.x, rect.y, rect.width, rect.height); } public void paint(Graphics g, int p0, int p1, Shape bounds, JTextComponent t) { try { for (int i = p0; i < p1; i++) { Rectangle r = t.modelToView(i); Point l1 = t.modelToView(i + 1).getLocation(); g.setColor(colors[(int) (Math.random() * colors.length)]); g.fillOval(r.x, r.y, l1.x - r.x, r.height); } } catch (BadLocationException ble) { } } public Shape paintLayer(Graphics g, int p0, int p1, Shape bounds, JTextComponent c, View view) { paint(g, p0, p1, bounds, c); return bounds; } } /** * The non wrapping text areas and state buttons. */ JTextArea textarea1; JTextArea textarea2; JTextArea textarea3; JCheckBox enabled1; JCheckBox editable1; JPanel panel1; /** * The char wrapping textareas and state buttons. */ JTextArea textarea4; JTextArea textarea5; JTextArea textarea6; JCheckBox enabled2; JCheckBox editable2; /** * The word wrapping textareas and state buttons. */ JTextArea textarea7; JTextArea textarea8; JTextArea textarea9; JCheckBox enabled3; JCheckBox editable3; /** * The custom colored textareas and state buttons. */ JTextArea textarea10; JTextArea textarea11; JTextArea textarea12; JTextArea textarea13; JTextArea textarea14; JTextArea textarea14b; JCheckBox enabled4; JCheckBox editable4; /** * Some miscellaneous textarea demos. */ JTextArea textarea15; JTextArea textarea16; JTextArea textarea17; JCheckBox enabled5; JCheckBox editable5; /** * Creates a new demo instance. */ public TextAreaDemo() { 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()); JTabbedPane tabPane = new JTabbedPane(); tabPane.addTab("Non-wrap", createNonWrapPanel()); tabPane.addTab("Char-wrap", createCharWrapPanel()); tabPane.addTab("Word-wrap", createWordWrapPanel()); tabPane.addTab("Custom colors", createCustomColoredPanel()); tabPane.addTab("Misc", createMiscPanel()); add(tabPane); } private JPanel createNonWrapPanel() { JPanel panel = new JPanel(new BorderLayout()); panel.setBorder(BorderFactory.createTitledBorder("Not wrapping")); panel1 = new JPanel(new GridLayout(2, 2)); textarea1 = new JTextArea("Hello World!"); textarea1.setFont(new Font("Dialog", Font.PLAIN, 8)); panel1.add(new JScrollPane(textarea1)); textarea2 = new JTextArea("Hello World!"); textarea2.setFont(new Font("Dialog", Font.ITALIC, 12)); panel1.add(new JScrollPane(textarea2)); textarea3 = new JTextArea("Hello World!"); textarea3.setFont(new Font("Dialog", Font.BOLD, 14)); panel1.add(new JScrollPane(textarea3)); panel.add(panel1); JPanel statePanel = new JPanel(); statePanel.setLayout(new BoxLayout(statePanel, BoxLayout.Y_AXIS)); statePanel.add(Box.createVerticalGlue());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -