简体   繁体   中英

Actionlisteners in constructor

I am trying to create NewCard class, with implements a frame. How can I add Actionlisteners to elements in constructor of NewCard class? I can't put Actionlistener into constructor, and when I put it outside, element "field" is invisible for saveButtonListener block..

Second question: class Record in try block throws two exceptions, why try block generate error?

package Interface;

import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;
import Engine.*;

class NewCard extends JFrame
{         
    NewCard()
    {
        JFrame Card = new JFrame(); 
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setTitle("New Card");
        setSize(340, 580);  
        setVisible(true);
        Container contentPane = getContentPane();
        contentPane.setLayout(null);

        // Field

        JTextField field = new JTextField();
        contentPane.add(field);
        field.setBounds(110,15,200,25);

        // Button:

        JButton saveButton = new JButton("Save");
        powZawartosci.add(saveButton);
        saveButton.setBounds(95,495,150,25);

        saveButtonListener listener1 = new saveButtonListener();
        saveButton.addActionListener(listener1);
    }

    private class saveButtonListener implements ActionListener 
    {
        try
        {
            @Override
            public void actionPerformed(ActionEvent event) 
            {
                new Record(field.getText());
            }
        }
        catch(IOException e)
        {
            System.out.println("IOException");
        }
        catch(SQLException e)
        {
            System.out.println("SQLException");
        }
        finally
        {

        }
    }
}

regarding:

I can't put Actionlistener into constructor, ...

Who says you can't? Simply add the ActionListener...

JButton saveButton = new JButton("Save");
saveButton.addActionListener(new SaveButtonListener()); // capitalize class names

Or you can use anonymous inner classes, or even better, use AbstractActions.

Edit 1:
Regarding:

Second question: class Record in try block throws two exceptions, why try block generate error?

If you have a question about an exception, it makes lots of sense to show the exception.

Edit 2
Regarding:

class NewCard extends JFrame
{         
    NewCard()
    {
        JFrame Card = new JFrame();

Why have the class extend JFrame and create a JFrame inside the class that is never used?Best to not have the class extend JFrame but rather to create the JFrame when needed.

You could put your action listener inside constructor like this:

final JTextField field = new JTextField();
...
saveButton.addActionListener(new ActionListener() {
   @Override
   public void actionPerformed(ActionEvent event) {
      new Record(field.getText());
   }

});

Pass field to the saveButtonListener by creating an appropriate constructor for the listener.

Or use an anonymous listener, as shown in Mersenne's answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM