2017-10-03 10 views
0

こんにちは、そこに:)、これは私の拳のポストです。 それに入ることができます: 私の問題: 私は、バックグラウンドイメージとして、それにJButtonを追加しました。 私の問題(秒):私は電卓を迅速に示されているプログラムを起動すると、それが消えると、私は、ウィンドウのサイズを変更すると、ボタンが 示されJButtonが画像の背後に表示されています

、電卓は

どう

を示しており、ボタン。消えます私はこの仕事をすることができますか?

HERESに私のコード:事前に

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); 

} 

ありがとう! :)

答えて

0

コンポーネントを追加するレイアウトと最終的な位置を指定する必要があります。 JFrameはデフォルトでBorderLayoutを使用します。

あなたはデフォルトの動作を使用する場合は、コンポーネントを配置する上での位置を置く必要があります。

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); 

    } 
} 

別のオプションの位置を指定したくないレイアウトを指定している - など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); 

    } 
} 
関連する問題