Java GUI JLabel 和 JPanel 图片 和组件重叠问题

package ui;

import java.awt.AWTEvent;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class MainUi extends JPanel implements ActionListener
{
 protected JTextField id, psw;
 protected JButton ok, register, exit;
 protected JLabel idl, pswl, imgl;
 protected static JLabel imgpanel;
 protected ImageIcon image;

 public MainUi()
 {
  enableEvents(AWTEvent.WINDOW_EVENT_MASK);
  image = new ImageIcon("D://java/PZ/image/bg.jpg");
  imgpanel = new JLabel(image);
  

  id = new JTextField(60);
  id.setActionCommand("ID");
  idl = new JLabel("ID :");
  idl.setLabelFor(id);

  psw = new JTextField(60);
  psw.setActionCommand("password");

  pswl = new JLabel("PassWord: ");
  pswl.setLabelFor(psw);

  ok = new JButton("OK");
  register = new JButton("REGISTER");
  exit = new JButton("EXIT");

  JPanel login = new JPanel();
  GridBagLayout gridbag = new GridBagLayout();
  GridBagConstraints c = new GridBagConstraints();
  login.setLayout(gridbag);

//  imgl.setBounds(0, 0, imgIcon.getIconWidth(), imgIcon.getIconHeight());
//  login.setOpaque(false);

  JLabel[] labels = { idl, pswl };
  JTextField[] textFields = { id, psw };
  addLabelTextRows(labels, textFields, gridbag, login);

  c.gridwidth = GridBagConstraints.REMAINDER; // last
  c.anchor = GridBagConstraints.WEST;
  c.weightx = 1.0;

  login.setBorder(BorderFactory.createCompoundBorder(BorderFactory
    .createTitledBorder("Text Fields"), BorderFactory
    .createEmptyBorder(5, 5, 5, 5)));
  

  add(login, BorderLayout.CENTER);

 }

 private void addLabelTextRows(JLabel[] labels, JTextField[] textFields,
   GridBagLayout gridbag, Container container)
 {
  GridBagConstraints c = new GridBagConstraints();
  c.anchor = GridBagConstraints.EAST;
  int numLabels = labels.length;

  for (int i = 0; i < numLabels; i++)
  {
   c.gridwidth = GridBagConstraints.RELATIVE; // next-to-last
   c.fill = GridBagConstraints.NONE; // reset to defau
   container.add(labels[i], c);

   c.gridwidth = GridBagConstraints.REMAINDER; // end row
   c.fill = GridBagConstraints.HORIZONTAL;
   c.weightx = 1.0;
   container.add(textFields[i], c);
  }
  
 }

 @Override
 public void actionPerformed(ActionEvent arg0)
 {

 }

 public static void createGUI()
 {
  JFrame frame = new JFrame("Plants and Zombies");
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
  frame.add(new MainUi());
  //frame.getContentPane().add(imgpanel);
  frame.getLayeredPane().add(imgpanel, new Integer(Integer.MIN_VALUE));
  
  frame.pack();
  
  frame.setSize(800, 600);
  frame.setVisible(true);
 }

 public static void main(String[] args)
 {
  SwingUtilities.invokeLater(new Runnable()
  {
   public void run()
   {
    UIManager.put("swing.boldMetal", Boolean.FALSE);
    createGUI();
   }
  });
 }
}
这里为什么图片不能显示??????

 

有知道的高手回答下。。 谢谢

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值