2017-03-15 6 views
0

JFrame bleed through JInternalFrameJFrame saigne par JInternalFrame

Je crée une application de bureau et je ne peux pas comprendre pourquoi le JFrame saigne à travers le JInternalFrame (voir photo). J'ai essayé d'inclure le code approprié ci-dessous donc s'il vous plaît ne tenez pas compte {}, il fonctionne sans erreurs dans l'application réelle.

public class MainJFrame extends JFrame { 

private static final long serialVersionUID = 2377310559170663631L; 
Container cPane; 
JDesktopPane desktopPane = new JDesktopPane(); 
JPanel deskPanel; 
JButton jButtonCreateWLog, jButtonCreateCLog; 



public static void main(String args[]) { 
    MainJFrame w = new MainJFrame(); 
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 
    int maxX = screenSize.width - 100; 
    int maxY = screenSize.height - 200; 
    w.setPreferredSize(new Dimension(maxX, maxY)); 
    w.setSize(maxX, maxY); 
    w.setTitle("Estes Fitness Help"); 
    w.setVisible(true); 
} 

public MainJFrame() { 
    super("Estes Fitness Help"); 
    getContentPane().add(desktopPane); 
    cPane = getContentPane(); 
    cPane.setLayout(null); 
    initiateComponents(); 
    initiateMenuBar(); 
    setDefaultCloseOperation(EXIT_ON_CLOSE); 
} 
private void initiateComponents(){ 

    //Set size to match desktop size 
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 
    int maxX = screenSize.width - 100; 
    int maxY = screenSize.height - 200; 

    //Create panel inside desktop 
    deskPanel = new JPanel(); 
    deskPanel.setName("Weight Log Entry"); 
    deskPanel.setLayout(null); 
    deskPanel.setBackground(new Color(200, 50, 50)); 

    //button for creating a weight log 
    jButtonCreateWLog = new JButton(); 
    jButtonCreateWLog.setText("Create Weight Log"); 
    jButtonCreateWLog.setBounds(80, 120, 150, 25); 
    jButtonCreateWLog.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent e) { 
     jButtonCreateWLogActionPerformed(e); 
     } 
    }); 

    //button for creating a cardio log 
    jButtonCreateCLog = new JButton(); 
    jButtonCreateCLog.setText("Create Cardio Log"); 
    jButtonCreateCLog.setBounds(250, 120, 150, 25); 
    jButtonCreateCLog.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent e) { 
     jButtonCreateCLogActionPerformed(e); 
     } 
    }); 

    //add components to deskPanel 
    deskPanel.add(jButtonCreateWLog); 
    deskPanel.add(jButtonCreateCLog); 
    deskPanel.setSize(maxX, maxY); 
    cPane.add(deskPanel); 

    pack(); 
    setVisible(true); 

} 

private void jButtonCreateWLogActionPerformed(ActionEvent e){ 
    MainJFrameController desktopPaneC = new MainJFrameController(this); 
    desktopPaneC.actionPerformed(e); 

} 

C'est le MainJFrameController:

public class MainJFrameController implements ActionListener{ 

private MainJFrame desktopFrame; 

public MainJFrameController(){ 

} 

public MainJFrameController(MainJFrame desktopFrame){ 
    this.desktopFrame = desktopFrame; 
    desktopFrame.getCreateWLogButton().addActionListener(this); 
    desktopFrame.getCreateCLogButton().addActionListener(this); 
} 

public void actionPerformed(ActionEvent e) { 
    String action = e.getActionCommand(); 
    if (action.equalsIgnoreCase("Create Weight Log")){ 
     getCreateWLogButton_actionPerformed(e); 
    } else if (action.equalsIgnoreCase("Create Cardio Log")){ 
     getCreateCLogButton_actionPerformed(e); 
    } 
} 

private void getCreateWLogButton_actionPerformed(ActionEvent e) { 
    StoreWeightLogJFrame SWLJFrame = new StoreWeightLogJFrame(); 
    SWLJFrame.pack(); 
    SWLJFrame.setVisible(true); 
    desktopFrame.add(SWLJFrame); 

} 
} 

C'est le WeightLog JInternalFrame

public class StoreWeightLogJFrame extends JInternalFrame{ 

private static final long serialVersionUID = 7750452209025354283L; 

JPanel jPanel1; 
JLabel jLabelSets, jLabelReps, jLabelWeight; 
JTextField jTextFieldSets, jTextFieldReps, jTextFieldWeight; 
JButton jButtonSaveWLog; 

public StoreWeightLogJFrame() { 
    super("Store Weight Log"); 
    initiateComponents(); 
    this.add(jPanel1); 
    this.setBounds(0, 0, 505, 505); 
    this.setVisible(true); 
    this.pack(); 
    this.setClosable(true); 
    this.setDefaultCloseOperation(DISPOSE_ON_CLOSE); 
    System.out.println("StoreWeightLogJFrame initialized"); 
} 
private void initiateComponents(){ 

    jPanel1 = new JPanel(); 
    jLabelSets = new JLabel(); 
    jTextFieldSets = new JTextField(); 
    jLabelReps = new JLabel(); 
    jTextFieldReps = new JTextField(); 
    jLabelWeight = new JLabel(); 
    jTextFieldWeight = new JTextField(); 
    jButtonSaveWLog = new JButton(); 
    jPanel1.setName("Weight Log Entry"); 
    jPanel1.setLayout(null); 
    jPanel1.setBackground(new Color(255, 50, 50)); 

    jLabelSets.setText("Sets: "); 
    jLabelSets.setBounds(25, 25, 100, 50); 
    jTextFieldSets.setText("3"); 
    jTextFieldSets.setBounds(80, 40, 25, 25); 


    jLabelReps.setText("Reps: "); 
    jLabelReps.setBounds(25, 60, 100, 50); 
    jTextFieldReps.setText("10"); 
    jTextFieldReps.setBounds(80, 75, 25, 25); 

    jLabelWeight.setText("Weight: "); 
    jLabelWeight.setBounds(25, 110, 50, 25); 
    jTextFieldWeight.setText("225"); 
    jTextFieldWeight.setBounds(80, 110, 30, 25); 

    jButtonSaveWLog.setText("Save Log"); 
    jButtonSaveWLog.setBounds(40, 150, 100, 25); 
    jButtonSaveWLog.addActionListener(new java.awt.event.ActionListener() { 
    public void actionPerformed(ActionEvent e) { 
     jButtonSaveWLogActionPerformed(e); 
    } 
    }); 

    jPanel1.add(jLabelSets); 
    jPanel1.add(jTextFieldSets); 
    jPanel1.add(jLabelReps); 
    jPanel1.add(jTextFieldReps); 
    jPanel1.add(jLabelWeight); 
    jPanel1.add(jTextFieldWeight); 
    jPanel1.add(jButtonSaveWLog); 
    jPanel1.setBounds(5, 5, 500, 550); 

} 
+0

Il semble que vous ayez des problèmes de commande en ligne. – MadProgrammer

Répondre

0
getContentPane().add(desktopPane); 
cPane = getContentPane(); 

D'abord, vous ajoutez le volet de bureau dans le volet de contenu.

cPane.add(deskPanel); 

Ensuite, vous ajoutez le deskPanel au volet de contenu. Comme vous utilisez une disposition nulle, vous avez ajouté deux composants au même conteneur et le bouton s'affichera lorsque la souris passera la souris dessus.

Je ne sais pas exactement ce que vous essayez de faire, mais je suppose que vous devriez:

  1. pas utiliser une mise en page nulle. Swing a été conçu pour être utilisé avec le gestionnaire de disposition
  2. pour afficher un «panneau de boutons» et le «panneau de bureau» sur le volet de contenu en utilisant un gestionnaire de disposition approprié (BorderLayout) pour contrôler l'emplacement de chaque composant.

Donc, la logique de base est quelque chose comme:

JDesktopPane desktop = new JDesktopPane(); 
frame.add(desktop, BorderLayout.CENTER); 

Maintenant, si vous voulez que vos « boutons » apparaissent sur l'image que vous pouvez créer un autre panneau pour vos boutons:

JPanel buttonPanel = new JPanel(); 
buttonPanel.add(theFirstButton); 
buttonPanel.add(theSecondButton); 
frame.add(buttonPanel, BorderLayout.PAGE_START); 

Maintenant, le cadre contiendra un panneau avec les boutons en haut du cadre et le panneau de bureau sera situé sous ce panneau.

Lire la section du tutoriel Swing sur Layout Managers pour plus d'informations et des exemples de travail.

+0

Je vais vérifier votre lien une fois de retour et de voir si je peux le faire fonctionner. –