【问题标题】:JButton, JFrame with Background imageJButton,带背景图像的 JFrame
【发布时间】:2015-04-21 15:31:43
【问题描述】:

我正在尝试在 Java 中使用背景图像制作 JFRame,但如果您单击按钮,我希望在背景上“覆盖”一些 JButton,它会出现一个带有消息的新窗口。但是,这些按钮不会出现在 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
    }
}

【问题讨论】:

    标签: java swing jframe jbutton


    【解决方案1】:
    jf.setLayout(new FlowLayout());
    jf.setContentPane(new JLabel(new ImageIcon("imageprova.jpg")));
    

    您设置了布局,但随后将内容窗格替换为 JLabel。标签没有默认布局管理器。

    你的代码应该是:

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

    现在您可以向标签添加组件了。

    请注意,此方法仅在图像大于您添加到标签的组件的大小时才有效(否则组件的绘制将被截断)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-04
      • 2014-01-13
      • 2011-02-08
      • 2015-07-04
      相关资源
      最近更新 更多