作为Java初学者,我希望获得一些帮助,以了解为控制台应用程序构建摆动UI的最佳方法或技术。
现在,控制台应用程序运行良好,我想将其转换为swing应用程序。我想在JScrollPane中有一个JTextArea,用户可以对其进行编辑以输入其字符串,然后单击一个countwords按钮并以int格式获取输出。
下面是我的控制台应用程序以及我尝试使用swing应用程序的代码。我花了很多时间尝试对此进行研究,但是我似乎无法正确地做到这一点。
我的控制台应用程序:
import java.util.*;
public class WordCounter{
public static void main(String args[])
{
// Create Scanner object
Scanner s=new Scanner(System.in);
// Read a string
String st=s.nextLine();
//Split string with space
String words[]=st.trim().split(" ");
System.out.println("No. of words "+words.length);
}
}
Run Code Online (Sandbox Code Playgroud)
我对Swing的尝试:
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JTextArea;
import java.util.*;
class Countswords extends JPanel {
public Countswords() {
myTextArea();
}
private void myTextArea() {
this.setLayout(new BorderLayout());
this.setPreferredSize(new Dimension(400, 200));
JTextArea textArea = new JTextArea(5, 40);
JScrollPane …Run Code Online (Sandbox Code Playgroud)