2013-06-03 69 views
0

我有一个包含两个Jbutton的JPanel。目的是当我推动第一个Jbutton(“你有预测价值......”)时,另一个JPanel弹出,我可以看到其他创建的Jbutton。问题是,当我运行代码时,我可以看到第一个面板,但是当我点击按钮时,没有任何反应。如果你能帮助我,那将会很棒。在JPanel中添加ActionListener

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

据我所知,您绝不会将'myPanel1'添加到'myPanel'中 – MadProgrammer

+2

您可能还想看看[如何使用CardLayout](http://docs.oracle.com/javase/tutorial /uiswing/layout/card.html) – MadProgrammer

+0

感谢您的评论。你是对的。我编辑了代码,但仍然有问题。 – MTT

回答

0

你有两个问题。首先,你应该使用一个JDialog框架来显示mypanel1 - 我不认为你可以单独显示JPanel。

所以,当点击该选项时,创建一个新的JDialog并添加第二个JPanel。一定要调用JDialog盒上的setVisible方法。

现在,你将会遇到另一个问题。您创建的第一帧(showConfirm消息)将获取所有actionEvents,并且您的JDialog将不会获取任何内容。而且,由于您在作为JOption框的父框架中传入null,所以您的新JDialog将无法“requestFocus”,因此不会收到任何actionEvents。

因此,您需要重构代码以确保生成的任何新JDialogBox都可以请求焦点。