总结
1.继承JFrame类
2.在最上方定义组件
3.在构造方法中创建组件
4.在构造方法中添加组件
5.设置窗体属性
6.显示窗体
7.在主函数中创建对象
所有布局管理器都可以添加任意组件
1.继承JFrame类
2.在最上方定义组件
3.在构造方法中创建组件
4.在构造方法中添加组件
5.设置窗体属性
6.显示窗体
7.在主函数中创建对象
所有布局管理器都可以添加任意组件
(滚动条...在这里拿按钮举例子)
package demo;
import java.awt.*;
import javax.swing.*;
public class Demo extends JFrame
{
//指向空,防止出错
JButton[] an={null,null,null,null,null,null,null,null};
public static void main(String[] args)
{
Demo lx= new Demo();
}
public Demo()
{
an[0]=new JButton("话梅");
an[1]=new JButton("果脯");
an[2]=new JButton("薯片");
an[3]=new JButton("饼干");
an[4]=new JButton("巧克力");
an[5]=new JButton("腰果");
an[6]=new JButton("锅巴");
an[7]=new JButton("开心果");
//由于java默认的是边界布局管理器,此时必需添加布局管理器
this.setLayout(new FlowLayout()); //添加布局管理器,以免添加出现错误
//窗口一行放不下时,下一行向左对齐
//this.setLayout(new FlowLayout(FlowLayout.LEFT));
//窗口一行放不下时,下一行向右对齐
// this.setLayout(new FlowLayout(FlowLayout.RIGHT));
this.add(an[0]);
this.add(an[1]);
this.add(an[2]);
this.add(an[3]);
this.add(an[4]);
this.add(an[5]);
this.add(an[6]);
this.add(an[7]);
//5.设置窗体属性
this.setTitle("边界布局BorderLayout");
this.setSize(380,320);
this.setLocation(200,200);
//窗口界面不可以放大缩小
this.setResizable(false);
//这句一定要写对,因为这句写的对不对运行时看不出来
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
网格布局GridLayout
package text;
import java.awt.*;
import javax.swing.*;
public class Text extends JFrame
{
//指向空,防止出错
JButton[] an={null,null,null,null,null,null,null,null,null};
int s=9;
public static void main(String[] args)
{
Text mm=new Text();
}
public Text()
{
an[0]=new JButton("话梅");
an[1]=new JButton("果脯");
an[2]=new JButton("薯片");
an[3]=new JButton("饼干");
an[4]=new JButton("巧克力");
an[5]=new JButton("腰果");
an[6]=new JButton("锅巴");
an[7]=new JButton("开心果");
an[8]=new JButton("xing");
//必须定义,3和3表示3*3的网格。15和15长和高的像素空间
this.setLayout(new GridLayout(3,3,15,15));
for(int i=0;i<s;i++)
{
this.add(an[i]);
}
this.setTitle("网格布局GridLayout");
this.setSize(380,320);
this.setLocation(200,200);
//窗口界面不可以放大缩小
this.setResizable(false);
//这句一定要写对,因为这句写的对不对运行时看不出来
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}