Java GUI 题。。。。 JPanel和JLabel中重叠问题

cheohanmee 2010-04-17 08:32:31
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();
}
});
}
}

这里怎样把图片作为背景显示出来?
现在图片不能显示。。 图片出来了其他组件就不显示。。

求高手帮忙。。谢谢哦~~
...全文
909 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
qjtttt 2010-04-18
  • 打赏
  • 举报
回复
frame.setContentPane(mainUi);
qjtttt 2010-04-18
  • 打赏
  • 举报
回复
楼上UP

可以不用重写paintComponent,太久不玩swing了,呵呵


public static void createGUI()
{
JFrame frame = new JFrame("Plants and Zombies");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

image = new ImageIcon("D://java/PZ/image/bg.jpg");
imgpanel = new JLabel(image);


frame.getLayeredPane().add(imgpanel, new Integer(Integer.MIN_VALUE));
imgpanel.setBounds(0,0,image.getIconWidth(),image.getIconHeight()); //这里不要漏掉
MainUi mainUi=new MainUi();
frame.setContentPane(mainUi);

((JPanel)frame.getContentPane()).setOpaque(false);

//frame.pack(); 既然下面设置了尺寸干嘛还要pack一下?
frame.setSize(800, 600);
frame.setVisible(true);


}
IT_xinxiu 2010-04-18
  • 打赏
  • 举报
回复
继续发

import java.awt.FlowLayout;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class TestBackgroundColor extends JFrame
{
private JPanel imagePanel;
private ImageIcon background;
public TestBackgroundColor(){
background = new ImageIcon("D://My Documents//图//谭健新//111.jpg");//背景图片
JLabel label = new JLabel(background);//把背景图片显示在一个标签里面
//把标签的大小位置设置为图片刚好填充整个面板
label.setBounds(0,0,background.getIconWidth(),background.getIconHeight());
//把内容窗格转化为JPanel,否则不能用方法setOpaque()来使内容窗格透明
imagePanel = (JPanel)this.getContentPane();
imagePanel.setOpaque(false);
//内容窗格默认的布局管理器为BorderLayout
imagePanel.setLayout(new FlowLayout());
imagePanel.add(new JButton("测试按钮"));
this.getLayeredPane().setLayout(null);
//把背景图片添加到分层窗格的最底层作为背景
this.getLayeredPane().add(label,new Integer(Integer.MIN_VALUE));
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(background.getIconWidth(),background.getIconHeight());
this.setVisible(true);
}
public static void main(String[] args)
{
// TODO Auto-generated method stub
TestBackgroundColor tbc = new TestBackgroundColor();
//tbc.setVisible(true);
}
}
IT_xinxiu 2010-04-18
  • 打赏
  • 举报
回复
不好意思,代码就这样黏贴上去,复制后会有很多空格,导致错误,下面从新给你发一遍
import java.awt.FlowLayout;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class TestBackgroundColor extends JFrame
{
private JPanel imagePanel;
private ImageIcon background;
public TestBackgroundColor(){
background = new ImageIcon("D://My Documents//图//谭健新//111.jpg");//背景图片
JLabel label = new JLabel(background);//把背景图片显示在一个标签里面
//把标签的大小位置设置为图片刚好填充整个面板
label.setBounds(0,0,background.getIconWidth(),background.getIconHeight());
//把内容窗格转化为JPanel,否则不能用方法setOpaque()来使内容窗格透明
imagePanel = (JPanel)this.getContentPane();
imagePanel.setOpaque(false);
//内容窗格默认的布局管理器为BorderLayout
imagePanel.setLayout(new FlowLayout());
imagePanel.add(new JButton("测试按钮"));
this.getLayeredPane().setLayout(null);
//把背景图片添加到分层窗格的最底层作为背景
this.getLayeredPane().add(label,new Integer(Integer.MIN_VALUE));
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(background.getIconWidth(),background.getIconHeight());
this.setVisible(true);
}
public static void main(String[] args)
{
// TODO Auto-generated method stub
TestBackgroundColor tbc = new TestBackgroundColor();
//tbc.setVisible(true);
}
}
IT_xinxiu 2010-04-18
  • 打赏
  • 举报
回复
怎么为Java程序添加漂亮背景图片代码,通过网络搜集整理后可执行代码如下,建议以后学任何一项技术时,不要让代码夹杂着太多其它的东西:
import Java.awt.*;
import Javax.swing.*;
public class TestBackgroundColor extends JFrame
{
  public static void main(String[] args)
  {
    // TODO Auto-generated method stub
    TestBackgroundColor tbc = new TestBackgroundColor();
    tbc.setVisible(true);
  }
  private JPanel imagePanel;
  private ImageIcon background;
  public TestBackgroundColor()
  {
    background = new ImageIcon("渐变背景14.png");//背景图片
    JLabel label = new JLabel(background);//把背景图片显示在一个标签里面
    //把标签的大小位置设置为图片刚好填充整个面板
    label.setBounds(0,0,background.getIconWidth(),background.getIconHeight());
    //把内容窗格转化为JPanel,否则不能用方法setOpaque()来使内容窗格透明
    imagePanel = (JPanel)this.getContentPane();
    imagePanel.setOpaque(false);
    //内容窗格默认的布局管理器为BorderLayout
    imagePanel.setLayout(new FlowLayout());
    imagePanel.add(new JButton("测试按钮"));
    this.getLayeredPane().setLayout(null);
    //把背景图片添加到分层窗格的最底层作为背景
    this.getLayeredPane().add(label,new Integer(Integer.MIN_VALUE));
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize(background.getIconWidth(),background.getIconHeight());
    this.setVisible(true);
  }
}
qjtttt 2010-04-18
  • 打赏
  • 举报
回复
接分。。。。
cheohanmee 2010-04-18
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 qjtttt 的回复:]
楼上UP

可以不用重写paintComponent,太久不玩swing了,呵呵


Java code

public static void createGUI()
{
JFrame frame = new JFrame("Plants and Zombies");
frame.setDefaultCloseOperation……
[/Quote]

谢谢哦~~

解决问题了。。回复晚 不好意思哦~
qjtttt 2010-04-18
  • 打赏
  • 举报
回复
刚才没注意看你的代码,弄错了,呵呵
重写一下JPanel的paintComponent就好了,另外你下面createGUI

import java.awt.AWTEvent;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
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;

/**
* 因为原来你原来就是实例变量而且不是private修饰的
* 所以我没有改成createGUI方法里的局部变量
* 因为下面要用所以image加了static修饰
*/
protected static JLabel imgpanel;
protected static ImageIcon image;

public MainUi()
{
enableEvents(AWTEvent.WINDOW_EVENT_MASK);


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

}

/**
* 这里重写了JPanel的paintComponent方法,目的是把图片画上去
*/
protected void paintComponent(Graphics g) {
if (ui != null) {
Graphics scratchGraphics = (g == null) ? null : g.create();
try {
ui.update(scratchGraphics, imgpanel);
}
finally {
scratchGraphics.dispose();
}
}
}

public static void createGUI()
{
JFrame frame = new JFrame("Plants and Zombies");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

image = new ImageIcon("D://java/PZ/image/bg.jpg");
imgpanel = new JLabel(image);


frame.getLayeredPane().add(imgpanel, new Integer(Integer.MIN_VALUE));
imgpanel.setBounds(0,0,image.getIconWidth(),image.getIconHeight()); //这里不要漏掉
MainUi mainUi=new MainUi();
frame.getContentPane().add(mainUi);
((JPanel)frame.getContentPane()).setOpaque(false);

//frame.pack(); 既然下面设置了尺寸干嘛还要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();
}
});
}
}

pywepe 2010-04-18
  • 打赏
  • 举报
回复
如今用mm图片做头像的人 大都是男人

希望你是女人

哈哈
汉唐斥候 2010-04-17
  • 打赏
  • 举报
回复
注意所在图片的路径和名称。
汉唐斥候 2010-04-17
  • 打赏
  • 举报
回复
我来说一下吧。用图片做背景是个很好的设想,但是图片不可以直接放在JFrame上。那我们怎么办呢?
我们应该以一个JPanel作为背景,然后把图片回到那上边。
下面是一个示例代码,仅供参考,希望对你有用处。

public class ChessPanel extends JPanel{//继承了JPanel

public void paintComponent(Graphics g){
super.paintComponent(g);
Toolkit kit=Toolkit.getDefaultToolkit();
Image img=kit.getImage("棋盘.jpg");//得到图片
g.drawImage(img, 0,0, null);//往JPanel上绘制图片
repaint();

}
当然你需要在Main方法中生成一个这样的对象。

大体就是这样,去试一下吧。祝你成功~~~~
cheohanmee 2010-04-17
  • 打赏
  • 举报
回复
加了login.setOpaque(false);
为什么不管用啊。。。。Y.Y
cheohanmee 2010-04-17
  • 打赏
  • 举报
回复
谢谢哦。。不过这个不是我想要的结果。。

我想把图片作为背景图,其他文本框和按钮都放在图片上。(不是南北分开的)
lonefeifei 2010-04-17
  • 打赏
  • 举报
回复
还有那个图片路径你改过来,
lonefeifei 2010-04-17
  • 打赏
  • 举报
回复
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 static JPanel login;//这里改成static 类中可使用
public MainUi()
{
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
image = new ImageIcon("D:\\A.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");

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.setLayout(new BorderLayout());
frame.add(new mainUi());
frame.getContentPane().add(imgpanel,BorderLayout.NORTH);
frame.getContentPane().add(login,BorderLayout.SOUTH);//在这加login
// 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();
}
});
}
}
cheohanmee 2010-04-17
  • 打赏
  • 举报
回复
这里要用什么布局呢??
我学的不好。。请谅解
qjtttt 2010-04-17
  • 打赏
  • 举报
回复
add(login, BorderLayout.CENTER);
你自己用的布局就不对,当然覆盖了,你在最后添加了一个panel用BorderLayout,怎么会不覆盖??
cheohanmee 2010-04-17
  • 打赏
  • 举报
回复
。。。 没有人知道么?

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧