作为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 scrollPane = new JScrollPane(textArea);
// textArea.setEditable(true);
JTextArea.setText(userInput);
}
public static void showFrame() {
JPanel panel = new Countswords();
panel.setOpaque(true);
JFrame frame = new JFrame("My Text Area");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(panel);
frame.pack();
frame.setVisible(true);
}
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);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Countswords.showFrame();
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
小智 3
您可以利用此代码使您的应用程序基于 Swing。
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class CountWords extends JFrame {
JTextArea textArea;
JScrollPane scrollPane;
Container container;
JPanel panel;
JLabel label;
JButton button;
public CountWords() {
initialize();
addToPanel();
}
public void initialize() {
container = getContentPane();
textArea = new JTextArea(5,20);
scrollPane = new JScrollPane(textArea);
button = new JButton(" Count Words ");
panel = new JPanel();
label = new JLabel(" Total Words : ");
label.setBackground(Color.yellow);
}
public void addToPanel() {
panel.add(button);
container.add(scrollPane, BorderLayout.NORTH);
container.add(panel, BorderLayout.SOUTH);
container.add(label, BorderLayout.CENTER);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae) {
label.setText("No.of Words : "+ getTotalWords(textArea.getText()));
}
});
}
public int getTotalWords( String text ){
String words[]= text.trim().split(" ");
return words.length;
}
public static void main(String args[]) {
CountWords cw = new CountWords();
cw.setDefaultCloseOperation(3); // JFrame.EXIT_ON_CLOSE => 3
cw.pack();
cw.setSize(400,300);
cw.setVisible(true);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1393 次 |
| 最近记录: |