简体   繁体   English

JButton在图像后面显示

[英]JButton is displaying behind image

So hello there :), this is my fist post here. 您好:),这是我的拳头贴。 Lets get right into it: My problem: I put a calculator as a background image, than added a JButton to it. 让我们开始吧:我的问题:我把计算器作为背景图像,而不是添加了JButton。 My Problem(s): 我的问题:

When I start the program the calculator is quickly shown, then it vanishes and the button is shown When I resize the window, the calculator shows up and the button dissapears 当我启动程序时,快速显示计算器,然后它消失并且显示按钮。当我调整窗口大小时,计算器显示出来,并且按钮消失

How can I make this work? 我该如何进行这项工作?

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

}

Thanks in advance! 提前致谢! :) :)

You need to specify the layout and eventual position to which you want to add the component. 您需要指定要向其中添加组件的布局和最终位置。 JFrame uses BorderLayout by default. JFrame默认使用BorderLayout

If you want to use the default behavior, you should put a position on which to place the component: 如果要使用默认行为,则应放置一个放置组件的位置:

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

    }
}

Another option is to specify a layout that does not want to specify a position - such as FlowLayout : 另一个选择是指定一个不想指定位置的布局,例如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);

    }
}

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

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