Creating Notepad in Java

I am creating a notepad application in Java. I created a text area and menu. I have a menu called "EDIT" and under that I have "UPPERCASE". If I select a specific text and press "UPPERCASE", I want the string to be converted to uppercase. Can someone tell me how to implement this in Java.

+3
source share
7 answers

This can be done using the following methods:

  • JTextArea.getSelectionStart()
  • JTextArea.getSelectionEnd()
  • JTextArea.getText()
  • String.toUpperCase()
  • JTextArea.replaceRange()
+7
source

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

Swing . , , , , , 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 = new UpperCaseAction();
JMenuItem menuItem = new JMenuItem( action );
JButton button = new JButton( action ); 
0

, , .

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

. :

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

Source: https://habr.com/ru/post/1750470/


All Articles