Java:如何将代码从按钮单击更改为onload

Java:如何将代码从按钮单击更改为onload,java,swing,onload,Java,Swing,Onload,我有以下代码可以正常工作,但我希望在加载时显示人名,而不是在用户单击按钮时显示人名。 我将如何实现这一点 public class NameSwing implements ActionListener { private JTextArea tf = new JTextArea(20, 20); private JFrame f = new JFrame("names"); private JButton b = new JButton("view"); st

我有以下代码可以正常工作,但我希望在加载时显示人名,而不是在用户单击按钮时显示人名。 我将如何实现这一点

public class NameSwing implements ActionListener {

    private JTextArea tf = new JTextArea(20, 20);
    private JFrame f = new JFrame("names");
    private JButton b = new JButton("view");
    static String fullName;

    public NameSwing() {
        f.add(new JLabel("Name"));
        tf.setEditable(true);
        f.add(tf);

        b.addActionListener(this);
        f.add(b);

        f.setLayout(new FlowLayout());
        f.setSize(600, 600);
        f.setVisible(true);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == b) {
            tf.setText(fullName);
        }
    }

    public static void main(String[] args) throws FileNotFoundException, IOException {
        NameSwing nameSwing = new NameSwing();

        Names t = new Names();
        t.OpenFile();
        t.ReadFile();
        t.CloseFile();

        fullName = Names.fullName;
    }

}
名称
类别:

package names;

public class Names {

    Scanner scan;
    static String Firstname = null;
    static String Surname;
    static String Fullname;
    static String fullName;
    String myArray[];

    public void OpenFile() {
        try {
            scan = new Scanner(new File("/Users/nikhilpatel/NetBeansProjects/Names/src/names/test.txt"));
            System.out.println("File found!");
        } catch (Exception e) {
            System.out.println("File not found");
        }
    }

    public void ReadFile() {
        while (scan.hasNext()) {
            Firstname = scan.next();
            Surname = scan.next();
            Fullname += Firstname + " " + Surname + "\n";
            fullName = Fullname.replace("null", "");

            System.out.println(fullName);
        }

    }

    public void CloseFile() {
        scan.close();
    }
}

只需在中添加
actionPerformed
内容。或者在
actionPerformed
中创建一个新方法(称为buttonClick或其他方法)并将其命名为1.)和2。)在
main

的末尾,根据您想要实现结果的“优雅程度”,有几种选择,一种方法是重构代码,将
actionPerformed
方法体提取为更独立的方法,然后在
main
中调用该公共方法:

@Override
public void actionPerformed(ActionEvent e) {
   fillTextArea();
}

public void fillTextArea() {
    // You can drop this line, this Listener is registered for 'b', so 'b' 
    // is the only one who fires the ActionEvent, not need to recheck 
    //
    // if (e.getSource() == b) {

    tf.setText(fullName);
}

public static void main(String[] args) throws FileNotFoundException, IOException {
    NameSwing nameSwing = new NameSwing();

    Names t = new Names();
    t.OpenFile();
    t.ReadFile();
    t.CloseFile();

    fullName = Names.fullName;

    // Invoke the new method
    nameSwing.fillTextArea();
}
另一种方法(也是一种棘手的方法)是像这样简单地调用from
b

// Add a getter for 'b' 
public JButton getButton() { 
    return b;
}

public static void main(String[] args) throws FileNotFoundException, IOException {
    NameSwing nameSwing = new NameSwing();

    Names t = new Names();
    t.OpenFile();
    t.ReadFile();
    t.CloseFile();

    fullName = Names.fullName;

    // This will simulate a 'click' on the button, loading the names 
    nameSwing.getButton().doClick();
}
希望这有助于

1)请学习类、方法和属性名称的通用(特别是用于名称的大小写)并一致使用。2)
Fullname+=Firstname+“”+姓氏+“\n”不要硬编码EOL。Java有它的属性。3) 给定数据的形式,我倾向于在
JTable
// Add a getter for 'b' 
public JButton getButton() { 
    return b;
}

public static void main(String[] args) throws FileNotFoundException, IOException {
    NameSwing nameSwing = new NameSwing();

    Names t = new Names();
    t.OpenFile();
    t.ReadFile();
    t.CloseFile();

    fullName = Names.fullName;

    // This will simulate a 'click' on the button, loading the names 
    nameSwing.getButton().doClick();
}