2011-02-18 61 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

但要使其完全按照您的要求顯示(並使用循環!)將需要您進行大量的思考。