学习完GUI之后,我们发现到目前为止,我们编写的都是基于控制台的程序;
GUI(Graphical User Interface)即图形用户界面,它能够使应用程序看上去更加友好;
JFrame
void setSize(int width, int height)
void setVisible(boolean b)
void setTitle(String title
void setResizable(boolean resizable)
void setLocation(int x,int y)
void setLocationRelativeTo(null);
void setDefaultCloseOperation(int operation)
void dispose()
常用方法:
void setBackground(Color bg)设置面板的背景色,由参数bg指定颜色
void setLayout(LayoutManager mgr)设置面板的布局,参数是布局管理器
Component add(Component comp)往面板中添加一个组件
代码如下:
package hm;
import javax.swing.*;
import java.awt.*;
public class Register extends JFrame {
public Register(){
this.setTitle("欢迎登陆");
this.setSize(310,150);
this.setLocationRelativeTo(null); //相对位置 水平垂直居中
this.setResizable(false);//禁止设置窗口大小
JPanel jPanel = new JPanel();
JButton jButton = new JButton("登录");//添加按钮
JButton jButton1 = new JButton("取消");
JLabel label = new JLabel("管理员");
label.setForeground(Color.BLACK);
label.setFont(new Font("黑体",Font.PLAIN,20));
JTextField accountText = new JTextField(14);
JLabel label1 = new JLabel("密 码");
label1.setForeground(Color.BLACK);
label1.setFont(new Font("黑体",Font.PLAIN,20));
JTextField accountText1 = new JTextField(14);
jPanel.add(label);
jPanel.add(accountText);
jPanel.add(label1);
jPanel.add(accountText1);
jPanel.add(jButton);
jPanel.add(jButton1);
this.add(jPanel);
this.setVisible(true);
}
public static void main(String[] args) {
new Register();
}
}
运行如下