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、を返しnullます。throwsまた、例外を処理している(メッセージを出力している)ため、を削除しました。

最後に、メソッド名は適切に聞こえません。getLablesまたはとして名前を付ける必要がありますgetLableNames

String[]文字列を次のように設定するには、oflablesを使用する必要があります。

  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 に答える