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

时间:2012-12-13 01:03:07

标签: 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");
    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;
    }

}

Names上课:

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

2 个答案:

答案 0 :(得分:1)

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;
    tf.setText(fullName);
}

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

答案 1 :(得分:1)

有几种选择,取决于你想要实现结果的“优雅”,可以重构你的代码,将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();
}

另一种方法(也是一种棘手的方法)是简单地从b调用method doClick,如下所示:

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

希望这有帮助