简体   繁体   English

具有png背景的Java面板

[英]java panel with png background

i found this link.. LINK what i want is there's a JPanel that has a background and another JPanel with half the size of the first JPanel but with an image that is transparent and with a face or a ball at the middle.. :) just like the screenshot from the link.. is that possible to code in java? 我发现这个链接.. LINK我想是有,有一个背景,并与第一JPanel的一半大小,但与透明,并用脸或者在中间的球的图像的另一个JPanel的一个JPanel .. :)就像链接中的屏幕截图一样。是否可以用Java编写代码? :) im just thinking it like for web programming. :)我只是认为它像Web编程一样。 just a sort of DIV's to have that but i dont know in java.. :) sorry for bad english.. :D i have this as a background.. 只是一种DIV有,但是我在Java中不知道.. :)对不起,英语不好..:D我有这个作为背景..

package waterKing;

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

 @SuppressWarnings("serial")

 public class Main extends JFrame {
MainData data = new MainData();

public static void main(String[] args) {
    Main frmMain = new Main();
    frmMain.setExtendedState(Frame.MAXIMIZED_BOTH);
    frmMain.setVisible(true);

}

public Main() {
    data.tk = getToolkit();
    data.d = data.tk.getScreenSize();

    data.jP = new JPanel() {            
        protected void paintComponent(Graphics g) {
            data.e = getSize();
            data.iI = new ImageIcon("images/mainBG.png").getImage();
            g.drawImage(data.iI,0, 0, data.d.width, data.d.height, null);
            super.paintComponent(g);    
        }                   
    };

    data.jP.setOpaque(false);
    data.jSp = new JScrollPane(data.jP);
    data.jB = new JButton("EXIT");
    data.jB.setBounds(10,10,200,40);
    data.jB.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    });
    data.jP.setLayout(null);
    data.jP.add(data.jB);       

    this.setTitle("Water King Inventory System");
    this.setUndecorated(true);
    this.getContentPane();
    this.add(data.jSp);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setLocationRelativeTo(null);

}

 }

i dont know how to add another JPanel to show in the middle with this background 我不知道如何添加另一个JPanel在此背景下显示在中间 替代文字

i dont know how to add another JPanel to show in the middle with this background 我不知道如何添加另一个JPanel在此背景下显示在中间

Its just like adding components to a panel. 就像将组件添加到面板中一样。 You need to use a layout manager and then the component will be positioned properly based on the rules of the layout manager. 您需要使用布局管理器,然后根据布局管理器的规则正确放置组件。 In your case you can set the layout manager of the background panel to be a BorderLayout. 您可以将背景面板的布局管理器设置为BorderLayout。 Then you can add a JLabel with the appropriate Icon to the center of the BorderLayout. 然后,您可以将带有适当图标的JLabel添加到BorderLayout的中心。

You will need to set the preferred size (or override the getPreferredSize() method of your panel since you add it to a scroll pane. Scrollbars will only appear when the preferred size of the panel is greater than the size of the scroll pane. 您需要设置首选大小(或覆盖面板的getPreferredSize()方法,因为您将其添加到滚动窗格中。滚动条仅在首选大小大于滚动窗格的大小时显示。

You should not be reading the image in your paintComponent() method since this method is called multiple times. 您不应在paintComponent()方法中读取图像,因为该方法已被多次调用。

You should not be using the "screen size" to determine the width/height of the image because the frame will contain a border. 您不应该使用“屏幕尺寸”来确定图像的宽度/高度,因为框架将包含边框。 You need to use the size of the panel. 您需要使用面板的尺寸。

Get rid of all the setBounds() code. 摆脱所有setBounds()代码。 Learn to use layout managers. 学习使用布局管理器。

For a general purpose background panel that takes into account most of the suggestions made here check out Background Panel . 对于考虑了此处大部分建议的通用背景面板,请查看“ 背景面板”

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

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