나는 파일 판독기가있어서 레이블 루프 텍스트에 텍스트를 사용해야합니다. 문제가 있습니다. 레이블에있는 텍스트가 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();
}
});
}
}
뭔가를 반환하는 메소드를 설정하는 것은 매우 나쁜 생각입니다. 리팩터링 객체 고려하기 –