2012-12-01 1 views
1

나는 파일 판독기가있어서 레이블 루프 텍스트에 텍스트를 사용해야합니다. 문제가 있습니다. 레이블에있는 텍스트가 setLabel 메서드의 이름을 나타 내기 위해 필요합니다.java from String [] from

메인 클래스 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.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

try 블록 이전 setLable()String[] names의 선언을 이동하고 유사하게, 예를 들어 캐치 후 return 문을 이동 완료 읽기 파일 클래스

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; 

    } 


} 

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을 제거했습니다.

마지막으로 메서드 이름이 적절하지 않은 것으로 나타납니다. getLables 또는 getLableNames이라는 이름을 붙여야합니다.

당신은 같은 문자열을 설정하는 lables의 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() 위치 : 변수 유형 – user1516251

+0

도와 주셔서 감사합니다 – user1516251

+0

@ user1516251 답변을 업데이트했습니다. 또한 예외 (메시지 인쇄)를 처리 할 때 메서드 (setLable)에서 throw를 제거했습니다. –

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[] setLabel()이어야합니다.

+0

내가 hadnt 그 감사를 발견했다 – user1516251

+0

난 아직도 st.setLabel() 어디에서 내가 그 자리에 기호를 찾을 수 없습니다 오류가 발생합니까? – user1516251

+0

당신이합니까? 철자 오류를 수정 한 후에? 확실합니까? 기묘한. 정확한 오류 메시지는 무엇입니까? 어쨌든, 코드의 철자가 틀린 사실조차도 당신이 어떤 종류의 IDE도 사용하고 있지 않고, 뭔가를 작성하고 컴파일할지 여부를 알려주고 있습니다. Eclipse 또는 NetBeans를 설치하고 사용하십시오. 오타에 대해 즉시 경고 할 수 있으며 사소한 실수에 대한 수정을 제안 할 수도 있습니다. – ZeroOne