简体   繁体   中英

Nothing happens when button is clicked

I'm writing a program in Java where I'm using JTabbedPane. Each tab is associated with a different panel with labels, textfields and a button. I have used GridBagLayout in the panels. I have added an actionlistener to the button, but when I click it nothing happens. EDIT: I also have other buttons outside the JTabbedPane which works perfectly fine.

I can see that nothing is happening because I do this:

public void actionPerformed( ActionEvent e ) { 
    if ( e.getSource() == button ) { 
        System.out.println("blablabla");
    }

and nothing is printed out.

Is there any common problems with using buttons and GridBagLayout/JTabbedPane?

EDIT with SSCCE

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.*;

public class Hjelp extends JFrame { 
    private FlowLayout layout;
    private JButton button1; 
    private JButton button2;
    private JPanel menu, frontpage; 
    private JPanel present, previous, something;

    public Hjelp() {
        layout = new FlowLayout(FlowLayout.CENTER, 10, 20); 
        setLayout(layout);
        setSize(900, 900); 
        setLocationRelativeTo(null); 
        setVisible(true);
        setPanels();
        something = something();
        add(something, BorderLayout.CENTER);
        something.setVisible(false);
        button1 = new JButton("CLICK ME"); 
        add(button1);
        buttonListener();
    }

    private void buttonListener() {
        Buttonlistener listener = new Buttonlistener();
        button1.addActionListener(listener);
        button2.addActionListener(listener);

    }

    private void setPanels() {
        menu = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 0));
        frontpage = new JPanel();
        previous = frontpage;
        present = frontpage;
        add(menu);
    }

    public void visiblePanel() { 
        previous.setVisible(false);
        present.setVisible(true);
    }

    private JPanel something() {
        visiblePanel();
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(1, 1));
        JTabbedPane tabbedPane = new JTabbedPane();

        JComponent panel1 = tab();
        tabbedPane.addTab("Click me", panel1);
        tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);

        tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);

        panel.add(tabbedPane);

        return panel;
    }

    private JComponent tab() {
        JPanel panel = new JPanel(false);
        panel.setPreferredSize(new Dimension(870, 300));
        panel.setLayout(new GridBagLayout());
        GridBagConstraints cs = new GridBagConstraints();
        cs.fill = GridBagConstraints.HORIZONTAL;
        button2 = new JButton("Click me");
        cs.gridx = 1;
        cs.gridy = 6;
        cs.gridwidth = 1;
        panel.add(button2, cs);
        return panel;
  }

        private class Buttonlistener implements ActionListener { 
        @Override
        public void actionPerformed( ActionEvent e ) { 
            if ( e.getSource() == button1 ) { 
                    present = something; 
                    button1.setVisible(false);
                    something();
                    previous = something;
                }
                else if (e.getSource() == button2) {
                    System.out.println("Blablabla");
                }

        }
    }



    public static void main(String [] args) { 
        final Hjelp vindu = new Hjelp();
        vindu.addWindowListener(
                        new WindowAdapter() {
                            @Override
                            public void windowClosing(WindowEvent e) {
                                System.exit(0);
                            }
                        } );
    }

}

SOLVED

Solution

You don't need the getSource check at all—your listener is (hopefully) attached to just one button, so if it was invoked, that already means the button was clicked. Remove the check and unconditionally print your string. If you still don't see anything, then you have a problem.

You may not have attached a handler to the actual button, and therefore the event will never get called.

Part 1:

        ButtonHandler handler = new ButtonHandler();
        button.addActionListener( handler );

Part 2:

public class ButtonHandler implements ActionListener 
   {
        @Override
        public void actionPerformed(ActionEvent event) {    

        }
   }

ALSO : Java GUI can be finicky, rather than using "e.getSource() == button" you could try "button..isFocusOwner()"

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