How to create a text field in which the user can enter a large amount of text in

I am currently engaged in Java learning as a computer science. As part of this assignment, I am trying to create an additional frame that the user can write UML code to which my main application will then be transferred, and then to the class diagram.

The bit that I'm stuck with is that the JTextBox that I injected into this secondary frame is the size I want, but the recording starts in the middle and does not change to a new line when it becomes a different frame size.

This is an image of what is currently happening:

Image of output]! [The output of my current code

the code

And this is the code that I have for this class, if needed.

package classdesign; import java.awt.*; import javax.swing.*; public class ClassCreation extends JFrame { private JFrame frame; private JLabel instructionlabel; private JTextField inputUML; private JButton upButton; private String Message; public void ClassCreation(){ frame = new JFrame(); frame.setSize(300, 400); frame.setLocationRelativeTo(null); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setTitle("Class Design"); JPanel CreationPanel = new JPanel(); CreationPanel.setLayout(new BorderLayout()); instructionlabel = new JLabel("Fill Class details in using UML"); CreationPanel.add(instructionlabel,BorderLayout.NORTH); inputUML = new JTextField("",20); CreationPanel.add(inputUML,BorderLayout.CENTER); frame.add(CreationPanel); } public Frame getFrame() { return frame; } } 

So, to summarize what I was hoping, someone can tell me how to do this is to get the text from the user, to start in the upper left corner and go to the next line when he gets to the far right, text editor etc.

+4
source share
3 answers

use JTextPane or JEditorPane. A sample can be found at http://docs.oracle.com/javase/tutorial/uiswing/components/editorpane.html

+5
source

JTextField is a lightweight component that allows you to edit a single line of text. ( source )

Since this is a single-line component, regardless of its size, the cursor will always be centered and will never skip to the next line.

I suggest you use JTextArea , as it is a multi-line area and allows the user to enter input the way you want. to.

+6
source

An example of using a text area (with a few other tips thrown for free) to check comments).

ClassCreation

 import java.awt.*; import javax.swing.*; // Has an instance of frame, does not need to extend it. public class ClassCreation { //extends JFrame { private JFrame frame; private JLabel instructionlabel; // as mentioned by talnicolas private JTextArea inputUML; // Don't give a method the same name as a class!! //public void ClassCreation(){ public void initGui(){ frame = new JFrame(); //frame.setSize(300, 400); //pack() instead! //frame.setLocationRelativeTo(null); // do something better frame.setLocationByPlatform(true); // better! //frame.setVisible(true); // do later frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setTitle("Class Design"); JPanel CreationPanel = new JPanel(); CreationPanel.setLayout(new BorderLayout()); instructionlabel = new JLabel("Fill Class details in using UML"); CreationPanel.add(instructionlabel,BorderLayout.NORTH); inputUML = new JTextArea("",7,30); // very important next 2 lines inputUML.setLineWrap(true); inputUML.setWrapStyleWord(true); // add it to a scrollpane CreationPanel.add(new JScrollPane(inputUML),BorderLayout.CENTER); frame.add(CreationPanel); frame.pack(); // assume the natural size! frame.setVisible(true); for (int ii=0; ii<150; ii++) { inputUML.append(SENTENCE); inputUML.setCaretPosition( inputUML.getText().length() ); } } public static void main(String[] args) { // Swing GUIs should be created and altered on the EDT. SwingUtilities.invokeLater(new Runnable() { public void run() { ClassCreation cc = new ClassCreation(); cc.initGui(); } }); } private static String SENTENCE = "The quick brown fox jumps over the lazy dog! "; } 
+4
source

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


All Articles