简体   繁体   English

JButton,带有背景图像的JFrame

[英]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. 我正在尝试用Java在JFRame中使用背景图像,但是如果您单击按钮,它将出现在带有消息的新窗口中,但是我想在背景上有一些JButton。 But, the buttons doesn't appear in the JFrame. 但是,按钮没有出现在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. 设置布局,然后用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). 请注意,只有当图像大于您添加到标签中的组件的大小时,此方法才有效(否则,组件的绘制将被截断)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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