2017-10-03 7 views
0

Alors bonjour là :), voici mon premier post ici. Lets entrer dans le vif: Mon problème: je mets une calculatrice comme une image d'arrière-plan, que ajouté un JButton à elle. Mon problème (s):JButton s'affiche derrière l'image

Quand je lance le programme de la calculatrice est rapidement mis en évidence, il disparaît et le bouton apparaît Quand je redimensionnez la fenêtre, la calculatrice apparaît et le bouton dissapears

Comment puis-je faire ce travail?

Heres mon code:

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

public class Calculator extends JFrame { 
    private ImageIcon image; 
    private JLabel label; 


Calculator() { 


    image = new ImageIcon(getClass().getResource("TStiny.png")); 
    label = new JLabel(image); 
    add(label); 
} 

public static void main (String args[]) { 

    Calculator gui = new Calculator(); 
    gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    gui.setTitle("Texas Instruments TI-30XIIS"); 
    gui.pack(); 
    gui.setVisible(true); 

    JPanel panel = new JPanel(); 
    gui.add(panel); 

    JButton button7 = new JButton(); 
    button7.setIcon(new ImageIcon(Calculator.class.getResource("button_7.png"))); 
    button7.setVisible(true); 
    button7.setBorderPainted(false); 
    button7.setBounds(90, 445, 45, 35); 
    panel.add(button7); 

} 

Merci à l'avance! :)

Répondre

0

Vous devez spécifier la position de mise en page et éventuellement à laquelle vous voulez ajouter le composant. JFrame utilise BorderLayout par défaut.

Si vous voulez utiliser le comportement par défaut, vous devez mettre une position sur laquelle placer le composant:

public class Calculator extends JFrame { 
    private ImageIcon image; 
    private JLabel label; 


    Calculator() { 
     image = new ImageIcon(getClass().getResource("TStiny.png")); 
     label = new JLabel(image); 
     add(label, BorderLayout.LINE_START); 
    } 

    public static void main(String args[]) { 

     Calculator gui = new Calculator(); 
     gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     gui.setTitle("Texas Instruments TI-30XIIS"); 
     gui.pack(); 
     gui.setVisible(true); 

     JPanel panel = new JPanel(); 
     gui.add(panel, BorderLayout.LINE_END); 

     JButton button7 = new JButton(); 
     button7.setIcon(new ImageIcon(Calculator.class.getResource("button_7.png"))); 
     button7.setVisible(true); 
     button7.setBorderPainted(false); 
     button7.setBounds(90, 445, 45, 35); 
     panel.add(button7); 

    } 
} 

Une autre option est de spécifier une mise en page qui ne veut pas spécifier une position - comme FlowLayout:

public class Calculator extends JFrame { 
    private ImageIcon image; 
    private JLabel label; 


    Calculator() { 
     setLayout(new FlowLayout()); 

     image = new ImageIcon(getClass().getResource("TStiny.png")); 
     label = new JLabel(image); 
     add(label); 
    } 

    public static void main(String args[]) { 

     Calculator gui = new Calculator(); 
     gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     gui.setTitle("Texas Instruments TI-30XIIS"); 
     gui.pack(); 
     gui.setVisible(true); 

     JPanel panel = new JPanel(); 
     gui.add(panel); 

     JButton button7 = new JButton(); 
     button7.setIcon(new ImageIcon(Calculator.class.getResource("button_7.png"))); 
     button7.setVisible(true); 
     button7.setBorderPainted(false); 
     button7.setBounds(90, 445, 45, 35); 
     panel.add(button7); 

    } 
}