swing布局方式有哪些(java swing gui)

swing布局方式有哪些(java swing gui)

扫码添加渲大师小管家,免费领取渲染插件、素材、模型、教程合集大礼包!

大家好,今天来介绍swing布局方式有哪些(java中的swing)的问题,以下是渲大师小编对此问题的归纳和整理,感兴趣的来一起看看吧!

java中swing的布局

当谨穗配族坦然可以。
首先布局要设为空布局setLayout(null);
然后就可以具体布置控件祥指了,有两种方法:
1:label.setBounds(int x,int y,int width,int height)
2:label.setSize(int width, int height);
label.setLocation(int x,int y);

Java的swing的布局那方面的知识

边框布数御坦局:package swing_BorderLayout;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class swing3 extends JFrame implements ActionListener
{
JPanel jp=new JPanel();
JButton jb1=new JButton("东");
JButton jb2=new JButton("南");
JButton jb3=new JButton("西");
JButton jb4=new JButton("北");
JButton jb5=new JButton("中");
public swing3()
{
this.setTitle("创建边框布局");
jp.setLayout(new BorderLayout(5,5));
//jp.setLayout(new BorderLayout());//默薯桐认
//jp.setLayout(new BorderLayout(int Hgap,int Vgap));
jb1.setMnemonic('d');
jb1.addActionListener(this);
jb2.setMnemonic('s');
jb2.addActionListener(this);
jb3.setMnemonic('a');
jb3.addActionListener(this);
jb4.setMnemonic('w');
jb4.addActionListener(this);
jb5.setMnemonic('q');
jb5.addActionListener(this);
jp.add(jb1,BorderLayout.EAST);
jp.add(jb2,BorderLayout.SOUTH);
jp.add(jb3,BorderLayout.WEST);
jp.add(jb4,BorderLayout.NORTH);
jp.add(jb5,BorderLayout.CENTER);
this.add(jp);
this.setBounds(300,250,300,200);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==jb1)
{
jb5.setText("EAST");
}
else if(e.getSource()==jb2)
{
jb5.setText("SOUTH");
}
else if(e.getSource()==jb3)
{
jb5.setText("WEST");
}
else if(e.getSource()==jb4)
{
jb5.setText("NORTH");
}
else if(e.getSource()==jb5)
{
jb5.setText("CENTER");
}
}
}
public class C
{
public static void main(String[] args)
{
swing3 sb=new swing3();
}
}卡片布局:package swing_CardLayout;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class swing5 extends JFrame implements ActionListener
{
JPanel jp=new JPanel();
CardLayout cl=new CardLayout();//卡片布局需要通过类来创建
//CardLayout cl=new CardLayout();
//CardLayout cl=new CardLayout(int 控件和容器边界水平距离, int 控件和拆缺容器边界垂直距离, int Hgap, int Vgap);
JButton jbf=new JButton("第一个");
JButton jbl=new JButton("最后一个");
JButton jbn=new JButton("下一个");
JButton jbp=new JButton("上一个");
JButton jbc=new JButton("中间那个");
public swing5()
{
this.setTitle("创建卡片布局");
this.setLayout(null);//窗口的布局设为空布局
jbf.setBounds(120, 40, 100, 20);
jbl.setBounds(120, 70, 100, 20);
jbn.setBounds(120, 100, 100, 20);
jbp.setBounds(120, 130, 100, 20);
jbc.setBounds(120, 160, 100, 20);
this.add(jbf);
this.add(jbl);
this.add(jbn);
this.add(jbp);
this.add(jbc);
jbf.addActionListener(this);
jbl.addActionListener(this);
jbn.addActionListener(this);
jbp.addActionListener(this);
jbc.addActionListener(this);
jp.setBounds(10,40,100,100);
jp.setLayout(cl);
for(int i=0; i<50; i++)
{
JButton jb=new JButton("按钮"+i);
jp.add(jb, ""+i);
}
this.add(jp);
this.setBounds(200, 200, 300, 220);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==jbf)
{
cl.first(jp);
}
else if(e.getSource()==jbl)
{
cl.last(jp);
}
else if(e.getSource()==jbn)
{
cl.next(jp);
}
else if(e.getSource()==jbp)
{
cl.previous(jp);
}
else if(e.getSource()==jbc)
{
cl.show(jp,"25");
}
}
}
public class E
{
public static void main(String[] args)
{
swing5 sc=new swing5();
}}流布局:package swing_FlowLayout;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
class swing1 extends JFrame implements ActionListener
{
int i=0;
JPanel jp=new JPanel();
JButton jb=new JButton("创建按钮");
public swing1()
{
this.setTitle("创建流布局管理器");
jp.setLayout(new FlowLayout(1,5,5));
//this.setLayout(new FlowLayout());//默认为居中对齐,水平和垂直间距为5
//this.setLayout(new FlowLayout((int)指定对齐方式));
//this.setLayout(new FlowLayout((int)指定对齐方式,(int)控件间水平间距,(int)控件间垂直间距));
jb.setMnemonic('a');
jp.add(jb);
jb.addActionListener(this);
jp.setToolTipText("提示而已");
this.add(jp);
this.setBounds(300,250,300,200);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
++i;
JButton jbi=new JButton("按钮"+i);
jp.add(jbi);
this.show(true);
}
}
public class A
{

public static void main(String[] args)
{
swing1 sf=new swing1();
}
}网格布局:package swing_GridLayout;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class swing2 extends JFrame
{

JPanel jp=new JPanel();
JButton jb=new JButton("创建按钮");
int i=0;
public swing2()
{
jp.setLayout(new GridLayout(3,2));//注意!!!是面板设置布局流
//jp.setLayout(new GridLayout());//默认行默认列
//jp.setLayout(new GridLayout(int Rows, int Columns));//设置行和列,常用
//jp.setLayout(new GridLayout(int Rows, int Columns, int Hgap, int Vgap));//设置行和列,控件间水平和垂直间距
jb.setMnemonic('a');
jb.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
++i;
JButton jbi=new JButton("按钮"+i);
jp.add(jbi);
swing2.this.show(true);//注意!!!是窗口中的对象刷新
}
});
jp.add(jb);
this.add(jp);
this.setTitle("创建网格布局管理器");
this.setBounds(300,250,300,200);
this.setVisible(true);
}
}
public class B
{
public static void main(String[] args)
{
swing2 sg=new swing2();
}}空布局:package swing_null_Layout;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class swing4 extends JFrame
{
JPanel jp=new JPanel();
JButton jb1=new JButton("动");
JLabel jl=new JLabel("标签");
int i=10;
int j=10;
public swing4()
{
this.setTitle("空布局管理器");
jp.setLayout(null);
jb1.setBounds(50, 50, 70, 20);
jl.setBounds(150, 70, 100, 20);
jb1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
i+=10;
j+=5;
jb1.setBounds(50, 50, 70+i, 20+j);
jl.setBounds((150+i), (70+j), 100, 20);//空布局中的控件可以改变大小
}
});
jp.add(jb1);
jp.add(jl);
this.add(jp);
this.setBounds(300, 250, 300, 200);
this.setVisible(true);
}
}
public class D
{
public static void main(String[] args)
{
swing4 sn=new swing4();
}} 当初我学Java也是把布局一个一个写过的~~所以就复制给你了~~你也试试自己写吧~~可以加深记忆的~~加油吧~~

swing控件如何调整布局

  • 如果你的父窗体JFrame或者槐春橡JDialog是setResizable(false),绝对布局是可以的。publicvoidsetBounds(intx,inty,intwidth,intheight)这个方法的参数表达的含义要弄明白,x、y是相对于其父组件的x和y坐标,其他两个分森冲别代表了宽和高。
    无论是不是设置了setResizable(false),推荐使用GridBagLayout或者GroupLayout,它们可以很好的实现布局。
    有些ide可以自动生成代码,并且可以查看“设铅旁计时”和“运行时”的效果(design/view),比如netbeans和安装了swingdesigner的eclipse。

java swing布局

null layout不是很好,所有的东西都要自己定义,一旦窗口大小变化就要重新计算。我建议使用MigLayout。上手慢,但很强大。http://www.miglayout.com

而且,就算你要型橘世用内建的Layout Manager,还有一个GridBag Layout,比Grid Layout更强大一点。

接下来,那个边框,不是JSeparator, 而是一个边框。我猜是

Border result = BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), "修改信息: ");

如果是我,使用MigLayout,以上代码还是比较简单的:

importjava.awt.event.KeyEvent;

importjavax.swing.BorderFactory;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JPanel;
importjavax.swing.JTextField;
importjavax.swing.SwingUtilities;
importjavax.swing.border.Border;
importjavax.swing.border.EtchedBorder;

importnet.miginfocom.swing.MigLayout;

publicclassMyFrameextendsJFrame{
publicMyFrame(){
begin();
}

privatevoidbegin(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(newMigLayout("insets10,fill","[]","[]5[]"));

//firstpanel
JPanelfirst=newJPanel();

//first这个panel有5像素的边缘,2列,3行。第一列右对齐,占40%宽卜肢度;第二列默认,左对齐,占据剩余所有空间。
first.setLayout(newMigLayout("insets5,fill","[right,40%]5[fill,grow]","[]5[]5[]"));
first.setOpaque(false);
//这个面板的border有些特殊:createTitledBorder()方法的签名可以有两个:前一个是线的类型,后面一个是标题文本。
伍亮Borderresult=BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED),"修改信息:");
first.setBorder(result);
//把first加到contentPane里面
add(first,"cell00,grow");

JLabeloriginal=newJLabel("输入原密码:");
JLabelnewPass=newJLabel("输入新密码:");
JLabelconfirm=newJLabel("确认新密码:");

JTextFieldorig_field=newJTextField();
JTextFieldnew_field=newJTextField();
JTextFieldconfirm_field=newJTextField();

//miglayout的核心就是网格排布。用坐标来定义添加元素的位置
first.add(original,"cell00,w100!");//addtocol0,line0,min:pref:maxwidthallsetto100
first.add(newPass,"cell01,w100!");//addtocol0,line1
first.add(confirm,"cell02,w100!");//addtocol0,line2

first.add(orig_field,"cell10,w150!");//addtocol1,line0,min:pref:maxwidthallsetto150
first.add(new_field,"cell11,w150!");//addtocol1,line1
first.add(confirm_field,"cell12,w150!");//addtocol1,line2

//按钮面板
JPanelbuttons=newJPanel();
buttons.setOpaque(false);
//边缘为5像素;有两列,中间是10像素的间距,列内元素居中;有一行,行中上下对齐也是居中
buttons.setLayout(newMigLayout("insets5,fill","[center]10[center]","[center]"));
JButtonyes=newJButton("Y.确定");
//快捷键设为虚拟键Y,得到下划线效果
yes.setMnemonic(KeyEvent.VK_Y);

JButtonquit=newJButton("Q.退出");
//快捷键设为虚拟键Q,得到下划线效果
quit.setMnemonic(KeyEvent.VK_Q);

//把yes按钮加到第一列第一行,min:pref:max的大小都设为100像素
buttons.add(yes,"cell00,w100!");
//把quit按钮加到第二列第一行,min:pref:max的大小都设为100像素
buttons.add(quit,"cell10,w100!");

//把按钮面板加到contentPane里面
add(buttons,"cell01,grow");
pack();
setBounds(0,0,500,400);
setLocationRelativeTo(null);
setVisible(true);
}
publicstaticvoidmain(String[]args){
SwingUtilities.invokeLater(newRunnable(){

@Override
publicvoidrun(){
MyFrameframe=newMyFrame();

}

});
}
}

效果是这样:

MigLayout还允许你使用辅助线来debug。把这一行:

first.setLayout(newMigLayout("insets5,fill","[right,40%]5[fill,grow]","[]5[]5[]"));

改成:

first.setLayout(newMigLayout("insets5,fill,debug","[right,40%]5[fill,grow]","[]5[]5[]"));

你就可以看到first面板里面的辅助线了。

Java swing的文本框与按钮排列

package bdzhidao;
/耐侍*
* 建议你学习一下最基本的布局方式,BorderLayout,FlowLayout,GridLayout,CardLayout,SpingLayout,BoxLayout;
*/昌卜吵

import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;

public class Layout extends JFrame{
private static final long serialVersionUID = 1L;
public Layout(){
this.setLayout(new FlowLayout(FlowLayout.LEFT,5,5));
this.add(new JTextField(18));
this.add(new JButton("按钮1"));
this.add(new JButton("按钮2"));

}
public static void main(String[] args){
JFrame frame=new Layout();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(240,100);
frame.setVisible(true);
frame.setLocation(200,300);//缺省是弊岩显示在桌面左上角
frame.setResizable(false);
}

}

分享到 :
相关推荐

美国虚拟主机评测的方法有哪些(美国虚拟主机评测的方法有哪些呢)

美国虚拟主机评测的方法有:1。根据系统高峰期的使用人数和各种操作的频率来设计综合测试...

tomcat虚拟主机有什么用(tomcat虚拟主机的配置)

Server和ServiceTomcat中Server是最顶级的组件。它代表Tom[...

云服务器性能监控要关注的三大问题(云服务器性能监控要关注的三大问题是)

随着云服务器的市场需求提高。很多IDC服务商都推出了云服务器产品。企业在选择云服务器...

怎么看香港免备案虚拟主机好不好(免费免备案香港虚拟主机)

看香港免备案虚拟主机好不好的方法:1。看香港虚拟主机稳定性是否较好。网站在线率是否有...

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注