Java – how and where should I add an actionlistener to my code?

I wrote the following code with a text field and a button After entering characters and pressing the button, a label is created with the same title as the one entered in the field

Several tabs can be created in the same way Now again in the new tab, there is a long text pane for the text fields and buttons to display the results

I want to display the text entered into the text field in the text pane of each label

Now please lead me to learn how and where to put the listener on the tab button... And recommend any other necessary listener (I think there should be another listener to guide me to the tab I focus on or select)

It should be mentioned that I have added these tabs to the array list for any reuse, but I don't know if I use it correctly or how to use it?

package test;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;

public class TestGUI extends JFrame {


    private JTextField jTextField1;
    private JButton jButton1;
    static ArrayList<JPanel> ary = new ArrayList<JPanel>();
    private int tabIndex = 0;
    static int index = 0;
    private JTabbedPane tabbedPane;

    /**
    * @param args
    */
    public TestGUI() {

        super("Testing Tab Frame");
        setLayout(null);

        Handler but1 = new Handler();

        jTextField1 = new JTextField();
        jTextField1.setVisible(true);
        jTextField1.setBounds(12,12,85,30);
        add(jTextField1);

        jButton1 = new JButton("Button1");
        jButton1.setVisible(true);
        jButton1.setBounds(130,30);
        add(jButton1);
        jButton1.addActionListener(but1);

        tabbedPane = new JTabbedPane();
        tabbedPane.setBounds(12,54,200,150);
        tabbedPane.setVisible(false);
        add(tabbedPane);
        pack();
        setSize(250,110);
        setLocationRelativeTo(null);

    }

    private class Handler implements ActionListener {

        public void actionPerformed(ActionEvent evt) {
            String input = jTextField1.getText();
            if (!input.isEmpty()) {
                setSize(250,250);
                JPanel inst = createPanel(input);
                inst.setVisible(true);
                tabbedPane.addTab(Integer.toString(tabIndex),inst);
                tabbedPane.setVisible(true);
            }

        }
    }

    protected JPanel createPanel(String input) {
        JPanel inst = new JPanel();
        inst.setVisible(true);
        JTextField textField = new JTextField();
        textField.setVisible(true);
        textField.setBounds(12,80,30);
        JButton button = new JButton();
        button.setVisible(true);
        button.setBounds(100,30);
        JTextPane textPane = new JTextPane();
        textPane.setBounds(12,168,40);
        inst.add(textPane);
        textPane.setVisible(true);
        inst.setLayout(null);
        inst.add(button);
        inst.add(textField);
        ary.add(inst);
        tabIndex = index;
        index++;
        return inst;
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        TestGUI inst = new TestGUI();
        inst.setVisible(true);
    }

}

Solution

You can add actionlistener to the button in the createpanel method So your approach is this (make some assumptions about what you really want to do with text because it's not clear):

protected JPanel createPanel(String input) {
    JPanel inst = new JPanel();
    inst.setVisible(true);
    final JTextField textField = new JTextField();
    textField.setVisible(true);
    textField.setBounds(12,30);
    JButton button = new JButton();        
    button.setVisible(true);
    button.setBounds(100,30);
    final JTextPane textPane = new JTextPane();
    textPane.setBounds(12,40);
    inst.add(textPane);
    textPane.setVisible(true);

    button.addActionListener(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent arg0) {
            textPane.setText(textPane.getText() + textField.getText());
        }});

    inst.setLayout(null);
    inst.add(button);
    inst.add(textField);
    ary.add(inst);
    tabIndex = index;
    index++;
    return inst;
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>