2012-12-01 55 views
1

我有一个filereader,并需要使用文本的标签循环的文本。我遇到了麻烦。我需要标签中的文本是来自setLabel方法的名称列表。java返回String []方法

主类a 包构建;

import java.io.IOException; 
import java.awt.*; 
import javax.swing.BorderFactory; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JRadioButton; 
import javax.swing.JPanel; 

public class a { 
    final static boolean shouldFill = true; 
    final static boolean shouldWeightX = true; 
    final static boolean RIGHT_TO_LEFT = false; 

    public static void addComponentsToPane(Container pane) { 
     a st = new a(); // object <<<<<<<<<<<<<<<<<<<< 

     if (RIGHT_TO_LEFT) { 
      pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); 
     } 

      JButton button; 
     JLabel headlabel; 
     JLabel label[] = new JLabel[10]; 
     JRadioButton radioButton; 
     JPanel panel1, panel2, panel3, panel4; 


      pane.setLayout(new GridBagLayout()); 
      GridBagConstraints c = new GridBagConstraints(); 

panel1 = new JPanel(new GridBagLayout()); 
    c.gridx = 0; 
    c.gridy = 0; 
    pane.add(panel1, c); 
    panel2 = new JPanel(new GridBagLayout()); 
    c.gridx = 0; 
    c.gridy = 1; 
    pane.add(panel2, c); 
    panel3 = new JPanel(new GridBagLayout()); 
    c.gridx = 0; 
    c.gridy = 2; 
    pane.add(panel3, c); 
     for (int i = 0; i < 10; i = i+1) { 
      for (int e = 0; e < 8; e = e+1) { 
       label[i]= new JLabel(st.setLabel()); // return String[] stocknames <<<<<<<<<<< 
       label[i].setBorder(BorderFactory.createLineBorder(Color.black)); 
        c.fill = GridBagConstraints.HORIZONTAL; 
        c.ipady = 0;  //reset to default 
        c.weighty = 1.0; //request any extra vertical space 
        c.anchor = GridBagConstraints.PAGE_END; //bottom of space 
        //c.insets = new Insets(5,5,5,5); //top padding 
        c.gridx = e;  //aligned with button 2 
        //c.gridwidth = 100; //2 columns wide 
        c.gridy = i;  //third row 
        panel3.add(label[i], c); 
      } 
     } 
    } 


    /** 
    * Create the GUI and show it. For thread safety, 
    * this method should be invoked from the 
    * event-dispatching thread. 
    */ 
    private static void createAndShowGUI() { 
      //Create and set up the window. 
      JFrame frame = new JFrame("Stock Table"); 
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

      //Set up the content pane. 
     //frame.setPreferredSize(new Dimension(600, 600)); 
      addComponentsToPane(frame.getContentPane()); 

      //Display the window. 
      frame.pack(); 
      frame.setVisible(true); 
     } 


     public static void main(String[] args) { 

      //Schedule a job for the event-dispatching thread: 
      //creating and showing this application's GUI. 
      javax.swing.SwingUtilities.invokeLater(new Runnable() { 
        public void run() { 
         createAndShowGUI(); 
        } 
      }); 
     } 

    public String[] setLable() throws IOException { 
     String file_name = "path"; 

     try { 
      ReadFile file = new ReadFile(file_name); 
      String[] aryLines = file.OpenFile(); 
      String[] names = new String[aryLines.length]; 
      String[] links = new String[aryLines.length]; 

      int i; 
      for(i=2; i < aryLines.length; i++) { 
       //System.out.println(aryLines[i]); 
       int firstquote = aryLines[i].indexOf("\"")+1; 
       int secondquote = aryLines[i].indexOf("\"", firstquote+1); 
       int thirdquote = aryLines[i].indexOf("\"", secondquote+1)+1; 
       int fourthquote = aryLines[i].indexOf("\"", thirdquote+1); 
       names[i] = aryLines[i].substring(firstquote, secondquote); 
       links[i] = aryLines[i].substring(thirdquote, fourthquote); 


      } 

     return names; 
     } 
     catch (IOException e) { 
      System.out.println(e.getMessage()); 
     } 

    } 

} 

读文件类

package build; 

import java.io.IOException; 
import java.io.FileReader; 
import java.io.BufferedReader; 

public class ReadFile { 
    private String path; 

    public ReadFile(String file_path) { 
     path = file_path; 
    } 

    public String[] OpenFile() throws IOException { 

     FileReader fr = new FileReader(path); 
     BufferedReader textReader = new BufferedReader(fr); 

     int numberOfLines = readLines(); 
     String[] textData = new String[numberOfLines]; 

     int i; 

     for(i=0; i < numberOfLines; i++) { 
      textData[i] = textReader.readLine(); 
     } 

     textReader.close(); 
     return textData; 

    } 

    int readLines() throws IOException { 

     FileReader file_to_read = new FileReader(path); 
     BufferedReader bf = new BufferedReader(file_to_read); 

     String aLine; 
     int numberOfLines = 0; 

     while ((aLine = bf.readLine()) != null) { 
      numberOfLines++; 
     } 

     bf.close(); 

     return numberOfLines; 

    } 


} 

完成

package build; 


import java.io.IOException; 
import java.awt.*; 
import javax.swing.BorderFactory; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JRadioButton; 
import javax.swing.JPanel; 

public class a { 
    final static boolean shouldFill = true; 
    final static boolean shouldWeightX = true; 
    final static boolean RIGHT_TO_LEFT = false; 

    public static void addComponentsToPane(Container pane) { 
     a st = new a(); 
     if (RIGHT_TO_LEFT) { 
      pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); 
     } 

      JButton button; 
     JLabel headlabel; 
     JLabel label[] = new JLabel[10]; 
     JRadioButton radioButton; 
     JPanel panel1, panel2, panel3, panel4; 


      pane.setLayout(new GridBagLayout()); 
      GridBagConstraints c = new GridBagConstraints(); 
      if (shouldFill) { 
       //natural height, maximum width 
       c.fill = GridBagConstraints.HORIZONTAL; 
      } 

     panel1 = new JPanel(new GridBagLayout()); 
     c.gridx = 0; 
     c.gridy = 0; 
     pane.add(panel1, c); 
     panel2 = new JPanel(new GridBagLayout()); 
     c.gridx = 0; 
     c.gridy = 1; 
     pane.add(panel2, c); 
     panel3 = new JPanel(new GridBagLayout()); 
     c.gridx = 0; 
     c.gridy = 2; 
     pane.add(panel3, c); 


     String[] labels = st.setLabel(); 
     for (int i = 0; i < 10; i = i+1) { 
      for (int e = 0; e < 8; e = e+1) { 
       label[i]= new JLabel(labels[i]); 
       label[i].setBorder(BorderFactory.createLineBorder(Color.black)); 
        c.fill = GridBagConstraints.HORIZONTAL; 
        c.ipady = 0;  //reset to default 
        c.weighty = 1.0; //request any extra vertical space 
        c.anchor = GridBagConstraints.PAGE_END; //bottom of space 
        //c.insets = new Insets(5,5,5,5); //top padding 
        c.gridx = e;  //aligned with button 2 
        //c.gridwidth = 100; //2 columns wide 
        c.gridy = i;  //third row 
        panel3.add(label[i], c); 
      } 
     } 




    } 


    /** 
    * Create the GUI and show it. For thread safety, 
    * this method should be invoked from the 
    * event-dispatching thread. 
    */ 
    private static void createAndShowGUI() { 
      //Create and set up the window. 
      JFrame frame = new JFrame("Stock Table"); 
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

      //Set up the content pane. 
     //frame.setPreferredSize(new Dimension(600, 600)); 
      addComponentsToPane(frame.getContentPane()); 

      //Display the window. 
      frame.pack(); 
      frame.setVisible(true); 
     } 

     public String[] setLabel() { 
      String file_name = "path"; 
      String[] names = null; 
      try { 
       ReadFile file = new ReadFile(file_name); 
       String[] aryLines = file.OpenFile(); 
       String[] links = new String[aryLines.length]; 
       names = new String[aryLines.length]; 

       int i; 
       for(i=2; i < aryLines.length; i++) { 
         //System.out.println(aryLines[i]); 
         int firstquote = aryLines[i].indexOf("\"")+1; 
         int secondquote = aryLines[i].indexOf("\"", firstquote+1); 
         int thirdquote = aryLines[i].indexOf("\"", secondquote+1)+1; 
         int fourthquote = aryLines[i].indexOf("\"", thirdquote+1); 
         names[i] = aryLines[i].substring(firstquote, secondquote); 
         links[i] = aryLines[i].substring(thirdquote, fourthquote); 
       } 
     }catch (IOException e) { 
       System.out.println(e.getMessage()); 
      } 
      return names; 

     } 

     public static void main(String[] args) { 


     // get the values of x & y from class A 
     int x = ReadWrite.getX(); 
     int y = ReadWrite.getY(); 
     // print them out 
     //System.out.println("x = " + x + ", y = " + y); 
      //Schedule a job for the event-dispatching thread: 
      //creating and showing this application's GUI. 
      javax.swing.SwingUtilities.invokeLater(new Runnable() { 
       public void run() { 
        createAndShowGUI(); 
       } 
     }); 
    } 
} 
+0

我认为这是一个非常糟糕的主意,有一套方法来返回一些东西。考虑重构对象 –

回答

1

移动的String[] names宣言setLable()try块,同样抓后移动return语句如

public String[] setLable() { 
     String file_name = "path"; 
     String[] names = null; 
     try { 
     ReadFile file = new ReadFile(file_name); 
     String[] aryLines = file.OpenFile(); 
     String[] links = new String[aryLines.length]; 
     names = new String[aryLines.length]; 

     int i; 
     for(i=2; i < aryLines.length; i++) { 
      //System.out.println(aryLines[i]); 
      int firstquote = aryLines[i].indexOf("\"")+1; 
      int secondquote = aryLines[i].indexOf("\"", firstquote+1); 
      int thirdquote = aryLines[i].indexOf("\"", secondquote+1)+1; 
      int fourthquote = aryLines[i].indexOf("\"", thirdquote+1); 
      names[i] = aryLines[i].substring(firstquote, secondquote); 
      links[i] = aryLines[i].substring(thirdquote, fourthquote); 
     } 
     }catch (IOException e) { 
     System.out.println(e.getMessage()); 
     } 
     return names; 
    } 

请注意:如果存在的names人口之前任何异常,那么它会返回null。在处理异常时(打印消息),我还删除了throws

最后,方法名称听起来不合适。它应该更好地命名为getLablesgetLableNames

您需要使用标贴的String[]设置为字符串:

String [] lables = st.setLabel();//change to getLables 
    for (int i = 0; i < 10; i = i+1) { 
    for (int e = 0; e < 8; e = e+1) { 
     label[i]= new JLabel(lables[i]);//use string array index to set the string 
+0

我得到java:135错误:找不到符号标签[i] = new JLabel(st.setLabel());符号:方法setLabel()位置:变量st类型a – user1516251

+0

感谢您的帮助 – user1516251

+0

@ user1516251更新了答案。当您处理异常(打印消息)时,我还从方法(setLable)中删除了引发。 –

0

我不能完全肯定我理解正确的问题,但我认为你需要改变这一点:

for (int i = 0; i < 10; i = i+1) { 
    for (int e = 0; e < 8; e = e+1) { 
     label[i]= new JLabel(st.setLabel()); // return String[] stocknames <<<<<<<<<<< 

这样:

String[] labels = st.setLabel(); 
for (int i = 0; i < 10; i = i+1) { 
    for (int e = 0; e < 8; e = e+1) { 
     label[i]= new JLabel(labels[i]); 

另外请注意,你有public String[] setLable()拼写错误,应该是public String[] setLabel()

+0

我没有注意到,谢谢 – user1516251

+0

我仍然得到一个错误找不到符号st.setLabel()我把它放在哪里? – user1516251

+0

你呢?修复拼写错误之后?你确定?奇怪的。什么是完全错误信息?无论如何,你甚至在代码中拼写错误的事实告诉我,你并没有使用任何类型的IDE,而只是在写一些东西并试图编译它。请安装Eclipse或NetBeans并使用它,他们可以立即警告您错别字,并且通常甚至可以建议更正琐碎的错误。 – ZeroOne