简体   繁体   English

在JPanel中添加ActionListener

[英]Add ActionListener in JPanel

I have a JPanel that has two Jbuttons. 我有一个具有两个Jbutton的JPanel。 The purpose is that as soon as I push the first Jbutton ("Do you have predicted value ...."), another JPanel pop up and I can see other created Jbuttons. 目的是,一旦按下第一个Jbutton(“您是否有预测值....”),就会弹出另一个JPanel,并且可以看到其他创建的Jbutton。 The problem is that when I run the code, I can see the first panel, but when I click on the button, nothing happens. 问题是,当我运行代码时,我可以看到第一个面板,但是当我单击按钮时,什么也没有发生。 It would be great if you can help me. 如果您能帮助我,那就太好了。

public class Main {

    private static Component frame;
    private static JFileChooser inputFile;
    private static JFileChooser outputFile;
    private static String fullpath;
    private static String fullpath1;
    private static String fullpath2;
    private static String fullpath3;

    public static void main(String args[]) throws FileNotFoundException, IOException {

        try {

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.insets = new Insets(5, 5, 5, 5);

            JButton nextPanel = new JButton("Do you have predicted values or residual errors?");
            JButton inputButton = new JButton("Browse predictor dataset");

            JPanel myPanel = new JPanel(new GridBagLayout()); //new panel

            gbc.gridwidth = 1;
            gbc.gridheight = 1;
            gbc.gridx = 0;
            gbc.gridy = 1;
            gbc.anchor = (0 == 0) ? GridBagConstraints.WEST : GridBagConstraints.EAST;
            gbc.fill = (0 == 0) ? GridBagConstraints.BOTH
                : GridBagConstraints.HORIZONTAL;
            gbc.weightx = (0 == 0) ? 0.1 : 0.1;
            gbc.weighty = 1.0;
            myPanel.add(nextPanel, gbc);

            final JPanel myPanel1 = new JPanel(new GridBagLayout());
            myPanel.add(myPanel1);    

            nextPanel.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e){

                    GridBagConstraints gbc1 = new GridBagConstraints();
            gbc1.insets = new Insets(5, 5, 5, 5);
            JButton errorButton = new JButton("Browse residual error associated to each instance");
            JButton predictedButton = new JButton("Browse predicted value associated to each instance");
            gbc1.gridwidth = 1;
            gbc1.gridheight = 1;
            gbc1.gridx = 0;
            gbc1.gridy = 1;
            gbc1.anchor = (0 == 0) ? GridBagConstraints.WEST : GridBagConstraints.EAST;
            gbc1.fill = (0 == 0) ? GridBagConstraints.BOTH
                : GridBagConstraints.HORIZONTAL;
            gbc1.weightx = (0 == 0) ? 0.1 : 0.1;
            gbc1.weighty = 1.0;
            myPanel1.add(errorButton, gbc1);
                }
            });

            gbc.gridwidth = 1;
            gbc.gridheight = 1;
            gbc.gridx = 0;
            gbc.gridy = 9;
            gbc.anchor = (0 == 0) ? GridBagConstraints.WEST : GridBagConstraints.EAST;
            gbc.fill = (0 == 0) ? GridBagConstraints.BOTH
                : GridBagConstraints.HORIZONTAL;
            gbc.weightx = (0 == 0) ? 0.1 : 0.1;
            gbc.weighty = 1.0;
            myPanel.add(inputButton, gbc);

            inputButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    JFileChooser inputFile = new JFileChooser();
                    inputFile.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
                    if (inputFile.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
                        File file1 = inputFile.getSelectedFile();
                        String fullpathTemp = (String) file1.getAbsolutePath();
                        fullpath = fullpathTemp;
                    }
                 }
            });

             int result = JOptionPane.showConfirmDialog(null, myPanel, "CPM Program", JOptionPane.OK_CANCEL_OPTION);


 } catch (Exception e) {
             System.err.println("Error: " + e.getMessage());
         } finally {
         }
    }
}

You have two problems. 你有两个问题。 Firstly, you should use a JDialog frame in order to display mypanel1--I don't think you can just display JPanel by itself. 首先,您应该使用JDialog框架来显示mypanel1-我认为您不能仅显示JPanel。

So, , when the option is clicked, create a new JDialog and add your second JPanel to it. 因此,单击该选项时,创建一个新的JDialog并将第二个JPanel添加到其中。 Be sure to call the setVisible method on the JDialog box. 确保在JDialog框上调用setVisible方法。

Now, you will have another problem. 现在,您将遇到另一个问题。 The first frame that you created(the showConfirm message) is going to get all the actionEvents,and your JDialog will get none . 您创建的第一帧(showConfirm消息)将获取所有actionEvent,而您的JDialog将不获取任何内容。 And, since you passed in null as the parent frame for your JOption box, your new JDialog will not be able to "requestFocus" and thus, won't receive any actionEvents. 并且,由于您传入了空值作为JOption框的父框架,因此新的JDialog将无法“ requestFocus”,因此将不会收到任何actionEvents。

So, you will need to refactor your code in order to make sure that any new JDialogBox that is spawned can request focus. 因此,您将需要重构代码,以确保产生的任何新JDialogBox都可以请求焦点。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM