在一个JPanel中包含有另外两个JPanel组件,一个A显示背景,另一个B显示其他一些组件,B的范围小于A,且覆盖A的中间一部分,可行吗?请问如何作?多谢了!
------解决方案--------------------
- Java code
class MyPanel extends Panel{ void go(){ this.setLayout(new BorderLayout()); Panel p1=new Panel();//背景 Panel p2=new Panel(); this.add(p1); p1.setLayout(new GridLayout(3,3)); p1.add(p2,5); }}
------解决方案--------------------
class MyPanel extends Panel{
void go(){
this.setLayout(new BorderLayout());
Panel p1=new Panel();//背景
Panel p2=new Panel();
this.add(p1);
p1.setLayout(new GridLayout(3,3));
p1.add(p2,5);
}
}
------解决方案--------------------
两个panel的载体都是同一个Panel
- Java code
import java.awt.Color;import java.awt.Container;import java.awt.Dimension;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;public class SubPanel extends JFrame { private JPanel backPanel = new JPanel(); private JPanel forePanel = new JPanel(); public SubPanel() { Container contentPane = this.getContentPane(); contentPane.add(backPanel); backPanel.setSize(300, 300); backPanel.setBackground(Color.BLUE); forePanel.setPreferredSize(new Dimension(50, 50)); forePanel.setSize(new Dimension(50, 50)); JButton button = new JButton("JButton"); button.setBackground(Color.RED); button.setForeground(Color.YELLOW); forePanel.add(button); contentPane.add(forePanel); forePanel.setOpaque(false); contentPane.setComponentZOrder(forePanel, 0); } /** * @param args */ public static void main(String[] args) { SubPanel sp = new SubPanel(); sp.setVisible(true); sp.setSize(300, 300); sp.setDefaultCloseOperation(EXIT_ON_CLOSE); }}
------解决方案--------------------
- Java code
JPanel jPanel1=new JPanel();JPanel jPanelA=new JPanel();JPanel jPanelB=new JPanel();JButon jButton1=new JButton();JButon jButton2=new JButton();JLabel jLabel1=new JLabel();JLabel jLabel2=new JLabel();jPanelB.add(jLabel1);jPanelB.add(jLabel2);jPanelB.add(jButton1);jPanelB.add(jButton2);jPanel1.add(jPanelA);jPanelA.add(jPanelB);//再设置一下各JPanel组件的Layout