[英]Java, multi class instance variable referencing
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類將在CountryComponent方法內部引用所做選擇的“ csvName”,但沒有
我現在絕對迷路了,我想嘗試將選擇作為參數傳遞到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);
構造函數必須沒有void
。 它沒有返回類型。
public CountryComponent() throws IOException
{
...
}
public CountryComponent(String test) throws IOException {
...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.