怎样实现,Swing控件叠放??? 要实现控件叠放,应该怎么作呢?比如一个JLabel放在另一个JLabel上面,或者一个JPanel放在另一个JPanel上面。然后控制他们显示,要显示哪就个显示哪个。高分求教!!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 楼主是用Jbuilder拖的组件吧?应该可以自己写代码,两个组件都设置组件的x,y坐标你要显示哪个就把它设为可见,另一个则相反是这个意思吗? 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(); }} happyandsad,我的意思不是cardlayout,cardlayout实现的叠放不是我要的效果。我要的是这样的,比如,显示一张图片,然后在图片上显示一个lable或者button之类的东西,但可以看到图片。还是要谢谢你,happyandsad。 也许这个程序对你有点帮助, 正好以前做过实验: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(); } }); }} 可以通过LayeredPane这个容器来实现你的效果.他是一个相当于层的容器.控件之间可以相互叠加的.不影响他们的显示效果.不知道这次合不合你意.如果要例子的话我可以给你一个 http://blog.csdn.net/mq612/archive/2006/10/18/1339872.aspx happyandsad的第二个想法,跟我一致,但我试了几次,没弄出要的效果来。经过多次尝试,搞定啦。谢谢各位。三分! 请教下java C/S聊天室几个问题~~ 请问为什么我这个类无法运行。。。老是说main的void类型不可用 JAVA中 引用的内存(栈中的)大小是多少 SAX更改xml的内容 怎样让包含JTable的JScrollPane占据整个JPanel EAR是什么? 关于jtabbedpane的切换问题 写一个Liunx 下的java 程序,怎样才能使kill pid的方法杀不死他呢? 基础:操作符的问题,请指教!! 请问在JLIST的渲染器单元中可不以监听鼠标事件,200分 求一个排列组合的算法! 关于创建对像以自身作为参数的问题
应该可以自己写代码,两个组件都设置组件的x,y坐标
你要显示哪个就把它设为可见,另一个则相反
是这个意思吗?
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();
}
}
还是要谢谢你,happyandsad。
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();
}
});
}}
不知道这次合不合你意.如果要例子的话我可以给你一个
经过多次尝试,搞定啦。谢谢各位。三分!