【问题标题】:Creating Notepad in Java在 Java 中创建记事本
【发布时间】:2010-06-17 10:46:13
【问题描述】:

我正在用 Java 创建一个记事本应用程序。我还创建了文本区域和菜单。我有一个名为“编辑”的菜单,在该菜单下我有“大写”。如果我选​​择特定文本并单击“ UPPERCASE" ,我希望将字符串转换为大写。 谁能告诉我如何在 Java 中实现它。

【问题讨论】:

  • Java 的字符串类有一个 toUpperCase() 方法...或者您是在问如何找到选定的文本?这可能是您控制的财产...
  • 如果你用awt,改成swing!

标签: java


【解决方案1】:

这可以通过以下方法来完成:

  • JTextArea.getSelectionStart()
  • JTextArea.getSelectionEnd()
  • JTextArea.getText()
  • String.toUpperCase()
  • JTextArea.replaceRange()

【讨论】:

  • 它不起作用。我的大写菜单已启用。我还有 else if(e.getsource()==this.upper) { JTextArea textArea = new JTextArea("some text"); int start = textArea.getSelectionStart(); int end = textArea.getSelectionEnd();字符串替换 = textArea.getSelectedText().toUpperCase(); textArea.replaceRange(替换,开始,结束); } 但它仍然不工作..你能告诉我要做什么吗?
  • @Saranya.R:当然,您必须使用向用户显示的 JTextArea,而不是您在本地创建的。
【解决方案2】:

【讨论】:

    【解决方案3】:

    String 类具有您可以使用的 toUpperCase()toLowerCase() 方法。这是一个例子:

        System.out.println("Hello World!".toUpperCase());
        // prints "HELLO WORLD!"
    
        System.out.println("FOO $@&# BAR".toLowerCase());
        // prints "foo $@&# bar"
    

    如果您需要进行特定于语言环境的转换,它们还具有采用 java.util.Locale 的重载。

    相关问题


    提醒:String 是不可变的

    String 是不可变的:您不能调用会改变调用它的字符串实例的方法。以下是初学者常见的错误:

        String text = "  blah blah bloop  ";
        text.toUpperCase();
        text.trim();
        System.out.println(text);
        // prints "  blah blah bloop  "
    

    这些方法不会改变调用它们的实例,而是返回String 实例。

        String text = "  blah blah bloop  ";
        text = text.toUpperCase().trim();
        System.out.println(text);
        // prints "BLAH BLAH BLOOP"
    

    【讨论】:

      【解决方案4】:

      嗯,JTextArea 有一个 getText 和一个 setText 方法,而 String 有一个 toUpperCase 方法。看看这个例子:http://www.exampledepot.com/egs/javax.swing.text/ta_EditTextArea.html

      编辑

      这可能有效:

      JTextArea textArea = new JTextArea("some text");
      int start = textArea.getSelectionStart();
      int end = textArea.getSelectionEnd();
      String replace = textArea.getSelectedText().toUpperCase();
      textArea.replaceRange(replace, start, end); 
      

      EIDT 2:

      这是一个工作示例:

      import java.awt.BorderLayout;
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;
      
      import javax.swing.JButton;
      import javax.swing.JFrame;
      import javax.swing.JTextArea;
      
      public class Test {
      
      JTextArea textArea = new JTextArea("some text");
      
      public Test() {
      
          JButton button = new JButton("upper");
          button.addActionListener(new ActionListener(){
              public void actionPerformed(ActionEvent e) {
                  int start = textArea.getSelectionStart();
                  int end = textArea.getSelectionEnd();
                  String replace = textArea.getSelectedText().toUpperCase();
                  textArea.replaceRange(replace, start, end);
              }
          });
      
          JFrame frame = new JFrame();
          frame.add(textArea);
          frame.add(button, BorderLayout.SOUTH);
          frame.setSize(100, 100);
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
      }
      
      public static void main(String[] args) {
          new Test();
      }
      }
      

      【讨论】:

      • 它没有显示任何错误但无法正常工作你能告诉我 getSelectionStart() 是预定义的函数吗?
      【解决方案5】:

      Swing 使用 Actions 进行此类处理。剪切、复制、粘贴、选词等功能均由 Actions 完成。创建您自己的自定义操作:

      class UpperCaseAction extends TextAction
      {
          public UpperCaseAction()
          {
              super("UpperCase");
          }
      
          public void actionPerformed(ActionEvent e)
          {
              JTextComponent component = getFocusedComponent();
              int start = cmoponent.getSelectionStart();
              int end = component.getSelectionEnd();
              String replace = component.getSelectedText().toUpperCase();
              component.replaceRange(replace, start, end);
          }
      }
      

      Action 的好处是它适用于任何具有焦点的文本组件。您使用以下操作:

      Action action = new UpperCaseAction();
      JMenuItem menuItem = new JMenuItem( action );
      JButton button = new JButton( action ); 
      

      【讨论】:

        【解决方案6】:

        如果您只是将文本发送到变量,将其更改为大写,然后将其发送回来,会容易得多。

        string upper = JTextArea.Text;
        upper.toUpperCase();
        JTextArea.Text = upper;
        

        【讨论】:

          【解决方案7】:

          我有更快的技巧。 在这个菜单项监听器中你可以写:

          String selectedText=jTextArea1.getSelectedText();
                 if (selectedText==null)return;
                 selectedText=selectedText.toUpperCase();
                 jTextArea1.replaceSelection(selectedText);
          

          【讨论】:

            猜你喜欢
            • 2012-05-20
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-12-23
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多