开发者

Creating Notepad in Java

开发者_如何学GoI am creating a notepad application in Java.I have created the text area also the menus .I have a menu called "EDIT" and under that I have "UPPERCASE"..If i select a particular text and click on "UPPERCASE" ,I want the string to be converted into uppercase. Can anyone tell me how to implement this in Java.


This can be done by using the following methods:

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


String.toUpperCase()


The String class has toUpperCase() and toLowerCase() methods that you can use. Here's an example:

    System.out.println("Hello World!".toUpperCase());
    // prints "HELLO WORLD!"

    System.out.println("FOO $@&# BAR".toLowerCase());
    // prints "foo $@&# bar"

They also have overloads that takes a java.util.Locale if you need to do locale-specific transformation.

Related questions

  • How do I convert strings between uppercase and lowercase in Java?
  • Convert typed-in Text to lowercase

Reminder: String is immutable

String is immutable: you can't invoke a method that will mutate the string instance it's invoked upon. The following is a common beginner's mistake:

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

Instead of mutating the instance they're invoked on, these methods return a new instance of String.

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


Well, JTextArea has a getText and a setText method and String has a toUpperCase method. Check out this example: http://www.exampledepot.com/egs/javax.swing.text/ta_EditTextArea.html

EDIT:

This might work:

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:

Here is a working example:

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();
}
}


Swing uses Actions for this type of processing. Functions like cut, copy, paste, select word are all done by Actions. Create your own custom action:

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);
    }
}

The benefit of the Action is that it will work on any text component that has focus. You use the Action like:

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


It's much easier if you just send the text to a variable, change it to uppercase and then send it back.

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


I have faster trick. In this menu item listener u can write:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜