1

我有一个文件阅读器,需要将文本用于标签循环的文本。我遇到了麻烦。我需要标签中的文本是 setLabel 方法中的名称数组。

主类一个包构建;

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();
                }
        });
    }
}
4

2 回答 2

1

在块之前移动String[] namesin的声明,并在 catch 之后类似地移动 return 语句,例如setLable()try

    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,那么它将返回nullthrows此外,当您处理异常(打印消息)时,我也删除了。

最后,方法名称听起来不合适。最好将其命名为getLablesor getLableNames

您需要使用String[]of 标签将字符串设置为:

  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
于 2012-12-01T22:38:24.117 回答
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().

于 2012-12-01T22:44:39.600 回答