繁体   English   中英

单击按钮没有任何反应

[英]Nothing happens when button is clicked

我在用JTabbedPane用Java编写程序。 每个选项卡都与带有标签,文本字段和按钮的不同面板相关联。 我在面板中使用了GridBagLayout。 我已经在按钮上添加了一个动作监听器,但是当我单击它时什么也没有发生。 编辑:我也有JTabbedPane之外的其他按钮,可以很好地工作。

我可以看到没有任何反应,因为我这样做:

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

而且什么也没打印出来。

使用按钮和GridBagLayout / JTabbedPane是否存在任何常见问题?

用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);
                            }
                        } );
    }

}

解决了

您根本不需要getSource检查-(希望)您的侦听器仅附加到一个按钮,因此,如果调用它,则意味着已单击该按钮。 删除支票并无条件打印您的字符串。 如果仍然看不到任何内容, 则说明您有问题。

您可能尚未在实际按钮上附加处理程序,因此该事件将永远不会被调用。

第1部分:

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

第2部分:

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

        }
   }

另外 :Java GUI可能很挑剔,而不是使用“ e.getSource()== button”,您可以尝试“ button..isFocusOwner()”

暂无
暂无

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

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