⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 textboundarytest.java

📁 sun公司开发的,java2核心技术,卷II:高级性能,包括一系列的高级java应用技术,如数据库德连接,高级swing,多线程,软件本地化等等,本文件中则包含该书中的所用实例,配合该书使用,使对ja
💻 JAVA
字号:
/**
 * @version 1.10 1999-10-14
 * @author Cay Horstmann
 */

import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.util.*;
import javax.swing.*;

public class TextBoundaryTest
{  public static void main(String[] args)
   {  JFrame frame = new TextBoundaryFrame();
      frame.show();
   }
}

class TextBoundaryFrame extends JFrame
{  public TextBoundaryFrame()
   {  setSize(400, 400);
      setTitle("TextBoundaryTest");

      addWindowListener(new WindowAdapter()
         {  public void windowClosing(WindowEvent e)
            {  System.exit(0);
            }
         } );

      ActionListener listener =
         new ActionListener()
         {  public void actionPerformed(ActionEvent event)
            {  updateDisplay();
            }
         };

      JPanel p = new JPanel();
      addCheckBox(p, characterCheckBox, cbGroup, listener, false);
      addCheckBox(p, wordCheckBox, cbGroup, listener, false);
      addCheckBox(p, lineCheckBox, cbGroup, listener, false);
      addCheckBox(p, sentenceCheckBox, cbGroup, listener, true);

      getContentPane().setLayout(new GridBagLayout());
      GridBagConstraints gbc = new GridBagConstraints();
      gbc.fill = GridBagConstraints.NONE;
      gbc.anchor = GridBagConstraints.EAST;
      add(new JLabel("Locale"), gbc, 0, 0, 1, 1);
      gbc.anchor = GridBagConstraints.WEST;
      add(localeCombo, gbc, 1, 0, 1, 1);
      add(p, gbc, 0, 1, 2, 1);
      gbc.fill = GridBagConstraints.BOTH;
      gbc.weighty = 100;
      add(new JScrollPane(inputText), gbc, 0, 2, 2, 1);
      add(new JScrollPane(outputText), gbc, 0, 3, 2, 1);


      locales = BreakIterator.getAvailableLocales();
      for (int i = 0; i < locales.length; i++)
         localeCombo.addItem(locales[i].getDisplayName());
      localeCombo.setSelectedItem(
         Locale.getDefault().getDisplayName());

      localeCombo.addActionListener(listener);

      inputText.setText("The quick, brown fox jump-ed\n"
        + "over the lazy \"dog.\" And then...what happened?");
      updateDisplay();
   }

   public void addCheckBox(Container p, JCheckBox checkBox,
      ButtonGroup g, ActionListener listener, boolean v)
   {  checkBox.setSelected(v);
      checkBox.addActionListener(listener);
      g.add(checkBox);
      p.add(checkBox);
   }

   public void add(Component c, GridBagConstraints gbc,
      int x, int y, int w, int h)
   {  gbc.gridx = x;
      gbc.gridy = y;
      gbc.gridwidth = w;
      gbc.gridheight = h;
      getContentPane().add(c, gbc);
   }

   public void updateDisplay()
   {  Locale currentLocale = locales[
         localeCombo.getSelectedIndex()];
      BreakIterator currentBreakIterator = null;
      if (characterCheckBox.isSelected())
         currentBreakIterator
            = BreakIterator.getCharacterInstance(currentLocale);
      else if (wordCheckBox.isSelected())
         currentBreakIterator
            = BreakIterator.getWordInstance(currentLocale);
      else if (lineCheckBox.isSelected())
         currentBreakIterator
            = BreakIterator.getLineInstance(currentLocale);
      else if (sentenceCheckBox.isSelected())
         currentBreakIterator
            = BreakIterator.getSentenceInstance(currentLocale);

      String text = inputText.getText();
      currentBreakIterator.setText(text);
      outputText.setText("");

      int from = currentBreakIterator.first();
      int to;
      while ((to = currentBreakIterator.next()) !=
         BreakIterator.DONE)
      {  outputText.append(text.substring(from, to) + "|");
         from = to;
      }
      outputText.append(text.substring(from));
   }

   private Locale[] locales;
   private BreakIterator currentBreakIterator;

   private JComboBox localeCombo = new JComboBox();
   private JTextArea inputText = new JTextArea(6, 40);
   private JTextArea outputText = new JTextArea(6, 40);
   private ButtonGroup cbGroup = new ButtonGroup();
   private JCheckBox characterCheckBox = new JCheckBox("Character");
   private JCheckBox wordCheckBox = new JCheckBox("Word");
   private JCheckBox lineCheckBox = new JCheckBox("Line");
   private JCheckBox sentenceCheckBox = new JCheckBox("Sentence");
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -