简体   繁体   中英

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? :) im just thinking it like for web programming. 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..

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 替代文字

i dont know how to add another JPanel to show in the middle with this background

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. Then you can add a JLabel with the appropriate Icon to the center of the 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.

You should not be reading the image in your paintComponent() method since this method is called multiple times.

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. 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 .

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