怎样实现,Swing控件叠放???

hongming1129 2008-04-08 02:07:14
要实现控件叠放,应该怎么作呢?
比如一个JLabel放在另一个JLabel上面,或者一个JPanel放在另一个JPanel上面。然后控制他们显示,要显示哪就个显示哪个。
高分求教!!!
...全文
347 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
hongming1129 2008-04-09
  • 打赏
  • 举报
回复
happyandsad的第二个想法,跟我一致,但我试了几次,没弄出要的效果来。
经过多次尝试,搞定啦。谢谢各位。三分!
marcien 2008-04-08
  • 打赏
  • 举报
回复
呵呵,大家见了100分,就像苍蝇见了血,一窝蜂地涌了进来。:)
小宏 2008-04-08
  • 打赏
  • 举报
回复
http://blog.csdn.net/mq612/archive/2006/10/18/1339872.aspx
小宏 2008-04-08
  • 打赏
  • 举报
回复 1
可以通过LayeredPane这个容器来实现你的效果.他是一个相当于层的容器.控件之间可以相互叠加的.不影响他们的显示效果.
不知道这次合不合你意.如果要例子的话我可以给你一个
Inhibitory 2008-04-08
  • 打赏
  • 举报
回复
也许这个程序对你有点帮助, 正好以前做过实验:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class MultiPanel extends JPanel {
private static final long serialVersionUID = 1L;
private JButton changeButton1;
private JButton changeButton2;
private JButton changeButton3;
private JPanel panel1;
private JPanel panel2;
private JPanel panel3;
private JPanel currentPanel;

public MultiPanel() {
this.setLayout(new BorderLayout());

changeButton1 = new JButton("<html><font color=red>Red Panel</font></html>");
changeButton2 = new JButton("<html><font color=green>Green Panel</font></html>");
changeButton3 = new JButton("<html><font color=blue>Blue Panel</font></html>");
panel1 = new JPanel();
panel2 = new JPanel();
panel3 = new JPanel();

panel1.setBackground(Color.RED);
panel2.setBackground(Color.GREEN);
panel3.setBackground(Color.BLUE);
currentPanel = panel1;

Box box = Box.createHorizontalBox();
box.add(changeButton1);
box.add(changeButton2);
box.add(changeButton3);
this.add(box, BorderLayout.SOUTH);

this.add(currentPanel, BorderLayout.CENTER);

addButtonActionListener();
}

private void addButtonActionListener() {
changeButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
currentPanel.setVisible(false);
currentPanel = panel1;
currentPanel.setVisible(true);
add(currentPanel, BorderLayout.CENTER);
validate();
}
});

changeButton2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
currentPanel.setVisible(false);
currentPanel = panel2;
currentPanel.setVisible(true);
add(currentPanel, BorderLayout.CENTER);
validate();
}
});

changeButton3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
currentPanel.setVisible(false);
currentPanel = panel3;
currentPanel.setVisible(true);
add(currentPanel, BorderLayout.CENTER);
validate();
}
});
}

private static void createAndShowGUI() {
JFrame frame = new JFrame("Multi Panel");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 600);

MultiPanel panel = new MultiPanel();
frame.getContentPane().add(panel, BorderLayout.CENTER);

frame.setVisible(true);
}

/**
* @param args
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
MultiPanel.createAndShowGUI();
}
});
}

}
hongming1129 2008-04-08
  • 打赏
  • 举报
回复
happyandsad,我的意思不是cardlayout,cardlayout实现的叠放不是我要的效果。我要的是这样的,比如,显示一张图片,然后在图片上显示一个lable或者button之类的东西,但可以看到图片。
还是要谢谢你,happyandsad。
小宏 2008-04-08
  • 打赏
  • 举报
回复
我不知道你是不是这个意思
小宏 2008-04-08
  • 打赏
  • 举报
回复
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class CardLayoutDemo implements ActionListener{

JPanel p1,p2,p3,p4;
int i = 1;
JFrame f;

public CardLayoutDemo() {

f = new JFrame();
Container contentPane = f.getContentPane();
contentPane.setLayout(new GridLayout(2,1));

p1 = new JPanel();
Button b = new Button("Change Card");
b.addActionListener(this);
p1.add(b);
contentPane.add(p1);

p2 = new JPanel();
p2.setLayout(new FlowLayout());
p2.add(new JButton("first"));
p2.add(new JButton("second"));
p2.add(new JButton("third"));
p3 = new JPanel();
p3.setLayout(new GridLayout(3,1));
p3.add(new JButton("fourth"));
p3.add(new JButton("fifth"));
p3.add(new JButton("This is the last button"));
p4 = new JPanel();
p4.setLayout(new CardLayout());
p4.add("one",p2);
p4.add("two",p3);
((CardLayout)p4.getLayout()).show(p4,"one");

contentPane.add(p4);

f.setTitle("CardLayout");
f.pack();
f.setVisible(true);

f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}

public void actionPerformed(ActionEvent event)
{
switch(i)
{
case 1:
((CardLayout)p4.getLayout()).show(p4,"two");
break;
case 2:
((CardLayout)p4.getLayout()).show(p4,"one");
break;
}
i++;
if (i==3)
i=1;

f.validate();
}

public static void main(String args[]) {

new CardLayoutDemo();

}
}
hongming1129 2008-04-08
  • 打赏
  • 举报
回复
我是用eclipse来做的。
需要正能量 2008-04-08
  • 打赏
  • 举报
回复
楼主是用Jbuilder拖的组件吧?
应该可以自己写代码,两个组件都设置组件的x,y坐标
你要显示哪个就把它设为可见,另一个则相反
是这个意思吗?

62,623

社区成员

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

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