目录
GUI
概念
swing概述
设置窗体的常用方法
方法
实例
常用组件
标签(JLabel)
单行文本(JTextField)
密码框(JPasswordField)
多行文本框(JTextArea)
按钮(JButton)
GUI
概念
- GUI(Graphical User Interface)即图形用户界面,是指采用图形方式 显示的用户界面,与早期计算机使用的命令行界面相比,图形界面对于用 户来说在视觉上更易于接受。
swing概述
- swing 是一个为Java设计的图形界面工具包javax.swing,该包中包括了图形用 户界面的各种组件支持。
- 一个 Java 的图形界面,由各种不同类型的“元素”组成,这些“元素”被称为组件(Component)
设置窗体的常用方法
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()
实例
/*
自定义一个窗口类,继承JFrame,拥有了窗口的功能
*/
public class DemoFrame extends JFrame {
public DemoFrame(){
this.setTitle("原神启动");//设置标题
this.setSize(500,400);//设置大小
//this.setLocation(222,333);设置窗口指定坐标位置
this.setLocationRelativeTo(null);//设置窗口居中
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设定关闭窗口的选项 关闭窗口,退出程序
this.setResizable(false);//设置窗口大小不能改变
setVisible(true);//显示窗口,此行放在最后一行设置
}
public static void main(String[] args) {
new DemoFrame();
}
}
常用组件
标签(JLabel)
标签是容纳文本和图标的控件,通常用来在界面中标识别的控件。
构造函数:
- JLabel(String text) //创建一个带文本的标签
方法:
- setFont(new Font(“宋体”,Font.BOLD, 18)); //设置字体
public class Framedemo extends JFrame {
private JLabel text;
public Framedemo( ){
//定义组件变量
text = new JLabel("文本标签");
//this
this.setTitle("标题");//设置标题
this.setSize(500,400);//设置大小
// this.setLocation(222,333);设置窗口指定坐标位置
this.setLocationRelativeTo(null);//设置窗口居中
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设定关闭窗口的选项 关闭窗口,退出程序
this.setResizable(false);//设置窗口大小不能改变
//text
text.setFont(new Font("宋体", Font.BOLD,18));
setVisible(true);//显示窗口,此行放在最后一行设置
}
单行文本(JTextField)
JTextField的构造函数:
- JTextField(int columns)
方法:
- String getText() //获得文本框中的文本
public class Framedemo extends JFrame {
private JTextField oneText;
public Framedemo( ){
//定义组件变量
oneText = new JTextField();
//this
this.setTitle("标题");//设置标题
this.setSize(500,400);//设置大小
// this.setLocation(222,333);设置窗口指定坐标位置
this.setLocationRelativeTo(null);//设置窗口居中
Container contentPane = getContentPane();
contentPane.setLayout(null);
// oneText---JTextField
oneText.setText("单行文本");
String s = oneText.getText();
System.out.println(s);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设定关闭窗口的选项 关闭窗口,退出程序
this.setResizable(false);//设置窗口大小不能改变
setVisible(true);//显示窗口,此行放在最后一行设置
}
密码框(JPasswordField)
构造函数:
- JPasswordField(String text)
- JPasswordField(String text, int columns)
方法:
- char[] getPassword()
private JLabel passageField;
public Framedemo( ){
//定义组件变量
passageField = new JLabel();
//this
this.setTitle("标题");//设置标题
this.setSize(500,400);//设置大小
// this.setLocation(222,333);设置窗口指定坐标位置
this.setLocationRelativeTo(null);//设置窗口居中
Container contentPane = getContentPane();
contentPane.setLayout(null);
//passageField ---JTextField
passageField.setText("\u5bc6\u7801");
contentPane.add(passageField);
passageField.setBounds(35, 145, 45, 30);
contentPane.add(textField2);
textField2.setBounds(85, 150, 215, textField2.getPreferredSize().height);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设定关闭窗口的选项 关闭窗口,退出程序
this.setResizable(false);//设置窗口大小不能改变
setVisible(true);//显示窗口,此行放在最后一行设置
}
多行文本框(JTextArea)
构造函数:
JTextArea(int rows, int columns) //创建一个指定行数和列数的空文本域
方法:
- String getText() //获得文本域中的文本
- void setFont(Font font) //设置文本域中文本的字体
- void setLineWrap(boolean wrap) //是否自动换行,默认false
如果需要文本区自动出现滚动条,可将文本区对象放入滚动窗格(JScrollPane)中:
- JScrollPane scrollPane = new JScrollPane(txtArea);
- add(scrollPane )
public class Framedemo extends JFrame {
private JTextArea textArea1;
public Framedemo( ){
label1 = new JLabel();
//======== scrollPane1 ========
{
scrollPane1.setViewportView(textArea1);
}
contentPane.add(scrollPane1);
scrollPane1.setBounds(10, 10, 310, 210);
}
}
按钮(JButton)
构造方法:
JButton(String text) //创建一个带文本的标签
方法:
void setBackground(Color bg) 设置按钮的背景色
void setEnabled(boolean b) 设置启用(或禁用)按钮,由参数b决定
void setToolTipText(String text) 设置按钮的悬停提示信息
public class Framedemo extends JFrame {
private JButton button1;
public Framedemo( ){
//定义组件变量
//---- button1 ----
button1 = new JButton();
button1.setText("\u53d1\u9001");
contentPane.add(button1);
button1.setBounds(new Rectangle(new Point(320, 230), button1.getPreferredSize()));
}
}