小工具知识网

当前位置: 首页 > 行业知识 > Java——GUI(图形用户界面)-浅记

行业知识

Java——GUI(图形用户界面)-浅记

2026-12-04 18:39:17 来源:互联网转载

目录

JFrame

1、创建窗口

2、基本组件

3、常用布局

1、FlowLayout布局

2、BorderLayout布局(默认)

3、CardLayout布局

4、GridLayout布局

5、null布局

6、布局嵌套

4、处理事件


JFrame

一定要先继承这个类

1、创建窗口

//构造一个窗体//      JFrame frame=new JFrame("123");        //设置标题        this.setTitle("学生在线");        //窗体的初始位置//      this.setLocationRelativeTo(null);居中显示有问题,会显示在右下角//      this.setLocation(500,300 );//固定值我的是居中,到别的电脑不一定居中        //居中显示        Toolkit t=Toolkit.getDefaultToolkit();//获取屏幕分辨率        Dimension s=t.getScreenSize();//获取屏幕的宽高        this.setLocation((s.width-width)/2, (s.height-height)/2);                //窗体的大小        this.setSize(width,height);        //窗体是否可见        this.setVisible(true);        //窗口不可变        this.setResizable(false);        //当窗口关闭时结束程序        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

2、基本组件

1、文本框: jcomponent的子类jtextfield创建        函数:JTextFiled(名字);        方法:public string getText(int num);             public void setText(string t);​2、文本区:jcomponent的子类jtextArea创建        函数:JTextArea(名字);        方法:public string getText(int a);             public void setText(string t);             3、按钮:jcomponent的子类jtext创建        函数:JButton();Jbutton( string text)        方法:public void addActionListener(ActionListener  I)        4、标签:JLable        函数:Jlble(名字);        方法:public string getText(int a);             public void setText(string t);5、选择框:JCheckBox        函数:JCheckBox();        方法:public void addltemListener( ltem.. i)             public void addActionListener(Ac.. i)            litemListener接口中:getltemSelectable()相当于getSource()                                getStateChange();若被选中selected            6、单选按钮:JRadioButton        函数:JRadioButton()        方法:addltemListener( ltem.. i)        7、下拉列表:JcomboBox        函数:JcomboBox()        方法:getSelectedIndex();​​8、密码框:JPasswordField        函数:JPasswordField()        方法:getText()             setText()             setEchoChar(char c)使用该方法重新设置回显字符             getpassword()方法返回实际的密码

3、常用布局

1、FlowLayout布局

1、创建布局对象(顺序布局)

FlowLayout flow=new FlowLayout();

2、容器frame使用布局对象

frame.setLayout(flow)

3、con可以使用contaniner类提供的add方法将组件顺序添加到容器

2、BorderLayout布局(默认)

创建布局(五大块)

BorderLayout b=new BorderLayout();

使用

与flow布局差不多但它可以将组件指定位置添加​frame.add(b,BorderLayout.CENTER)

3、CardLayout布局

1、创建对象

CardLayout  card=new CardLayout()

2、为容器设置布局

frame.setLayout(card)

3、容器调用add将组件b加入容器

add(string name,Component b);

4、布局对象card用CardLayout提供的show()方法,显示name组件

card.show(frame,name)

4、GridLayout布局

1、创建布局对象

GridLayout grid=new GridLayout(10,8);

2、使用GridLayout布局将组件添加到容器

5、null布局

6、布局嵌套

4、处理事件

上一篇:电脑命令怎么进入服务器?

下一篇:独家揭秘,红双喜香烟最新价格行情,行情一网打尽!