当前位置: 代码迷 >> Java相关 >> 帮我看一下好吗?
  详细解决方案

帮我看一下好吗?

热度:102   发布时间:2005-09-25 22:32:00.0
帮我看一下好吗?

import java.util.*; import java.awt.*; import java.awt.event.*; import javax.swing.*;

public class String6 extends JFrame{ private JLabel promptLabel; private JTextField inputField; private JTextArea ouputArea; public String6() { super("Testring Class StirngTokenizer"); Container container = getContentPane(); container.setLayout(new FlowLayout()); promptLabel = new JLabel("Enter a sentence and press Enter"); container.add(promptLabel); inputField = new JTextField(20); inputField.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { StringTokenizer tokens = new StringTokenizer(event.getActionCommand()); outputArea.setText("Number of elements : " + tokens.countTokens() + "\nThe tokens are:\n" ); while(tokens.hasMoreTokens()) outputArea.append(tokens.nextToken() + "\n" ); } } ); container.add(inputField); outputArea = new JTextArea(10,20); outputArea.setEditable(false); container.add(new JScrollPane(outputArea)); setSize(275,240); setVisible(true); } public static void main(String args[]) { String6 application = new String6(); application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); } }


----------------解决方案--------------------------------------------------------
// 问题出在,你在用 outputArea 这个对象之前, 并没有建立这个对象!!!
// 修改如下:
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class String6 extends JFrame
{
  private JLabel promptLabel;
  private JTextField inputField = new JTextField(20);
  private JTextArea outputArea = new JTextArea(10,20);

  public String6()
  {
    super("Testring Class StirngTokenizer");
    Container container = getContentPane();
    container.setLayout(new FlowLayout());
  
    promptLabel = new JLabel("Enter a sentence and press Enter");
    container.add(promptLabel);
  
   
    inputField.addActionListener(
       new ActionListener()
       {
         public void actionPerformed(ActionEvent event)
         {
           StringTokenizer tokens =
              new StringTokenizer(event.getActionCommand());
           outputArea.setText("Number of elements : " +
              tokens.countTokens() + "\nThe tokens are:\n" );
           while(tokens.hasMoreTokens())
             outputArea.append(tokens.nextToken() + "\n" );
         }
       });
    container.add(inputField);
   
    outputArea.setEditable(false);
    container.add(new JScrollPane(outputArea));
    setSize(275,240);
    setVisible(true);
  }
  public static void main(String args[])
  {
    String6 application = new String6();
    application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
  }
}
----------------解决方案--------------------------------------------------------
其实没问题,只是你把

public class String6 extends JFrame{
private JLabel promptLabel;
private JTextField inputField;
private JTextArea ouputArea;

这里面的那个outputArea的t少写了,哈哈......
----------------解决方案--------------------------------------------------------
以下是引用宇扬在2005-9-26 16:45:52的发言: 其实没问题,只是你把 public class String6 extends JFrame{ private JLabel promptLabel; private JTextField inputField; private JTextArea ouputArea; 这里面的那个outputArea的t少写了,哈哈......
你偷偷告诉人家啊,弄的人家多不好意思!!!!!!!!!!
----------------解决方案--------------------------------------------------------
  相关解决方案