开发者

what is the difference between write actionperformed in run() or outside of main

开发者 https://www.devze.com 2023-04-04 13:46 出处:网络
I write action performed code like outside of main private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

I write action performed code like outside of main

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
    jTextField1.setText("do something");
}

and it can be written in run() like

       jButton1.addActionListener(new java.awt.event.ActionListener(){


            public void actionPerformed(ActionEvent e) {
                jTextField1.setText("aaaaaaaaaa");
            }
        });

what is the difference between this two is there any programming overhead related issue or not which is the better coding style.

i am using netbeans when i write it in run() it will demand to make the jTextField1 to static becauce main mathod is static so i think firstone is not good because it will occupy memory until programme is stoped开发者_开发百科.am i correct?

2.if i want to do that all buttons actions are written in different class then how can i implement this in swing form.


Try to keep your main as small as possible, reduce it to just a call to the constructor and a setVisible(true).

    public void run() {
        new NewJFrame().setVisible(true);
    }

The reason for that is that you may need to reuse your class in an other application. It is always better to create all your widgets in the class, keeping the Netbeans layout.

If you use the second option, and happen to need to use this, it will refer to the ActionListener and not to the Swing widget your class is extending.

In order to put your ActionListener in another class make a class like this:

public class AListener implements ActionListener {

    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("B1"))
            JOptionPane.showMessageDialog(null, "B1 clicked!");
        else  if (e.getActionCommand().equals("B2"))
            JOptionPane.showMessageDialog(null, "B2 clicked!");
    }

}

and add the ActionListener to your button in the constructor after NB's initComponents() like that:

public NewJFrame() {
    initComponents();
    ActionListener al = new AListener();
    jButton1.addActionListener(al);
    jButton2.addActionListener(al);
}

For this to work you have to add the appropriate setActionCommand Strings for every button:

    jButton1.setActionCommand("B1");
    jButton2.setActionCommand("B2");

In Netbeans it can easily be done if you change the Post-Creation code of every button to include the String it is supposed to.

I think in "normal" cases (where you do not have 60 buttons, or so), it may still be better to stick to the standards. It is a matter of taste how you design your application.

Most of the time the actions of an application have to share data details that are local to your class. In order to avoid passing that information to your AListener explicitly, and in order to be able to use this, keep your ActionListeners together inside the class you are using.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号