在另一个方法中使用参数访问方法中的变量值

时间:2016-10-11 09:01:09

标签: java methods

正如你在public void form4()中看到的,我想创建一个JLabel数组,其大小取决于public void receiver(String rlc)中的变量x。 String rlc的值来自我的另一个类。如果不可能,请给我建议或其他方式。感谢

package name1;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JTextField;

public class NAME4 extends JFrame implements ActionListener
{
public int x;
String letter = "";
String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
JLabel label1;
JTextField part;

String count,count2;
int value;

double lite;
int arraycount;
int array;
JLabel[] parted;
String[] characters;
JButton okbutton;
int int1;
public String ameer;
JLabel label56;
public NAME4()
{
    super ("My Game4");
    setSize(500,500);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setResizable(true);
    setLocationRelativeTo(null);
    form4();

}
public void form4()
{
    label1 = new JLabel(letter);
    setLayout(new FlowLayout(1,250,25));
    part = new JTextField(null,3);
    okbutton = new JButton("OK");
    okbutton.addActionListener(this);
    add(label1);
    add(part);
    add(okbutton);
    parted = new JLabel[x];
}

public void receiver(String rlc)
{
    x = Integer.parseInt(rlc);        
    lite = 26/x;
    arraycount = (int) lite;
    array = arraycount +1 ;
    for (int a = 1; a < alphabet.length()+1; a++)
    {
        letter = letter + alphabet.charAt(a-1);
        if ((a) % x == 0)
        {
            letter = letter + "|";
        }
    }
    label1.setText(letter);
    characters = new String [array];
    int inc = x;
    int y;
    int h=0,z;
    for (z = 0 ; z < array ; z++)
    {
        characters[z] = "";
        for (y = h ; y < inc ; y++)
        {
            characters[z] += letter.charAt(y);
        }
            h += (x+1);
            inc = inc + (x+1);
        if (z == array - 2)
        {
            switch(x)
                {
                    case 1: inc = 52;
                        break;
                    case 2: inc = 38;
                        break;
                    case 3: inc = 34;
                        break;
                    case 4: inc = 32;
                        break;
                    case 5: inc = 31;
                        break;
                    case 6: inc = 30;
                        break;
                    case 7: inc = 29;
                        break;
                    case 8: inc = 29;
                        break;
                    case 9: inc = 28;
                        break;
                }
        }
    }
}//void receiver
@Override
public void actionPerformed(ActionEvent e) {

}

}

1 个答案:

答案 0 :(得分:0)

是的,您可以使用Sub try() Dim c As Range Dim c1 As Range Set c = Workbooks(MASTER_FILE_NAME).sheets("azzeetr").Cells(25, 1) Set c1 = Workbooks(REPORT_FILE_NAME).sheets("dfdsfvsdfvs").Cells(10, 1) c.Value = c1.Value Set c = c.offset(1) Set c1 = c1.offset(1) End Sub 方法访问x,因为form4()是一个类字段,可以通过您班级内的任何方法访问它。
但是,您要在x方法中初始化x,该方法在 receiver()之后执行。执行form4()时,form4()未初始化(默认为0),因此您的数组将具有0大小。

要解决此问题,您可以将最后一行从x移至form4(),如下所示:

receiver()