Java_GUI_布局_网格组布局
2006-02-12 16:13
459 查看
import java.awt.Component;
import java.awt.Container;
import java.awt.GraphicsEnvironment;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class GirdBagLayoutTest
{
public static void main(String[] args)
{
GirdBagLayoutFrame frame = new GirdBagLayoutFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class GirdBagLayoutFrame extends JFrame
{
public GirdBagLayoutFrame()
{
setTitle("网格组事件");
setSize(300, 200);
Container con = getContentPane();
GirdBagLayoutPanel panel = new GirdBagLayoutPanel();
con.add(panel);
}
}
class GirdBagLayoutPanel extends JPanel
{
public JLabel lname, lsize;
public JComboBox cname, csize;
public JCheckBox bold, italic;
public JTextArea area;
public GirdBagLayoutPanel()
{
lname = new JLabel("Name");
lsize = new JLabel("Size");
String[] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment()
.getAvailableFontFamilyNames();
cname = new JComboBox(fonts);
csize = new JComboBox(new String[]
{ "8", "12", "14", "16", "18", "20", "24", "26", "36" });
bold = new JCheckBox("Bold");
italic = new JCheckBox("Italic");
area = new JTextArea("欢迎来到太湖");
area.setLineWrap(true);//自动换行
//实例化网格组布局
GridBagLayout layout = new GridBagLayout();
//设置布局管理
setLayout(layout);
//实例化网格组布局的限制类(此类协助网格组布局类完成工作)
GridBagConstraints constraints = new GridBagConstraints();
constraints.anchor = GridBagConstraints.EAST;//居右对其
constraints.fill = GridBagConstraints.NONE;//不填充
constraints.weightx = 0; //水平方向不缩放
constraints.weighty = 0; //垂直方向不缩放 0代表不缩放 100代表缩放
addObj(constraints,0,0,1,1,lname);
addObj(constraints,0,1,1,1,lsize);
constraints.fill = GridBagConstraints.HORIZONTAL;//水平填充
constraints.weightx = 100;//水平缩放
addObj(constraints,1,0,1,1,cname);
addObj(constraints,1,1,1,1,csize);
constraints.weighty = 100;//垂直缩放
constraints.fill = GridBagConstraints.NONE;//不填充
constraints.anchor = GridBagConstraints.CENTER;//居中对其
addObj(constraints,0,2,2,1,bold);
addObj(constraints,0,3,2,1,italic);
constraints.fill = GridBagConstraints.BOTH;//水平垂直都填充
addObj(constraints,2,0,1,4,area);
}
//方法
public void addObj(GridBagConstraints gc,int x,int y,int w,int h,
Component c)//传递参数 Component 主键
{
gc.gridx = x;
gc.gridy = y;
gc.gridwidth = w;
gc.gridheight = h;
add(c,gc);
}
}
import java.awt.Container;
import java.awt.GraphicsEnvironment;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class GirdBagLayoutTest
{
public static void main(String[] args)
{
GirdBagLayoutFrame frame = new GirdBagLayoutFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class GirdBagLayoutFrame extends JFrame
{
public GirdBagLayoutFrame()
{
setTitle("网格组事件");
setSize(300, 200);
Container con = getContentPane();
GirdBagLayoutPanel panel = new GirdBagLayoutPanel();
con.add(panel);
}
}
class GirdBagLayoutPanel extends JPanel
{
public JLabel lname, lsize;
public JComboBox cname, csize;
public JCheckBox bold, italic;
public JTextArea area;
public GirdBagLayoutPanel()
{
lname = new JLabel("Name");
lsize = new JLabel("Size");
String[] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment()
.getAvailableFontFamilyNames();
cname = new JComboBox(fonts);
csize = new JComboBox(new String[]
{ "8", "12", "14", "16", "18", "20", "24", "26", "36" });
bold = new JCheckBox("Bold");
italic = new JCheckBox("Italic");
area = new JTextArea("欢迎来到太湖");
area.setLineWrap(true);//自动换行
//实例化网格组布局
GridBagLayout layout = new GridBagLayout();
//设置布局管理
setLayout(layout);
//实例化网格组布局的限制类(此类协助网格组布局类完成工作)
GridBagConstraints constraints = new GridBagConstraints();
constraints.anchor = GridBagConstraints.EAST;//居右对其
constraints.fill = GridBagConstraints.NONE;//不填充
constraints.weightx = 0; //水平方向不缩放
constraints.weighty = 0; //垂直方向不缩放 0代表不缩放 100代表缩放
addObj(constraints,0,0,1,1,lname);
addObj(constraints,0,1,1,1,lsize);
constraints.fill = GridBagConstraints.HORIZONTAL;//水平填充
constraints.weightx = 100;//水平缩放
addObj(constraints,1,0,1,1,cname);
addObj(constraints,1,1,1,1,csize);
constraints.weighty = 100;//垂直缩放
constraints.fill = GridBagConstraints.NONE;//不填充
constraints.anchor = GridBagConstraints.CENTER;//居中对其
addObj(constraints,0,2,2,1,bold);
addObj(constraints,0,3,2,1,italic);
constraints.fill = GridBagConstraints.BOTH;//水平垂直都填充
addObj(constraints,2,0,1,4,area);
}
//方法
public void addObj(GridBagConstraints gc,int x,int y,int w,int h,
Component c)//传递参数 Component 主键
{
gc.gridx = x;
gc.gridy = y;
gc.gridwidth = w;
gc.gridheight = h;
add(c,gc);
}
}
相关文章推荐
- Java_GUI_布局_网格布局
- Java开发GUI之GridLayout网格布局
- 十八、java-GUI之网格布局(GridLayout)
- Java使用网格组布局管理器
- Java复杂GUI布局举例与源码
- Java_流式布局、边框布局以及网格布局
- 五、Java图形化界面设计——布局管理器之GridLayout(网格布局)
- JAVA图形界面(GUI)之布局管理器
- JAVA基础再回首(二十七)——JAVA中的图形化界面、GUI概述、简单的窗体创建、事件监听机制、动作事件、GUI布局、鼠标事件
- JAVA进行GUI程序设计之布局管理(下)
- java gui图形界面编程(几乎包含所有常用的控件及布局)
- JAVA的图形用户界面布局GUI入门(上)
- #javaGUI#基于swing包的java用户界面设置入门,布局篇
- Java_GUI_布局_流式布局
- Java的GUI组件的布局管理器
- Java开发GUI之绝对布局
- 05 Java图形化界面设计——布局管理器之GridLayout(网格布局)
- atitit.软件开发GUI 布局管理优缺点总结java swing wpf web html c++ qt php asp.net winform
- java -- GUI程序设计之基于布局管理器的布局
- atitit.软件开发GUI 布局管理优缺点总结java swing wpf web html c++ qt php asp.net winform