簡體   English   中英

在JPanel中添加ActionListener

[英]Add ActionListener in JPanel

我有一個具有兩個Jbutton的JPanel。 目的是,一旦按下第一個Jbutton(“您是否有預測值....”),就會彈出另一個JPanel,並且可以看到其他創建的Jbutton。 問題是,當我運行代碼時,我可以看到第一個面板,但是當我單擊按鈕時,什么也沒有發生。 如果您能幫助我,那就太好了。

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 {
         }
    }
}

你有兩個問題。 首先,您應該使用JDialog框架來顯示mypanel1-我認為您不能僅顯示JPanel。

因此,單擊該選項時,創建一個新的JDialog並將第二個JPanel添加到其中。 確保在JDialog框上調用setVisible方法。

現在,您將遇到另一個問題。 您創建的第一幀(showConfirm消息)將獲取所有actionEvent,而您的JDialog將不獲取任何內容。 並且,由於您傳入了空值作為JOption框的父框架,因此新的JDialog將無法“ requestFocus”,因此將不會收到任何actionEvents。

因此,您將需要重構代碼,以確保產生的任何新JDialogBox都可以請求焦點。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM