0

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

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

2 回答 2

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

另一种方法(也是一种棘手的方法)是像这样简单地调用该方法doClickb

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

希望这可以帮助

于 2012-12-13T03:59:04.263 回答
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东西添加进去。或者创建一个新方法(称为 buttonClick 或其他东西)并actionPerformedmain.

于 2012-12-13T01:04:53.013 回答