2011-04-29 1 views
3

J'ai un JPanel dans une autre classe et je veux qu'il s'exécute lorsque je clique sur le bouton "Entrée". Pour une raison quelconque, j'ai essayé d'écrire un simple system.out.println dans mon code et cela fonctionne mais quand je veux qu'il fonctionne avec le JPanel, il ne fonctionnera pas, est-ce que quelqu'un sait quel est mon problème?Comment faire apparaître mon JPanel lorsqu'une action listneer est appelée

import inf45.spring2010.examples.gui3.fileChooser; 

    import java.awt.*; 
    import java.awt.event.*; 

    import javax.swing.*; 
    public class clientGUI extends JFrame { 

/** 
* Variables 
*/ 

private JLabel nameLabel; 
private JLabel IPLabel; 
private JLabel portLabel; 
private JTextField nameText; 
private JTextField IPText; 
private JTextField portText; 
private JButton enterButton; 
private JButton cancelButton; 

/** 
* Constructor for the Frame 
*/ 

public clientGUI() { 

    JFrame frame = new JFrame(); 
    frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); 
    buildUI(); 

} 

private void buildUI(){ 

    // Setup the GridBagLayout 

    GridBagLayout layout = new GridBagLayout(); 
    getContentPane().setLayout(layout); 


    /** 
    * Set Up the JLabels 
    */ 

    nameLabel = new JLabel ("Name:"); 
    getContentPane().add(nameLabel); 
    layout.setConstraints(
     nameLabel, 
     new GridBagConstraints(
      0, 1, 2, 1, 1.0, 1.0, 
      GridBagConstraints.CENTER, GridBagConstraints.BOTH, 
      new Insets(5, 10, 5, 10), 0, 0)); 


    IPLabel = new JLabel ("IP Address:"); 
    getContentPane().add(IPLabel); 
    layout.setConstraints(
     IPLabel, 
     new GridBagConstraints(
      0, 2, 2, 1, 1.0, 1.0, 
      GridBagConstraints.CENTER, GridBagConstraints.BOTH, 
      new Insets(5, 10, 5, 10), 0, 0)); 

    portLabel = new JLabel("Port:"); 
    getContentPane().add(portLabel); 
    layout.setConstraints(
     portLabel, 
     new GridBagConstraints(
      0, 3, 2, 1, 1.0, 1.0, 
      GridBagConstraints.CENTER, GridBagConstraints.BOTH, 
      new Insets(5, 10, 5, 10), 0, 0)); 

    /** 
    * Setup JTextFields 
    */ 

    nameText = new JTextField (15); 
    getContentPane().add(nameText); 
    layout.setConstraints(
     nameText, 
     new GridBagConstraints(
      2, 1, 2, 1, 1.0, 1.0, 
      GridBagConstraints.CENTER, GridBagConstraints.BOTH, 
      new Insets(5, 10, 5, 10), 0, 0)); 

    IPText = new JTextField (15); 
    getContentPane().add(IPText); 
    layout.setConstraints(
     IPText, 
     new GridBagConstraints(
      2, 2, 2, 1, 1.0, 1.0, 
      GridBagConstraints.CENTER, GridBagConstraints.BOTH, 
      new Insets(5, 10, 5, 10), 0, 0)); 

    portText = new JTextField(15); 
    getContentPane().add(portText); 
    layout.setConstraints(
     portText, 
     new GridBagConstraints(
      2, 3, 2, 1, 1.0, 1.0, 
      GridBagConstraints.CENTER, GridBagConstraints.BOTH, 
      new Insets(5, 10, 5, 10), 0, 0)); 

    /** 
    * Setup JButtons 
    */ 

    enterButton = new JButton ("Enter"); 
    getContentPane().add(enterButton); 
    layout.setConstraints(
     enterButton, 
     new GridBagConstraints(
      0, 4, 2, 1, 1.0, 1.0, 
      GridBagConstraints.CENTER, GridBagConstraints.BOTH, 
      new Insets(5, 10, 5, 10), 0, 0)); 

    //Action Listener for enter button that prompts the user to the send GUI 

    enterButton.addMouseListener(new MouseAdapter() 

      { 
       public void mousePressed(MouseEvent e) 
       { 
        fileChooser enterFile = new fileChooser(); 
        enterFile.setVisible(true); 
        enterFile.setSize(150,150); 
        System.out.println("osama"); 

       } 
      }); 

    cancelButton = new JButton ("Cancel"); 
    getContentPane().add(cancelButton); 
    layout.setConstraints(
     cancelButton, 
     new GridBagConstraints(
      2, 4, 2, 1, 1.0, 1.0, 
      GridBagConstraints.CENTER, GridBagConstraints.BOTH, 
      new Insets(5, 10, 5, 10), 0, 0)); 

    //ActionListener for the cancel button that should clear all fields. 
    cancelButton.addActionListener(
      new ActionListener() 
      { 
       public void actionPerformed(ActionEvent e) 
       { 
        //cancelPressed(); 

       } 
      }); 

} 
    } 

import java.io.*; 
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class fileChooser extends JPanel implements ActionListener { 
    static private final String newline = "\n"; 
    JButton openButton, saveButton; 
    JTextArea log; 
    JFileChooser fc; 

    public fileChooser() { 
     super(new BorderLayout()); 

     //Create the log first, because the action listeners 
     //need to refer to it. 
     log = new JTextArea(5,20); 
     log.setMargin(new Insets(5,5,5,5)); 
     log.setEditable(false); 
     JScrollPane logScrollPane = new JScrollPane(log); 

     //Create a file chooser 
     fc = new JFileChooser(); 
     openButton = new JButton("Select a File..."); 
     openButton.addActionListener(this); 

     //Create the save button. We use the image from the JLF 
     //Graphics Repository (but we extracted it from the jar). 
     saveButton = new JButton("Send Selected File"); 
     saveButton.addActionListener(this); 

     //For layout purposes, put the buttons in a separate panel 
     JPanel buttonPanel = new JPanel(); //use FlowLayout 
     buttonPanel.add(openButton); 
     // buttonPanel.add(saveButton); 

     //Add the buttons and the log to this panel. 
     add(buttonPanel, BorderLayout.PAGE_START); 
     add(logScrollPane, BorderLayout.CENTER); 
    } 

    public void actionPerformed(ActionEvent e) { 

     //Handle SEND button action. 
     if (e.getSource() == openButton) { 
      int returnVal = fc.showOpenDialog(fileChooser.this); 

      if (returnVal == JFileChooser.APPROVE_OPTION) { 
       File file = fc.getSelectedFile(); 
       //This is where a real application would open the file. 
       log.append("Selected: " + file.getName() + "." + newline); 
       System.out.println("Sent " + file.getName()); 
      } else { 
       log.append("Open command cancelled by user." + newline); 
       System.out.println("File selection canceled."); 
      } 
      log.setCaretPosition(log.getDocument().getLength()); 
     } 
    } 
    /** Returns an ImageIcon, or null if the path was invalid. */ 
    protected static ImageIcon createImageIcon(String path) { 
     java.net.URL imgURL = fileChooser.class.getResource(path); 
     if (imgURL != null) { 
      return new ImageIcon(imgURL); 
     } else { 
      System.err.println("Couldn't find file: " + path); 
      return null; 
     } 
    } 
    /** 
    * Create the GUI and show it. For thread safety, 
    * this method should be invoked from the 
    * event dispatch thread. 
    */ 
    public static void createAndShowFileChooser() { 
     //Create and set up the window. 

     JFrame frame = new JFrame("FileChooserDemo"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     //Add content to the window. 
     frame.add(new fileChooser()); 

     //Display the window. 
     frame.pack(); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       //Turn off metal's use of bold fonts 
       UIManager.put("swing.boldMetal", Boolean.FALSE); 
       //Displays and creates the file chooser 
        createAndShowFileChooser(); 
      } 
     }); 
    } 
} 

public class mainClient { 

/** 
* @param args 
*/ 
public static void main(String[] args) { 
    // TODO Auto-generated method stub 

    clientGUI client = new clientGUI(); 
    client.setTitle("Client GUI"); 
    client.setVisible(true); 
    client.setSize(200,200); 

} 

} 
+0

Sacrée cargaison de code, Batman! – corsiKa

+1

Pour une meilleure aide plus tôt, postez un [SSCCE] (http://pscode.org/sscce.html). BTW 1) La nomenclature commune pour les noms de classes Java est 'EachWordUpperCase', les noms d'attributs Java sont' firstWordLowerCase'. Pour éviter toute confusion avec d'autres programmeurs, il est préférable de s'en tenir à la convention. 2) Mieux vaut utiliser 'System.getProperty (" line.separator ")' que '" \ n "'. 3) Le 'JFrame' créé dans la classe' filechooser' devrait probablement être un 'JDialog' modal. 4) Les interfaces graphiques doivent être créées sur l'EDT. –

Répondre

3

A JPanel ne peut pas être affiché sur lui-même, il doit être à l'intérieur d'un JFrame. Donc, faites fileChooser étendre JFrame (et ajouter ce qui est nécessaire), ou ajouter le nouveau panneau (enterFile) à votre cadre actuel (tout ce dont vous avez besoin).

3

Vous créez un nouveau JPanel mais vous ne l'ajoutez pas à un conteneur de niveau supérieur. Un JPanel doit être ajouté au JFrame de la même manière que vous ajoutez les étiquettes et les champs de texte.

Une autre option est d'avoir votre classe filechooser étendre JDialog qui a été conçu pour créer ces types de Dialogues. Êtes-vous sûr de ne pas utiliser la classe javax.swing.JFileChooser intégrée?

Questions connexes