简体   繁体   English

GUI按钮ActionListeners

[英]GUI button ActionListeners

In my GUI program I have 4 sets of cards and I want to add an action listener to each Card button so that when it's clicked it goes invisible. 在我的GUI程序中,我有4套卡,并且我想向每个“卡”按钮添加一个动作监听器,以便在单击该按钮时它不可见。 I've tried the source.setVisible(), however it still doesn't work. 我已经尝试了source.setVisible(),但是它仍然无法正常工作。 I think my problem is that I created all the Card buttons through for loops, but I'm not entirely sure though. 我认为我的问题是我通过for循环创建了所有Card按钮,但是我不确定。

    import java.awt.GridBagConstraints;
    import java.awt.Dimension;
    import java.awt.GridBagLayout;
    import java.awt.Image;
    import java.awt.Insets;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.border.Border;
    import javax.swing.BorderFactory;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import java.awt.BorderLayout;
    public class CardGame extends JFrame implements ActionListener {
private JButton Deal;

CardGame(){
    setTitle("A Card Game");
    setSize(2000, 1000);

    final GridBagConstraints layoutSpec = new GridBagConstraints();
    layoutSpec.gridx = 0;
    layoutSpec.gridy = 0;

    final GridBagConstraints dealSpec = new GridBagConstraints();
    layoutSpec.gridx = 4;
    layoutSpec.gridy = 4;

    final GridBagConstraints dealSpecs = new GridBagConstraints();
    layoutSpec.gridx = 7;
    layoutSpec.gridy = 7;


    JPanel cardA = new JPanel();
    cardA.setPreferredSize(new Dimension(100, 100));
    add(cardA, BorderLayout.NORTH);
    for(int i = 0; i < 13; i++) {   //Loop that Creates a Hand of Cards for Hand A
        cardA.add(new JButton("Card A-" + Integer.toString(i+1)), layoutSpec);
    }

    JPanel cardB = new JPanel();
    cardB.setPreferredSize(new Dimension(100, 100));
    add(cardB, BorderLayout.EAST);
    for(int i = 0; i < 13; i++) {   //Loop that Creates a Hand of Cards for Hand B
        cardB.add(new JButton("Card B-" + Integer.toString(i+1)), layoutSpec);
    }

    JPanel cardC = new JPanel();
    cardC.setPreferredSize(new Dimension(100, 100));
    add(cardC, BorderLayout.WEST);
    for(int i = 0; i < 13; i++) {   //Loop that Creates a Hand of Cards for Hand C
        cardC.add(new JButton("Card C-" + Integer.toString(i+1)), layoutSpec);
    }

    JPanel cardD = new JPanel();
    cardD.setPreferredSize(new Dimension(100, 100));
    add(cardD, BorderLayout.SOUTH);
    for(int i = 0; i < 13; i++) {   //Loop that Creates a Hand of Cards for Hand D
        cardD.add(new JButton("Card D-" + Integer.toString(i+1)), layoutSpec);
    }

    JPanel fillCenter = new JPanel();
    fillCenter.setPreferredSize(new Dimension(100, 100));
    add(fillCenter, BorderLayout.CENTER);   //Adds a panel that fills the center of the JFrame
    fillCenter.add(new JButton("Deal"), dealSpec);  //Adds a Deal Button on top of the fillCenter JPanel
    fillCenter.add(new JButton("Reset"), dealSpecs);    //Adds a Reset Button on top of the fillCenter JPanel

}
@Override
   public void actionPerformed(ActionEvent event){
    if(event.getSource() == (Deal)){

    };
    if(event.getSource() == ("Deal"));
    for(int i = 0; i < 13; i++){
        if(event.getSource() == ("Card " + Integer.toString(i+1)));
    }
}


public static void main(String[] args) {
    CardGame cards = new CardGame();
    cards.setVisible(true);
    return;
}

} }

试试这个((JButton)event.getSource()).setVisible(false);

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

相关问题 ActionListeners:对于GUI中的每个按钮,是单独的侦听器还是所有按钮的一个侦听器? - ActionListeners: for each button in GUI a seperate listener or one listener for all buttons? 在方法内部使用ActionListeners创建GUI - Creating a GUI with ActionListeners inside a method 一个按钮上的多个功能(actionListeners) - Multiple functions (actionListeners) on one Button 如何为单个按钮具有多个ActionListeners - how to have multiple ActionListeners for a single button 无法使CardLayout /按钮ActionListeners正常工作 - Can't get CardLayout/button ActionListeners to Work 使我的 ActionListeners、Handlers 和 GUI 进行通信时遇到问题 - Having a problem getting my ActionListeners, Handlers, and GUI to communicate Java,大型GUI类,包含许多ActionListeners; 组织听众的最佳方式? - Java, Large GUI classes, with many ActionListeners; best way to organize the listeners? ActionListener,多个字段更新以及从文件中重新加载用户GUI选择 - ActionListeners, multiple field updates, and reloading user GUI selections from a file Java 和 GUI - 根据 MVC 模式,ActionListeners 属于哪里? - Java and GUI - Where do ActionListeners belong according to MVC pattern? actionListeners中的FileChooser - FileChooser in actionListeners
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM