常用的:
边界布局(borderlayout)
流式布局(flowlayout)
网格布局(gridlayout)
权重网格布局(GridBagLayout)
盒子布局(boxlayout),
都是在一个范围内,划出片块,每人分1块使用,如同七巧板。
其它:
ScrollPaneLayout,JScrollPane运用的布局管理器
ViewportLayout,JViewport运用的布局管理器
层叠相关:
CardLayout卡片布局,如同一摞扑克牌,默认只显示最上层的一张。
OverlayLayout默认层叠,比较绕。https://wenku.baidu.com/view/04d10b65757f5acfa1c7aa00b52acfc789eb9fa3.html
空布局,一个容器使用空布局时,其添加的控件会叠加。先加入的在最上,会遮挡后加入的。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.ImageIcon; import javax.swing.JPanel; import javax.swing.WindowConstants; import javax.swing.BorderFactory; import java.awt.BorderLayout; import java.awt.CardLayout; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.AlphaComposite; import java.awt.Color; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class NullLayoutPanel extends JFrame { public NullLayoutPanel() { JButton btn = new JButton("顶层的透明JPanel"); btn.setBounds(0, 0, 120, 120); JPanel panel_1 = new JPanel(); panel_1.setBorder(BorderFactory.createLineBorder(Color.RED)); // panel_1.setSize(200, 180); panel_1.setBounds(0, 200, 240, 200); panel_1.setOpaque(false); // 背景透明 panel_1.add(btn); JLabel label_2 = new JLabel(); label_2.setSize(400, 300); label_2.setIcon(new ImageIcon("/Users/moonmen/Desktop/图标/color_purple.png")); JPanel panel_2 = new JPanel(); panel_2.setLayout(null); // 必要的空布局 panel_2.setBounds(0, 0, 2400, 300); panel_2.setBorder(BorderFactory.createLineBorder(Color.GREEN)); panel_2.add(panel_1); // 空布局时后加入的被先加入的遮挡 panel_2.add(label_2); add(panel_2); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setSize(500, 450); // x,y,w,h setVisible(true); } public static void main(String[] args) { new AlphaPanel(); } } |
JLayeredPane层级面板,为容器添加了深度,允许组件在需要时互相重叠。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
import java.awt.*; import javax.swing.*; public class JLayeredPaneTest extends JFrame { public JLayeredPaneTest(){ JLayeredPane jLayeredPane = new JLayeredPane(); for (int c = 0; c < 10; c++) { JLabel jlb = new JLabel("控件-" + c, JLabel.CENTER); jlb.setPreferredSize(new Dimension(100, 100)); jlb.setBackground(new Color((int) (Math.random() * 256 * 256 * 256))); jlb.setBorder(BorderFactory.createLineBorder(Color.GREEN)); jlb.setBounds(50 * c, 25 * c, 100, 50); // 在无layout时起作用, 有layout时无作用 // jlb.setPreferredSize(new Dimension(100, 100)); // 没有layout则不起作用, 有layout时起作用 jlb.setOpaque(true); // 设为不透明。JLayeredPane中默认是透明 // jLayeredPane.add(jlb); // jLayeredPane.setLayer(jlb, c); // 层级。数字越大越顶层 jLayeredPane.add(jlb, Integer.valueOf(c)); // 如果想在add时直接设置层, 要将int转为Integer } add(jLayeredPane); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setSize(500, 450); setVisible(true); } public static void main(String[] arguments) { new JLayeredPaneTest(); } } |
上面2个示例代码中,顶层透明通常是有效的。但当底层使用了Canvas则“透明”会无效,可以结合“玻璃”实现透明。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
import java.awt.*; import javax.swing.*; public class JLayeredPaneTest extends JFrame { public JLayeredPaneTest(){ JLabel label_1 = new JLabel("工具条"); JButton btn = new JButton("顶层的透明JPanel"); JPanel panel_1 = new JPanel(); // 工具条,设为“玻璃”可透视底部Canvas panel_1.setLayout(new FlowLayout(FlowLayout.LEFT)); panel_1.setBorder(BorderFactory.createLineBorder(Color.RED)); panel_1.setBounds(30, 80, 240, 100); // 玻璃模式,无效了 panel_1.setOpaque(false); // 背景透明。但不能“透视”底部自定义的Canvas panel_1.add(label_1); panel_1.add(btn); JLabel label_2 = new JLabel("视频播放器"); label_2.setSize(100, 30); label_2.setIcon(new ImageIcon("/Users/moonmen/Desktop/图标/color_purple.png")); VideoPanel panel_2 = new VideoPanel(); // 视频播放器。内部自定义Canvas绘制,会使顶层面板透明无效 panel_2.setBounds(0, 0, 450, 300); panel_2.setBorder(BorderFactory.createLineBorder(Color.GREEN)); panel_2.init(); panel_2.playFrame(); panel_2.add(label_2); JLayeredPane jLayeredPane = new JLayeredPane(); jLayeredPane.add(panel_2, Integer.valueOf(1)); jLayeredPane.add(panel_1, Integer.valueOf(2)); // 数字越大越顶层 add(jLayeredPane); setGlassPane(panel_1); // 设成透明玻璃,子控件也不可见了 panel_1.setVisible(true); // 子控件可见了 setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setSize(500, 450); setVisible(true); } public static void main(String[] arguments) { new JLayeredPaneTest(); } } |
- end
声明
本文由崔维友 威格灵 cuiweiyou vigiles cuiweiyou 原创,转载请注明出处:http://www.gaohaiyan.com/4140.html
承接App定制、企业web站点、办公系统软件 设计开发,外包项目,毕设