GridLayout JButton JOptionPane,每个按钮带有ActionListener

时间:2018-10-05 23:11:03

标签: java swing jbutton actionlistener joptionpane

我正在制作日式约会游戏风格的约会游戏,提供图片和有趣娱乐的答案。我正在尝试为网格布局中的每个按钮显示一个unique_substrings = list(set([p[i:j+1+i] for i in range(len(p)) for j in range(len(p))]))消息对话框,作为对每个选项的响应。这样,它就像一棵逻辑树。我不习惯使用动作侦听器,因为我是一个初学者。这是我的代码。我只是不习惯这样做的语法。

有人可以帮助我吗?

JOptionPane

2 个答案:

答案 0 :(得分:0)

  

但是我已经实例化了扩展jpanel的父类

您是否看了教程中提供的示例代码???

示例“

... extends JFrame implements ActionListener

因此,您所需要做的就是:

... extends JPanel implements ActionListener

或者,如果您需要多个ActionListener,则更灵活的方法来创建自定义类。

您可以为ActionListener使用“匿名内部类”。像这样:

ActionListener al = new ActionListener()
{
    public void actionPerformed(ActionEvent e)
    {
        JButton button = (JButton)e.getSource();
        String text = button.getText();
        Window window = SwingUtilities.windowForComponent(button);
        JOptionPane.showMessageDialog(window, text);
    }
};

然后,当您创建按钮时,将执行以下操作:

for (String btnText : BTN_TEXTS) 
{
     JButton button = new JButton( btnText );
     button.addActionListener( al );
     southBtnPanel.add( button );
}

答案 1 :(得分:0)

查看以下内容,以了解如何向按钮添加动作侦听器。
请注意评论:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class NestedPanels extends JPanel {

    private static final String[] BTN_TEXTS = { "Say Hello", "Say You Look Good", "Say Sorry I'm Late" }; //three buttons
    //never used :   private static final int TITLE_POINTS = 3;
    public NestedPanels() {

        JPanel southBtnPanel = new JPanel(new GridLayout(3, 2, 1, 1));
        for (String btnText : BTN_TEXTS) {
            JButton b = new JButton(btnText);
            //add action listener
            b.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    buttonClicked(e);//when button clicked, invoke method
                }
            });

            //alternative much shorter way to add action listener:
            //b.addActionListener(e -> buttonClicked());
            southBtnPanel.add(b);
        }
        setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
        setLayout(new BorderLayout());

        //this adds Box to the default  BorderLayout.CENTER position
        add(Box.createRigidArea(new Dimension(600, 600)));

        add(southBtnPanel, BorderLayout.SOUTH);
    }

    //respond to button clicked
    private void buttonClicked(ActionEvent e) {
        String msg = ((JButton)e.getSource()).getActionCommand()+" pressed" ;
        JOptionPane.showMessageDialog(this, msg ); //display button Action
    }

    private static void createAndShowGui() {
        NestedPanels mainPanel = new NestedPanels();
        JFrame frame = new JFrame("Date Sim 1.0");
        //no need to invoke twice frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        //no need to invoke twice  frame.pack();
        //no need to invoke twice frame.setVisible(true);

        frame.getContentPane().add(mainPanel);

        /*
         * when posting images, use web resources that anyone can access
         *
        ImageIcon icon = new ImageIcon("C:/Users/wchri/Pictures/10346538_10203007241845278_2763831867139494749_n.jpg");
        JLabel label = new JLabel(icon);

        *this adds label to the default BorderLayout.CENTER position, which is already taken by
        *the Box. Only one (last) component will be added
        mainPanel.add(label);
        *
        */

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //remove all code which is not essential to the question
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                createAndShowGui();
            }
        });
    }
}