2013-10-29 84 views
0
public class FrameViewer 
{ 

    String csvName = "none"; 
    public static void main(String[] args)throws IOException 
    { 
     System.out.println("Which file would you like to open?" + " A - asia.csv" + " B - europe.csv" + " C - africa.csv"); 
     Scanner input = new Scanner(System.in); 
     String csvName = input.next(); 

     if (csvName.equals("A")) 
      csvName = "asia.csv"; 
     else if (csvName.equals("B")) 
      csvName = "europe.csv"; 
     else if (csvName.equals("C")) 
      csvName = "africa.csv"; 
     else 
      System.out.println("You havent chosen a file."); 

这里是我有一个问题,我是假设当下面创建画布时,CountryComponent类将引用从所做的选择'csvName' CountryComponent方法,但它没有爪哇,多类实例变量参考

现在我绝对失去了,我想尝试作为参数传递给getData方法,但我无法弄清楚如何传递选择本身,我不断得到一个错误

我留在组件类的实例变量csvName中,因为它可能导致问题,但idk,它不会让我编译没有它。

 JFrame frame = new JFrame(); 
     frame.setSize(750, 650); 
     frame.setTitle("Country Data"); 
     CountryComponent canvas = new CountryComponent(); 
     frame.add(canvas); 
     frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); 
     frame.setVisible(true); 
    } 
} 

组件类

public class CountryComponent extends JComponent 
{ 

    // instance variables 

    String csvName; 

    public void CountryComponent()throws IOException 
    { 

     getData(); 

    } 

    public void getData()throws IOException 
    { 
     ... 

这是我试过了,还有最后一次尝试,我已经做了尝试像20个不同的东西,试图得到真正得到所显示的信息。

public void CountryComponent(String test)throws IOException 
{ 
    String csv = test; 
    getData(csv); 

} 

public void getData(String csv1)throws IOException 
{ 
    try 
    { 
     csvName = csv1; 
     File csvFile = new File(csvName); 

其编译但此行的FrameViewer类给了错误

CountryComponent canvas = new CountryComponent(csvName); 

回答

0

constructor有是没有void。它没有返回类型。

public CountryComponent() throws IOException 
{ 
... 
} 

public CountryComponent(String test) throws IOException { 
... 
} 
+0

我现在觉得真的很愚蠢,因为我认为我需要它,因为它没有返回任何东西,只是调用其他方法来检索信息 –