[英]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.