2014-06-23 99 views
3

作为初学者的Java程序员,我想了解一些帮助以了解为我的控制台应用程序构建摇摆UI的最佳方式或技巧。将控制台应用程序转换为摇摆应用程序

控制台应用程序运行良好,现在我想将其转换为摇摆应用程序。我想在JScrollPane中有一个JTextArea的swing,用户可以通过编辑来输入它们的字符串,然后点击一个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); 
    } 
    } 

我企图在秋千:

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

一般认为你不能'转换'一个应用程序。从基于命令行到基于GUI。它需要一个完全不同的方法,只是“太宽泛”,不能在SO答案中解释。请参阅Java教程的[使用JFC/Swing创建GUI](http://docs.oracle.com/javase/tutorial/uiswing/)了解如何创建GUI。 –

+1

*“我在Swing的尝试”*所以......它有什么问题?你卡在哪里? –

+1

'this.setPreferredSize(new Dimension(400,200));'请参阅[我应该避免使用Java Swing中的set(Preferred | Maximum | Minimum)Size方法?](http://stackoverflow.com/q/ 7229226/418556)(是) –

回答

2

你可以利用此代码为基于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); 
} 
} 
+1

1)'cw.setDefaultCloseOperation(3);'不要使用魔术数字,使用定义的常量。 2)GUI应该在EDT上启动。 3)对于文本字段和文本区域的大小建议(列和行),应该不需要'cw.setSize(400,300);'。 4)除非改变功能,否则不要扩展'JFrame'。喜欢构成继承。 5)'container.add(scrollPane,BorderLayout.NORTH);'最好使用更多本地化的容器'container.add(scrollPane,BorderLayout.PAGE_START);'.. –

+0

谢谢我发现....字符串[] = text.trim()。split(“\\ w \\ s +”);是一种更好的方式来分隔单词的空格,因为它剥离了多个空格,如制表符,并提供更准确的字数!总是更好地去除正则表达式。 – Reign

相关问题