简体   繁体   中英

JButton, JFrame with Background image

I'm trying to do in Java a JFRame with a background image, but also I want to have some JButtons "over" the background if you click the button it appears a new window with a message. But, the buttons doesn't appear in the JFrame.

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

public class Prova3 extends JFrame implements ActionListener {//implementando el listener de eventos

JButton bt1, bt2, bt3;//creando variables globales de los botones
JFrame jf = new JFrame("BATTLESHIP");//creacion de ventana con el titulo

public Prova3(){//constructor de la clase
    //setVisible(true);

    //jf.setLayout(new FlowLayout());//Configurar como se dispondra el espacio del jframe
    jf.setLayout(new FlowLayout());
    jf.setContentPane(new JLabel(new ImageIcon("imageprova.jpg")));

    Dimension d = new Dimension();//objeto para obtener el ancho de la pantalla

    //imagen para el boton


    //Instanciando botones con texto
    bt1 = new JButton("HELLO");
    bt2 = new JButton("SIZE");


    //margenes para texto en boton
    bt1.setMargin(new Insets(3, 5, 3, 5));
    bt2.setMargin(new Insets(1, 1, 1, 1));

    //color de fondo del boton
    bt1.setBackground(Color.orange);

    //color de texto para el boton
    bt2.setForeground(Color.blue);

    //agregando los botones a la ventana
    jf.add(bt1); jf.add(bt2);


    //añadiendo el listener a los botones para manipular los eventos del click
    bt1.addActionListener(this);
    bt2.addActionListener(this);
   // bt3.addActionListener(this);

    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//finaliza el programa cuando se da click en la X        
    jf.setResizable(false);//para configurar si se redimensiona la ventana
    jf.setLocation((int) ((d.getWidth()/2)+290), 50);//para ubicar inicialmente donde se muestra la ventana (x, y)
    jf.setSize(680, 346);//configurando tamaño de la ventana (ancho, alto)
    jf.setVisible(true);//configurando visualización de la venta
}

public static void main(String[] args) {

    Prova3 gj = new Prova3();//uso de constructor para la ventana
}

@Override
public void actionPerformed(ActionEvent e) {//sobreescribimos el metodo del listener

    if(e.getActionCommand().equals("HELLO")){//podemos comparar por el contenido del boton
        JOptionPane.showMessageDialog(null, e.getActionCommand());
    }
    if(e.getSource()==bt2){//podemos comparar por el nombre del objeto del boton
        jf.setExtendedState(JFrame.MAXIMIZED_BOTH);//cambiamos el tamaño de la ventana
    }
}
jf.setLayout(new FlowLayout());
jf.setContentPane(new JLabel(new ImageIcon("imageprova.jpg")));

You set the layout, but then you replace the content pane with a JLabel. A label does not have a default layout manager.

Your code should be:

//jf.setLayout(new FlowLayout());
JLabel background = new JLabel(new ImageIcon("imageprova.jpg"));
background.setLayout( new FlowLayout() );
jf.setContentPane( background );

Now you can add components to the label.

Note this approach will only work if the image is greater than the size of the components you add to the label (otherwise the painting of the components will be truncated).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM