2011-02-18 69 views
1

我需要在更大的类中创建两个类。一个采取信息从一个文本文件格式: 字符串:双循环创建按钮

字符串:双

...

,并输出两个变量。 第二类接收这些信息并进行循环,创建带有每个文本条目的按钮作为标签。 我的代码到目前为止是:

public class MainClass { 
     Scanner readFile = new Scanner(new File("text.txt")); 
     while (fileScanner.hasNext()) { 
      String name = readFile.next(); 
      double value = readFile.nextDouble(); 
    } 
    class Button { 
     Button(String text. double number) { 
      this.text=text; 
      this.number=number; 
     } 
    } 
} 

我该怎么走?

+0

这段代码实际上并没有编译,是吗? – iluxa 2011-02-18 21:41:59

回答

1

不是一个答案,但这里的OP的代码修改,以便它编译

import java.io.File; 
import java.io.FileNotFoundException; 
import java.util.Scanner; 

import javax.swing.JButton; 

public class MainClass { 
    class ScanFile { 
     void Foo() throws FileNotFoundException{ 
      Scanner readFile = new Scanner(new File("text.txt")); // don't forget to catch FileNotFoundException! 
      readFile.useDelimiter(":|\\n"); 
      while (readFile.hasNext()) { 
       String name = readFile.next(); 
       double value = readFile.nextDouble(); 
       System.out.println(name + " " + value); 
      } 
     } 
    } 
    class Button extends JButton { 
     String text; 
     double number; 
     Button(String text, double number) { 
      super(text); 
      this.text=text; 
      this.number=number; 
     } 
    } 
} 
1

@詹姆斯,使得按键,而并非难事,确实需要Java的工作经验(因为你还必须知道如何创建框架,面板,ActionListeners,并处理点击按钮时的事件 - 足够的材料来填充教科书!)。

如果你只在一个窗口中进行一些按钮感兴趣,下面的教程应该给你如何使按钮一个基本框架的想法:

http://download.oracle.com/javase/tutorial/uiswing/components/frame.html

http://download.oracle.com/javase/tutorial/uiswing/components/button.html

但要使其完全按照您的要求显示(并使用循环!)将需要您进行大量的思考。