求教如何是一个qt 布局里固定大小管理器大小固定

使用边界布局管理器添加了一个标签,怎么才能设置或调整标签的大小?
--------------------------------------
将它所在的容器设置为空布局,这样你就可以在该容器内随意摆放控件了,比如JPanel p = new Jpanel();JLabel l = new JLabel("");p.setLayout(null);//设置空布局p.add(l);//添加控件l.setLocation(0,0,50,20);//设置控件相对于容器JPanel左上角的距离和控件的大小> 问题详情
容器被重新设置大小后,哪种布局管理器的容器中的组件大小不随容器大小的变化而改变?()A.CardLayo
悬赏:0&答案豆
提问人:匿名网友
发布时间:
容器被重新设置大小后,哪种布局管理器的容器中的组件大小不随容器大小的变化而改变? ()A.CardLayoutB.FlowLayoutC.BorderLayoutD.GridLavout请帮忙给出正确答案和分析,谢谢!
论文写作技巧
您可能感兴趣的试题
1算法的主运算如下,其中i的初值为1,s的初值为0,“←”为赋值号。&&while i<n do&&{ for j←1 to n do&&s←s+a[i,j]&&i←i*2;&&则该算法的时间复杂度为&&&&(&&)A.O(2n)B.O(n+log2n)C.O(nlog2n)D.O(n2)2下面关于数据结构的叙述中,正确的叙述是&&(&&)A.顺序存储方式的优点是存储密度大,且插入、删除运算效率高B.链表中的每一个结点都包含恰好一个指针C.包含n个结点的二叉排序树的最大检索长度为log2nD.将一棵树转换为二叉树后,根结点没有右子树
我有更好的答案
相关考试课程
请先输入下方的验证码查看最佳答案
图形验证:
验证码提交中……自定义圆布局管理器 - 开源中国社区
当前访客身份:游客 [
当前位置:
发布于 日 16时,
java自定义一个圆布局管理器
本篇文章转载:
代码片段(2)
1.&[图片] 自定义一个布局管理器&&&&
2.&[代码][Java]代码&&&&
///?post=189
import java.awt.BorderL
import java.awt.EventQ
import javax.swing.JB
import javax.swing.JF
import javax.swing.JP
import javax.swing.border.EmptyB
* @author k
public class CircleLayoutTest extends JFrame {
private static final long serialVersionUID = 6972580L;
private JPanel contentP
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
CircleLayoutTest frame = new CircleLayoutTest();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
* Create the frame.
public CircleLayoutTest() {
setTitle("自定义圆形布局");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
setLayout(new CircleLayout());
for (int i = 0; i & 12; i++) {
if (i & 10) {
add(new JButton("0" + i));
add(new JButton("" + i));
//-------------------------------------------------------------------------------------------------------------------------------------------
import java.awt.C
import java.awt.D
import java.awt.I
import java.awt.LayoutM
public class CircleLayout implements LayoutManager {
public void addLayoutComponent(String name, Component comp) {
public void layoutContainer(Container parent) {
double centerX = parent.getBounds().getCenterX();// 获得容器中心的X坐标
double centerY = parent.getBounds().getCenterY();// 获得容器中心的Y坐标
Insets insets = parent.getInsets();// 获得容器默认边框对象
double horizon = centerX - insets.// 获得水平可用长度的一半
double vertical = centerY - insets.// 获得垂直可用长度的一半
double radius = horizon & vertical ? vertical :// 取小的为圆形半径
int count = parent.getComponentCount();// 获得容器中控件的个数
for (int i = 0; i & i++) {// 依次设置所有可见控件的位置和大小
Component component = parent.getComponent(i);
if (component.isVisible()) {
Dimension size = component.getPreferredSize();// 大小使用其最佳大小
double angle = 2 * Math.PI * i /// 获得角度的大小
double x = centerX + radius * Math.sin(angle);// 获得圆周点的X坐标
double y = centerY - radius * Math.cos(angle);// 获得圆周点的Y坐标
component.setBounds((int) x - size.width / 2, (int) y - size.height / 2, size.width, size.height);// 重新设置控件的位置和大小
public Dimension minimumLayoutSize(Container parent) {
return parent.getMinimumSize();
public Dimension preferredLayoutSize(Container parent) {
return parent.getPreferredSize();
public void removeLayoutComponent(Component comp) {
开源中国-程序员在线工具:
开源从代码分享开始
dh_的其它代码博客访问: 331933
博文数量: 97
博客积分: 4010
博客等级: 上校
技术积分: 897
注册时间:
IT168企业级官微
微信号:IT168qiye
系统架构师大会
微信号:SACC2013
分类: Java
&&& 在使用JAVA开发图形界面时,经常会苦于控件的布局设计。最近开发一个小工具的时候,要设计出一个简单的界面,但是现有的布局管理器件不能满足需求,于是自己设计了一个,虽然说比较简单,但是个人觉得很实用。&&& 简单的说明一下该布局管理器的用法:&&& 根据控件的行号和列号进行从上到下,从左到右的布局。布局的过程中要考虑控件与控件之间的"左间距","上间距",还要考虑本身的"宽度","高度"。控制好这几个参数,就能布局出比较清晰,抑或比较复杂的用户界面。&&& 布局管理器设计草图如下:
500)this.width=500;" border=0>
中间用红线标明的几个参数"左间距","上间距","控件的高度","控件的宽度",也是几个核心的参数。当然还有控件所在的行号和列号也很重要。
其中可以看出并不是每一列都必须对的很整齐,可以调整参数实现复杂的布局。在以下示例二中可以更加具体的体现出来。
示例一截图如下:
500)this.width=500;" border=0>
LocationLayout.java源代码如下:
import java.awt.*;import javax.swing.*;/**&* 控件位置布局管理器&* @author caiqq&*&*/public class LocationLayout {&&&&/** X坐标的位置 */&&&&private int startX=25; &&&&/** Y坐标的位置 */&&&&private int startY=25; &&&&/** 上一行控件的高度 */&&&&private int lastRowHeight=50;&&&&/** 上一列控件的宽度 */&&&&private int lastColumnWidth=50;&&&&/** 上一列的编号 */&&&&private int lastRowNumber=0;&&&&/** 左间距 */&&&&static public int leftDistance=25;&&&&/** 上间距 */&&&&static public int upDistance=25;&&&&/** 控件的宽 */&&&&private int objectWidth=25;&&&&/** 控件的高 */&&&&private int objectHeight=25;&&&&&&&&/**&&&& * 默认构造函数&&&& */&&&& public LocationLayout()&&&&{&&&&&&&&&&&&}&&&& /**&&&& * 控件居中&&&& * @param component&&&& * 需要居中的控件&&&& */&&&&static public void setCenter(Component component)&&&&{&&&&&&&&int windowWidth = component.getWidth(); //获得窗口宽&&&&&&&&int windowHeight = component.getHeight(); //获得窗口高&&&&&&&&Toolkit kit = Toolkit.getDefaultToolkit(); &&&&&&&&//定义工具包&&&&&&&&Dimension screenSize = kit.getScreenSize(); &&&&&&&&//获取屏幕的尺寸&&&&&&&&int screenWidth = screenSize.width; &&&&&&&&//获取屏幕的宽&&&&&&&&int screenHeight = screenSize.height; &&&&&&&&//获取屏幕的高&&&&&&&&component.setLocation(screenWidth/2-windowWidth/2, screenHeight/2-windowHeight/2);//设置窗口居中显示&&&&}&&&&&&&&/**&&&& * 设置控件左间距&&&& * @param left&&&& * 左间距&&&&&&&& */&&&&public void setLeftDistance(int left)&&&&{&&&&&&&&leftDistance=left;&&&&&&&&}&&&&&&&&/**&&&& * 设置控件上间距&&&& * @param up&&&& * 上间距&&&&&&&& */&&&&public void setUpDistance(int up)&&&&{&&&&&&&&&&&&&&&&upDistance=up;&&&&}&&&&/**&&&& * 设置控件的大小&&&& * @param width&&&& * 控件的宽&&&& * @param height&&&& * 控件的高&&&& */&&&& public void setObjectSize(int width,int height)&&&&{&&&&&&&&objectWidth=width;&&&&&&&&objectHeight=height;&&&&}&&&/**&&&&* 根据控件的行号,列号,左边距,上边距,控件的宽,高实现控件的布局&&&&* @param object&&&&* 控件--支持各种控件&&&&* @param rowLine&&&&* 控件所在的行号&&&&* @param columnLine&&&&* 控件所在的列号&&&&* @param objectWidth&&&&* 控件的宽&&&&* @param objectHeight&&&&* 控件的高&&&&*/&&&& public void setPosition(Object object,int rowLine, int columnLine,int objectWidth,int objectHeight)&&&&{&&&&&&&&//调整Y坐标位置;&&&&&&&&if(rowLine==0)&&&&&&&&{&&&&&&&&&&&&startY=upDistance;&&&&&&&&&&&&lastRowHeight=0;&&&&&&&&}&&&&&&&&else if((lastRowNumber+1)==rowLine)//如果行号增加了,距离也随之增加&&&&&&&&{&&&&&&&&&&&&startY+=(upDistance+lastRowHeight);&&&&&&&&}&&&&&&&&&&&&&&&&&&&&//调整X坐标位置;&&&&&&&&if(columnLine==0)&&&&&&&&{&&&&&&&&&&&&startX=leftDistance;&&&&&&&&&&&&lastColumnWidth=0;&&&&&&&&}&&&&&&&&else&&&&&&&&{&&&&&&&&&&&&startX+=leftDistance+lastColumnWidth;&&&&&&&&&&&&&&&&&&&&}&&&&&&&&&&&&&&&&&&&&lastColumnWidth=objectWidth;&&&&&&&&lastRowHeight=objectHeight;&&&&&&&&&&&&&&&&lastRowNumber=rowLine;&&&&&&&&&&&&&&&&&&&&if(object==null)&&&&&&&&&&&&return;&&&&&&&&&&&&&&&&//JButton&&&&&&&&if(object.toString().startsWith("javax.swing.JButton"))&&&&&&&&{&&&&&&&&&&&&JButton JButton=(JButton)object ;&&&&&&&&&&&&JButton.setLocation(new Point(startX, startY));&&&&&&&&&&&&&&&&JButton.setSize(new Dimension(objectWidth, objectHeight));&&&&&&&&}&&&&&&&&//JLabel&&&&&&&&else if(object.toString().startsWith("javax.swing.JLabel"))&&&&&&&&{&&&&&&&&&&&&JLabel JLabel=(JLabel)object ;&&&&&&&&&&&&JLabel.setLocation(new Point(startX, startY));&&&&&&&&&&&&&&&&JLabel.setSize(new Dimension(objectWidth, objectHeight));&&&&&&&&}&&&&&&&&//JTextField&&&&&&&&else if(object.toString().startsWith("javax.swing.JTextField"))&&&&&&&&{&&&&&&&&&&&&JTextField JTextField=(JTextField)object ;&&&&&&&&&&&&JTextField.setLocation(new Point(startX, startY));&&&&&&&&&&&&&&&&JTextField.setSize(new Dimension(objectWidth, objectHeight));&&&&&&&&}&&&&&&&&//JPasswordField&&&&&&&&else if(object.toString().startsWith("javax.swing.JPasswordField"))&&&&&&&&{&&&&&&&&&&&&JPasswordField JPasswordField=(JPasswordField)object ;&&&&&&&&&&&&JPasswordField.setLocation(new Point(startX, startY));&&&&&&&&&&&&&&&&JPasswordField.setSize(new Dimension(objectWidth, objectHeight));&&&&&&&&}&&&&&&&&//JComboBox&&&&&&&&else if(object.toString().startsWith("javax.swing.JComboBox"))&&&&&&&&{&&&&&&&&&&&&JComboBox JComboBox=(JComboBox)object ;&&&&&&&&&&&&JComboBox.setLocation(new Point(startX, startY));&&&&&&&&&&&&&&&&JComboBox.setSize(new Dimension(objectWidth, objectHeight));&&&&&&&&}&&&&&&&&//JList&&&&&&&&else if(object.toString().startsWith("javax.swing.JList"))&&&&&&&&{&&&&&&&&&&&&JList JList=(JList)object ;&&&&&&&&&&&&JList.setLocation(new Point(startX, startY));&&&&&&&&&&&&&&&&JList.setSize(new Dimension(objectWidth, objectHeight));&&&&&&&&}&&&&&&&&//JRadioButton&&&&&&&&else if(object.toString().startsWith("javax.swing.JRadioButton"))&&&&&&&&{&&&&&&&&&&&&JRadioButton JRadioButton=(JRadioButton)object ;&&&&&&&&&&&&JRadioButton.setLocation(new Point(startX, startY));&&&&&&&&&&&&&&&&JRadioButton.setSize(new Dimension(objectWidth, objectHeight));&&&&&&&&}&&&&&&&&//JCheckBox&&&&&&&&else if(object.toString().startsWith("javax.swing.JCheckBox"))&&&&&&&&{&&&&&&&&&&&&JCheckBox JCheckBox=(JCheckBox)object ;&&&&&&&&&&&&JCheckBox.setLocation(new Point(startX, startY));&&&&&&&&&&&&&&&&JCheckBox.setSize(new Dimension(objectWidth, objectHeight));&&&&&&&&}&&&&&&&&//JTree&&&&&&&&else if(object.toString().startsWith("javax.swing.JTree"))&&&&&&&&{&&&&&&&&&&&&JTree JTree=(JTree)object ;&&&&&&&&&&&&JTree.setLocation(new Point(startX, startY));&&&&&&&&&&&&&&&&JTree.setSize(new Dimension(objectWidth, objectHeight));&&&&&&&&}&&&&&&&&//JTextArea&&&&&&&&else if(object.toString().startsWith("javax.swing.JTextArea"))&&&&&&&&{&&&&&&&&&&&&JTextArea JTextArea=(JTextArea)object ;&&&&&&&&&&&&JTextArea.setLocation(new Point(startX, startY));&&&&&&&&&&&&&&&&JTextArea.setSize(new Dimension(objectWidth, objectHeight));&&&&&&&&}&&&&}}
测试代码testFrame.java如下:
import javax.swing.*;public class testFrame extends JFrame {&&&&&&&&private JLabel jLabel1 = null;&&&&private JLabel jLabel2 = null;&&&&private JLabel jLabel3 = null;&&&&private JLabel jLabel4 = null;&&&&private JLabel jLabel5 = null;&&&&private JComboBox dataBaseTypeBox = null;&&&&&&&&private JTextField urlTextField = null;&&&&private JTextField dataBaseTextField = null;&&&&private JTextField accountTextField = null;&&&&private JPasswordField PasswordField = null;&&&&private JButton testConnButton = null;&&&&private JPanel jContentPane = null; &&&&public testFrame()&&&&{&&&&&&&&setTitle("布局管理器");&&&&&&&&&&&&setSize(320, 350);&&&&&&&&initialize();&&&&&&&&setLayout();&&&&&&&&&&&&&&&&setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);&&&&}&&&&/**&&&& * 初始化控件&&&& */&&&&private void initialize() {&&&&&&&&&&&&&&&&jLabel1 = new JLabel();&&&&&&&&jLabel1.setText("数据库类型:");&&&&&&&&jLabel2 = new JLabel();&&&&&&&&jLabel2.setText("数据库URL:");&&&&&&&&jLabel3 = new JLabel();&&&&&&&&jLabel3.setText("数据库名:");&&&&&&&&jLabel4 = new JLabel();&&&&&&&&jLabel4.setText("帐户:");&&&&&&&&jLabel5 = new JLabel();&&&&&&&&jLabel5.setText("密码:");&&&&&&&&dataBaseTypeBox=new JComboBox();&&&&&&&&dataBaseTypeBox.addItem("Oracle");&&&&&&&&dataBaseTypeBox.addItem("DB2");&&&&&&&&urlTextField=new JTextField();&&&&&&&&dataBaseTextField=new JTextField();&&&&&&&&accountTextField=new JTextField();&&&&&&&&PasswordField=new JPasswordField();&&&&&&&&testConnButton=new JButton();&&&&&&&&testConnButton.setText("测试连接");&&&&&&&&jContentPane = new JPanel();&&&&&&&&jContentPane.setLayout(null);&&&&&&&&jContentPane.add(dataBaseTypeBox);&&&&&&&&jContentPane.add(urlTextField);&&&&&&&&jContentPane.add(dataBaseTextField);&&&&&&&&jContentPane.add(accountTextField);&&&&&&&&jContentPane.add(PasswordField);&&&&&&&&jContentPane.add(testConnButton);&&&&&&&&jContentPane.add(jLabel1);&&&&&&&&jContentPane.add(jLabel2);&&&&&&&&jContentPane.add(jLabel3);&&&&&&&&jContentPane.add(jLabel4);&&&&&&&&jContentPane.add(jLabel5);&&&&&&&&&&&&&&&&setContentPane(jContentPane);&&&&}&&&&/**&&&& * 显示窗口&&&& */&&&&private void setLayout()&&&&{&&&&&&&&LocationLayout locationLayout=new LocationLayout();&&&&&&&&//窗口居中&&&&&&&&locationLayout.setCenter(this);&&&&&&&&//使用默认的左边距25,上边距25&&&&&&&&//第0行控件&&&&&&&&&&&&&&&&locationLayout.setPosition(jLabel1, 0, 0,70, 25);&&&&&&&&locationLayout.setPosition(dataBaseTypeBox, 0, 1, 150, 25);&&&&&&&&//第1行控件&&&&&&&&locationLayout.setPosition(jLabel2, 1, 0, 70, 25);&&&&&&&&locationLayout.setPosition(urlTextField, 1, 1,150, 25);&&&&&&&&//第2行控件&&&&&&&&locationLayout.setPosition(jLabel3, 2, 0,70, 25);&&&&&&&&locationLayout.setPosition(dataBaseTextField, 2, 1, 150, 25);&&&&&&&&&&&&//第3行控件&&&&&&&&locationLayout.setPosition(jLabel4, 3, 0, 70, 25);&&&&&&&&locationLayout.setPosition(accountTextField, 3, 1, 150, 25);&&&&&&&&//第4行控件&&&&&&&&locationLayout.setPosition(jLabel5, 4, 0, 70, 25);&&&&&&&&locationLayout.setPosition(PasswordField, 4, 1, 150, 25);&&&&&&&&//调整左边距为120&&&&&&&&//第5行控件&&&&&&&&locationLayout.setLeftDistance(120);&&&&&&&&locationLayout.setPosition(testConnButton, 5, 0, 90, 25);&&&&&&&&&&&&&&&&setVisible(true);&&&&}&&&&public static void main(String[] arg)&&&&{&&&&&&&&new testFrame();&&&&}}
代码分析:
& 仔细分析测试代码中的setLayout()方法,可以看出:& 1、使用本布局管理器,首先得设置默认的布局管理器为"null",即jContentPane.setLayout(null);& &2、在标明控件名行号和列号时,一定要一行一行的递增,一列一列的递增,并且第一行和第一列都是从"0"开始。
示例二截图如下:
这是我做数据导入小工具时的图形界面,相对来说比上个界面要复杂些,但也是用的这个布局管理器完成的。
500)this.width=500;" border=0>
源代码和文档.rar
如果有兴趣可以与我交流:QQ:
阅读(2793) | 评论(1) | 转发(0) |
相关热门文章
给主人留下些什么吧!~~
KK娱乐视频网,快乐齐分享
搞笑视频,动漫视频,美女写真,靓丽车模,美女翻唱,精彩MV,经典DV
请登录后评论。哪个布局管理器使用的是组件的最佳尺寸 ( ) A.FlowLayoutB.Border_答案_百度高考
哪个布局管理器使用的是组件的最佳尺寸
A.FlowLayoutB.BorderLayoutC.GridLayoutD.CardLayout
第-1小题正确答案及相关解析}

我要回帖

更多关于 布局管理器 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信