gpt4 book ai didi

java - 单击按钮时没有任何反应

转载 作者:行者123 更新时间:2023-12-01 23:49:47 26 4
gpt4 key购买 nike

我正在用 Java 编写一个程序,其中使用 JTabbedPane。每个选项卡都与带有标签、文本字段和按钮的不同面板相关联。我在面板中使用了 GridBagLayout。我已向按钮添加了一个 Action 监听器,但是当我单击它时没有任何反应。编辑:我在 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);
}
} );
}

}

已解决

Solution

最佳答案

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

关于java - 单击按钮时没有任何反应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16464142/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com