概述
package my;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Rectangle;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
public class MyFrame extends JFrame
{
public MyFrame(String title)
{
super(title);
// 不使用布局器
(默认地,ContentPane自带一个BorderLayout布局器)
Container root = this.getContentPane();
root.setLayout(null); // 取消其布局器
// 由于没有布局器,所以默认地,子控件无法显示
JLabel a1 = new ColorfulLabel("1", Color.yellow);
JLabel a2 = new ColorfulLabel("2", Color.blue);
root.add(a1);
root.add(a2);
a1.setBounds(new Rectangle(0,0, 200,200));
a2.setBounds(new Rectangle(100,100, 200,100));
}
// ColorfulLabel: 参考4.5节的讲解
private static class ColorfulLabel extends JLabel
{
public ColorfulLabel(String text, Color bgColor)
{
super(text);
setOpaque(true);
setBackground(bgColor);
setPreferredSize(new Dimension(60,30));
setHorizontalAlignment(SwingConstants.CENTER);
setFont(new Font("微软雅黑", Font.PLAIN, 20) );
}
}
}
创建布局器
package my;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.LayoutManager;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
public class MyFrame extends JFrame
{
JLabel a1 = new ColorfulLabel("Hello,World", Color.yellow);
JLabel a2 = new ColorfulLabel("样例文本", new Color(202,255,112));
public MyFrame(String title)
{
super(title);
// 根容器
Container root = this.getContentPane();
// 设置一个自定义的布局器
LayoutManager layoutMgr = new SimpleLayout();
root.setLayout(layoutMgr);
// 由于没有布局器,所以默认地,子控件无法显示
root.add(a1);
root.add(a2);
}
// ColorfulLabel: 参考4.5节的讲解
private static class ColorfulLabel extends JLabel
{
public ColorfulLabel(String text, Color bgColor)
{
super(text);
setOpaque(true);
setBackground(bgColor);
//setPreferredSize(new Dimension(60,30));
setHorizontalAlignment(SwingConstants.CENTER);
setFont(new Font("宋体", Font.PLAIN, 16));
}
}
private class SimpleLayout implements LayoutManager
{
@Override
public void addLayoutComponent(String name, Component comp)
{
}
@Override
public void removeLayoutComponent(Component comp)
{
}
@Override
public Dimension preferredLayoutSize(Container parent)
{
return null;
}
@Override
public Dimension minimumLayoutSize(Container parent)
{
return null;
}
@Override
public void layoutContainer(Container parent)
{
int w = parent.getWidth(); // 父窗口的宽度 width
int h = parent.getHeight(); // 父窗口的高度 height
System.out.println("父容器: " + w + ", " + h);
// a1 显示在中间, 以 Preferred Size 作为大小
if(a1.isVisible())
{
// 得取该控件所需的显示尺寸
Dimension size = a1.getPreferredSize();
//System.out.println(size);
int x = (w - size.width) / 2;
int y = (h - size.height) /2 ;
// 在设置子控件位置时,其坐标是相对于父窗口的
// (0,0) 就是父窗口的左上角
a1.setBounds(x, y, size.width, size.height);
}
// a2 显示在右上角
if(a2.isVisible())
{
Dimension size = a2.getPreferredSize();
//System.out.println(size);
int x = (w - size.width);
int y = 0;
a2.setBounds(x, y, size.width, size.height);
}
}
}
}
手动布局
package my;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.LayoutManager;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import af.swing.layout.AfSimpleLayout;
public class MyFrame extends JFrame
{
JLabel a1 = new ColorfulLabel("Hello,World", Color.yellow);
JLabel a2 = new ColorfulLabel("样例文本", new Color(202,255,112));
public MyFrame(String title)
{
super(title);
// 根容器
Container root = this.getContentPane();
// 设置一个自定义的布局器
LayoutManager layoutMgr = new MyCustomLayout();
root.setLayout(layoutMgr);
// 由于没有布局器,所以默认地,子控件无法显示
root.add(a1);
root.add(a2);
}
// ColorfulLabel: 参考4.5节的讲解
private static class ColorfulLabel extends JLabel
{
public ColorfulLabel(String text, Color bgColor)
{
super(text);
setOpaque(true);
setBackground(bgColor);
//setPreferredSize(new Dimension(60,30));
setHorizontalAlignment(SwingConstants.CENTER);
setFont(new Font("宋体", Font.PLAIN, 16));
}
}
private class MyCustomLayout extends AfSimpleLayout
{
@Override
public void layoutContainer(Container parent)
{
int w = parent.getWidth(); // 父窗口的宽度 width
int h = parent.getHeight(); // 父窗口的高度 height
System.out.println("父容器: " + w + ", " + h);
// a1 显示在中间, 以 Preferred Size 作为大小
if(a1.isVisible())
{
// 得取该控件所需的显示尺寸
Dimension size = a1.getPreferredSize();
//System.out.println(size);
int x = (w - size.width) / 2;
int y = (h - size.height) /2 ;
// 在设置子控件位置时,其坐标是相对于父窗口的
// (0,0) 就是父窗口的左上角
a1.setBounds(x, y, size.width, size.height);
}
// a2 显示在右上角
if(a2.isVisible())
{
Dimension size = a2.getPreferredSize();
//System.out.println(size);
int x = (w - size.width);
int y = 0;
a2.setBounds(x, y, size.width, size.height);
}
}
}
}
package af.swing.layout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.LayoutManager;
public abstract class AfSimpleLayout implements LayoutManager
{
@Override
public void addLayoutComponent(String name, Component comp)
{
}
@Override
public void removeLayoutComponent(Component comp)
{
}
@Override
public Dimension preferredLayoutSize(Container parent)
{
return null;
}
@Override
public Dimension minimumLayoutSize(Container parent)
{
return null;
}
// @Override
// public void layoutContainer(Container parent)
// {
// }
}
线性布局
package my;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.LayoutManager;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import af.swing.layout.AfXLayout;
public class MyFrame extends JFrame
{
JLabel a1 = new ColorfulLabel("Hello,World", Color.yellow);
JLabel a2 = new ColorfulLabel("样例文本", Color.blue);
JLabel a3 = new ColorfulLabel("Good Boy", Color.CYAN);
JLabel a4 = new ColorfulLabel("占满剩余", Color.red);
public MyFrame(String title)
{
super(title);
// 根容器
JPanel root = new JPanel();
this.setContentPane(root);
// 设置为横向布局
// 注: AfXLayout 与 AfRowLayout等效
root.setLayout(new AfXLayout(8)); // 子控件之间的间距为8像素
root.add(a1, "100px"); // 固定占100像素
root.add(a2, "30%");
// 固定占30%
root.add(a3, "auto"); // 按 Preferred Size 设置
root.add(a4, "1w");
// 按权重设置,权重为1 weight
}
// ColorfulLabel: 参考4.5节的讲解
private static class ColorfulLabel extends JLabel
{
public ColorfulLabel(String text, Color bgColor)
{
super(text);
setOpaque(true);
setBackground(bgColor);
//setPreferredSize(new Dimension(60,30));
setHorizontalAlignment(SwingConstants.CENTER);
setFont(new Font("宋体", Font.PLAIN, 16));
}
}
}
package af.swing.layout;
/* 横向布局器,和 AfRowLayout 等效
*
*/
public class AfXLayout extends AfRowLayout
{
public AfXLayout()
{
}
public AfXLayout(int gap)
{
super(gap);
}
public AfXLayout(int gap, boolean usePerferredSize)
{
super(gap, usePerferredSize);
}
}
自由位置布局
package my;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import af.swing.layout.AfAnyWhere;
import af.swing.layout.AfMargin;
public class MyFrame extends JFrame
{
JLabel a1 = new ColorfulLabel("Hello,World", Color.yellow);
JLabel a2 = new ColorfulLabel("样例文本", Color.blue);
JLabel a3 = new ColorfulLabel("Good Boy", Color.CYAN);
JLabel a4 = new ColorfulLabel("占满剩余", Color.red);
public MyFrame(String title)
{
super(title);
// 根容器
JPanel root = new JPanel();
this.setContentPane(root);
// 设置为自动位置布局
root.setLayout(new AfAnyWhere());
// 左上 ,相当于 new AfMargin(0,0,-1,-1)
root.add(a1, AfMargin.TOP_LEFT);
// 右上 ,相当于 new AfMargin(0,-1,-1,0)
root.add(a2, AfMargin.TOP_RIGHT);
// 中间 ,相当于 new AfMargin(-1, -1, -1, -1)
root.add(a3, AfMargin.CENTER); // 中间
a3.setPreferredSize(new Dimension(160,80));
// 自由位置定义
root.add(a4, new AfMargin(-1, 15, 20, 15));
a4.setPreferredSize(new Dimension(0,40));
}
// ColorfulLabel: 参考4.5节的讲解
private static class ColorfulLabel extends JLabel
{
public ColorfulLabel(String text, Color bgColor)
{
super(text);
setOpaque(true);
setBackground(bgColor);
//setPreferredSize(new Dimension(60,30));
setHorizontalAlignment(SwingConstants.CENTER);
setFont(new Font("宋体", Font.PLAIN, 16));
}
}
}
package af.swing.layout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.LayoutManager2;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/** 自由位置布局,一般应于JLayeredPane联用
*
*
只有在LayeredPane里才有真正的分层显示效果
*
*/
public class AfAnyWhere implements LayoutManager2
{
private static class Item
{
Component comp;
AfMargin margin;
}
List<Item> itemList = new ArrayList<>();
@Override
public void addLayoutComponent(String name, Component comp)
{
Item item = new Item();
item.comp = comp;
item.margin = new AfMargin(-1);
itemList.add( item );
}
@Override
public void addLayoutComponent(Component comp, Object constraints)
{
Item item = new Item();
item.comp = comp;
if( constraints instanceof AfMargin)
{
item.margin = (AfMargin)constraints;
}
else
{
System.out.println("AfLayerLayout: 布局参数须为AfMargin类型!");
item.margin = new AfMargin(-1); // 居中显示
}
itemList.add( item );
}
@Override
public void removeLayoutComponent(Component comp)
{
Iterator<Item> iter = itemList.iterator();
while(iter.hasNext())
{
Item item = iter.next();
if(comp == item.comp)
{
iter.remove();
break;
}
}
}
@Override
public void layoutContainer(Container parent)
{
Insets insets = parent.getInsets();
if(insets == null)
insets = new Insets(0,0,0,0);
int x = insets.left;
int y = insets.top;
int w = parent.getWidth() - insets.left - insets.right;
int h = parent.getHeight() - insets.top - insets.bottom;
for(int i=0; i<itemList.size(); i++)
{
Item item = itemList.get(i);
Component comp = item.comp;
AfMargin margin = item.margin;
if(! comp.isVisible()) continue;
Dimension preferedSize = comp.getPreferredSize();
int[] hh = getPosition(margin.left, margin.right, w, preferedSize.width);
int[] vv = getPosition(margin.top, margin.bottom, h, preferedSize.height);
Rectangle bounds = new Rectangle(hh[0]+x, vv[0]+y, hh[1]-hh[0], vv[1]-vv[0]);
comp.setBounds( bounds );
//System.out.println("** AfAnyWhere: " + i + ": " + bounds);
}
}
private int[] getPosition(int startMargin, int endMargin, int size, int preferedSize)
{
int[] values = {0,0};
if(startMargin <0 && endMargin <0)
{
// 居中显示
values[0] = (size-preferedSize)/2;
values[1] = values[0] + preferedSize;
}
else if(startMargin<0)
{
// 靠右 或者 靠下
values[0] = size - endMargin - preferedSize;
values[1] = size - endMargin;
}
else if(endMargin <0)
{
// 靠左 或者 靠上
values[0] = startMargin;
values[1] = startMargin + preferedSize;
}
else
{
// 两端对齐
values[0] = startMargin;
values[1] = size - endMargin;
}
return values;
}
//
@Override
public float getLayoutAlignmentX(Container target)
{
return 0;
}
@Override
public float getLayoutAlignmentY(Container target)
{
return 0;
}
@Override
public void invalidateLayout(Container target)
{
// TODO Auto-generated method stub
}
@Override
public Dimension preferredLayoutSize(Container parent)
{
return new Dimension(30,30);
}
@Override
public Dimension minimumLayoutSize(Container parent)
{
return new Dimension(30,30);
}
@Override
public Dimension maximumLayoutSize(Container target)
{
return new Dimension(9999,9999);
}
}
package af.swing.layout;
public class AfMargin
{
// 按照 Swing 的排列顺序,应该是 上 左 下 右
public int top, left, bottom, right;
// 9个常见的位置
public static final AfMargin FULL = new AfMargin(0, 0, 0,0);
public static final AfMargin TOP_LEFT = new AfMargin(0, 0, -1, -1);
public static final AfMargin TOP_CENTER = new AfMargin(0, -1, -1, -1);
public static final AfMargin TOP_RIGHT = new AfMargin(0, -1, -1, 0);
public static final AfMargin CENTER_LEFT = new AfMargin(-1, 0, -1, -1);
public static final AfMargin CENTER = new AfMargin(-1, -1, -1, -1);
public static final AfMargin CENTER_RIGHT = new AfMargin(-1, -1, -1, 0);
public static final AfMargin BOTTOM_LEFT = new AfMargin(-1, 0, 0, -1);
public static final AfMargin BOTTOM_CENTER = new AfMargin(-1, -1, 0, -1);
public static final AfMargin BOTTOM_RIGHT = new AfMargin(-1, -1, 0, 0);
public AfMargin()
{
}
public AfMargin(int value)
{
top = left = bottom = right;
}
public AfMargin(int hValue, int vValue)
{
left = right = hValue;
top = bottom = vValue;
}
public AfMargin(int top, int left, int bottom, int right)
{
this.top = top;
this.left = left;
this.bottom = bottom;
this.right = right;
}
}
(练习)
package my;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import af.swing.layout.AfXLayout;
public class MyFrame extends JFrame
{
ColorfulLabel a1 = new ColorfulLabel("。。。", Color.YELLOW);
ColorfulLabel a2 = new ColorfulLabel(">>>", Color.GREEN);
JButton sendButton = new JButton("发送");
public MyFrame(String title)
{
super(title);
// 根容器
JPanel root = new JPanel();
this.setContentPane(root);
// 总体上使用 BorderLayout
root.setLayout(new BorderLayout());
// 中间加一个控件
root.add(a1, BorderLayout.CENTER);
// 上面是一个组合
if( true )
{
// JPanel面板,其实就是一个容器
JPanel p = new JPanel();
p.setLayout(new AfXLayout(4));
p.add(a2,"1w"); // 权重为1,水平方向占满剩余空间
p.add(sendButton, "80px"); // 水平方向固定80像素宽度
root.add(p,
BorderLayout.PAGE_START);
}
}
// ColorfulLabel: 参考4.5节的讲解
private static class ColorfulLabel extends JLabel
{
public ColorfulLabel(String text, Color bgColor)
{
super(text);
setOpaque(true);
setBackground(bgColor);
//setPreferredSize(new Dimension(60,30));
setHorizontalAlignment(SwingConstants.CENTER);
setFont(new Font("宋体", Font.PLAIN, 16));
}
}
}
package af.swing.layout;
/* 横向布局器,和 AfRowLayout 等效
*
*/
public class AfXLayout extends AfRowLayout
{
public AfXLayout()
{
}
public AfXLayout(int gap)
{
super(gap);
}
public AfXLayout(int gap, boolean usePerferredSize)
{
super(gap, usePerferredSize);
}
}
最后
以上就是痴情白云为你收集整理的swing入门篇 6.自定义布局的全部内容,希望文章能够帮你解决swing入门篇 6.自定义布局所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复