我正在用Java编写程序,我正在使用JTabbedPane。每个选项卡都与一个带有标签,文本字段和按钮的不同面板相关联。我在面板中使用了GridBagLayout。 我在按钮上添加了一个actionlistener,但是当我点击它时没有任何反应。 编辑:我在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);
}
} );
}
}
解决
答案 0 :(得分:3)
您根本不需要getSource
检查 - 您的监听器(希望)仅附加到一个按钮,因此如果调用它,则表示已单击该按钮。取消支票并无条件打印您的字符串。如果您仍然没有看到任何内容,那么就会出现问题。
答案 1 :(得分:0)
您可能没有将处理程序附加到实际按钮,因此永远不会调用该事件。
第1部分:
ButtonHandler handler = new ButtonHandler();
button.addActionListener( handler );
第2部分:
public class ButtonHandler implements ActionListener
{
@Override
public void actionPerformed(ActionEvent event) {
}
}
ALSO :Java GUI可能很挑剔,而不是使用“e.getSource()==按钮”你可以尝试“button..isFocusOwner()”