简体   繁体   English

单击按钮没有任何反应

[英]Nothing happens when button is clicked

I'm writing a program in Java where I'm using JTabbedPane. 我在用JTabbedPane用Java编写程序。 Each tab is associated with a different panel with labels, textfields and a button. 每个选项卡都与带有标签,文本字段和按钮的不同面板相关联。 I have used GridBagLayout in the panels. 我在面板中使用了GridBagLayout。 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. 编辑:我也有JTabbedPane之外的其他按钮,可以很好地工作。

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? 使用按钮和GridBagLayout / JTabbedPane是否存在任何常见问题?

EDIT with SSCCE 用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. 您根本不需要getSource检查-(希望)您的侦听器仅附加到一个按钮,因此,如果调用它,则意味着已单击该按钮。 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: 第1部分:

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

Part 2: 第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()" 另外 :Java GUI可能很挑剔,而不是使用“ e.getSource()== button”,您可以尝试“ button..isFocusOwner()”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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