JButton
按钮的实现
构造方法
- JButton(String text):创建一个带文本的按钮
成员方法
- void setSize(int width,int height):设置大小
- void setLocation(int x,int y):设置位置(X坐标,Y坐标)
- void setBounds(int x,int y,int width,int height):设置位置和大小。
package com.itheima_02;
import javax.swing.*;
/*
构造方法
JButton(String text):创建一个带文本的按钮
成员方法
void setSize(int width, int height):设置大小
void setLocation(int x, int y):设置位置(x坐标,y坐标)
*/
public class JButtonDemo {
public static void main(String[] args) {
//创建窗体对象
//取消窗体的默认布局
JFrame jf = new JFrame();
// 设置窗口标题
jf.setTitle("窗口添加按钮");
// 设置窗口尺寸宽400,高三百
jf.setSize(400, 300);
// 传入数字为3表示窗口关闭时,退出应用
jf.setDefaultCloseOperation(3);
jf.setLocationRelativeTo(null);
jf.setAlwaysOnTop(true);
// 取消窗体默认布局
jf.setLayout(null);
//JButton(String text):创建一个带文本的按钮
JButton btn = new JButton("我是按钮!");
// btn.setSize(100, 20);
//
// btn.setLocation(100, 100);
btn.setBounds(100, 100, 100, 20);
JButton btn2 = new JButton("我是按钮2");
btn2.setBounds(100, 120, 100, 20);
jf.add(btn);
jf.add(btn2);
// //void setSize(int width, int height):设置大小
// btn.setSize(100,20);
// //void setLocation(int x, int y):设置位置(x坐标,y坐标)
btn.setLocation(0,0);
// btn.setLocation(100,100);
//设置窗体可见
jf.setVisible(true);
}
}
Jabel
短文本字符串或图像的显示区域
构造方法:
-
JLabel(String text):使用指定的文本创建JLable实例
-
JLable(Icon image):使用指定的图像创建JLable实例
-
imagelcon(String filename):从指定的文件创建ImageIcon
-
文件路径:绝对路径和相对路径
-
绝对路径:完整的路径名,不需要任何其他信息就可以定义它所表示的文件
-
相对路径:必须使用取自其他路径名的信息进行解释
成员方法: -
void setButtons(int x,int y,int width,int height):设置位置和大小
package com.itheima_02;
import javax.swing.*;
/*
构造方法
JLabel(String text):使用指定的文本创建 JLabel实例
JLabel(Icon image):使用指定的图像创建 JLabel实例
ImageIcon(String filename):从指定的文件创建ImageIcon
成员方法
void setBounds(int x, int y, int width, int height):设置位置和大小
*/
public class JLabelDemo {
public static void main(String[] args) {
//创建窗体对象
JFrame jf = new JFrame();
jf.setTitle("显示文本和图像");
jf.setSize(400, 300);
jf.setDefaultCloseOperation(3);
jf.setLocationRelativeTo(null);
jf.setAlwaysOnTop(true);
jf.setLayout(null);
//JLabel(String text):使用指定的文本创建 JLabel实例
JLabel jLabel = new JLabel("好好学习");
jLabel.setBounds(0, 0, 100, 20);
jf.add(jLabel);
//JLabel(Icon image):使用指定的图像创建 JLabel实例
//ImageIcon(String filename):从指定的文件创建ImageIcon
// ImageIcon imageIcon = new ImageIcon("F:\\ideaProjects\\javase_code\\itheima-api-gui\\images\\mn.png");
// JLabel jLabel2 = new JLabel(imageIcon);
// 相对路径显示图片
JLabel jLabel2 = new JLabel(new ImageIcon("itheima-api-gui\\images\\mn.png"));
jLabel2.setBounds(50, 50, 100, 143);
jf.add(jLabel2);
//设置窗体可见
jf.setVisible(true);
}
}
案例一:用户登录
- 需求编写如图所示的登录框
package com.itheima_03;
import javax.swing.*;
/*
用户登录
*/
public class UserLoginDemo {
public static void main(String[] args) {
// 创建Jframe窗体对象
JFrame jf = new JFrame();
// 设置窗口标题
jf.setTitle("用户登录");
// 设置窗口大小
jf.setSize(400,300);
// 设置关闭时退出程序设置当用户在此帧上启动“关闭”时默认发生的操作
jf.setDefaultCloseOperation(3);
// 设置窗口位置居中
jf.setLocationRelativeTo(null);
// 设置窗口浮于其他窗口之上
jf.setAlwaysOnTop(true);
// 设置布局管理
jf.setLayout(null);
// 显示用户名文本
JLabel usernameLable = new JLabel("用户名");
// 设置字体显示位置
usernameLable.setBounds(50,50,50,20);
// 在窗体中加入此文本
jf.add(usernameLable);
// 设置用户名输入框
JTextField usernameField = new JTextField();
// 设置文本框位置和长宽
usernameField.setBounds(150,50,180,20);
// 在窗体中加入文本框
jf.add(usernameField);
// 设置用户名密码
JLabel passwordLable = new JLabel("密码");
// 设置用户名密码位置
passwordLable.setBounds(50,100,50,20);
// 在窗体中加入文本
jf.add(passwordLable);
// 设置密码文本框
JPasswordField passwordField = new JPasswordField();
// 设置文本框位置大小
passwordField.setBounds(150,100,180,20);
// 将文本框加入窗体
jf.add(passwordField);
// 设置登录按钮
JButton loginButton = new JButton("登录");
// 设置按钮位置以及大小
loginButton.setBounds(50,200,280,20);
// 将按钮添加如窗体
jf.add(loginButton);
// 设置窗口可见
jf.setVisible(true);
}
}
案例二: 聊天室
- 需求:如图所示,做出界面
package com.itheima_03;
import javax.swing.*;
import java.awt.*;
/*
* 聊天室
* */
public class ChatRoomDemo {
public static void main(String[] args) {
// 创建窗体对象
JFrame jf = new JFrame();
// 设置窗体标题
jf.setTitle("聊天室");
// 设置窗体大小
jf.setSize(400, 300);
// 关闭时退出程序
jf.setDefaultCloseOperation(3);
// 设置窗体位置
jf.setLocationRelativeTo(null);
// 设置窗体浮于其他窗体上方
jf.setAlwaysOnTop(true);
// 设置窗体布局
jf.setLayout(null);
// 显示聊天信息的文本域
JTextArea messageAre = new JTextArea();
// 设置文本域的大小
messageAre.setBounds(10,10,360,200);
// 将文本域放入窗体
jf.add(messageAre);
// 输入聊天信息的文本框field:n 字段
JTextField messageField = new JTextField();
// 设置聊天信息文本框大小
messageField.setBounds(10,230,180,20);
// 聊天信息文本框加入窗体
jf.add(messageField);
/*设置发送按钮*/
JButton sendButton = new JButton("发送");
// 设置窗体位置和窗体大小
sendButton.setBounds(200,230,70,20);
// 将按钮添加到文本框
jf.add(sendButton);
/*s设置清空聊天按钮*/
JButton clearButton = new JButton("清空聊天");
// 设置按钮位置和大小
clearButton.setBounds(280,230,100,20);
// 将按钮添加到窗体
jf.add(clearButton);
// 设置窗体可见
jf.setVisible(true);
}
}
案例三:猜数字
需求如图所示:做出界面
package com.itheima_04;
//猜数字
import javax.swing.*;
public class GuessNumberText {
public static void main(String[] args) {
// 创建窗体对象
JFrame jf = new JFrame();
// 设置窗体标题
jf.setTitle("猜数字");
// 设置窗体大小
jf.setSize(400, 300);
// 设置窗口退出时默认关闭
jf.setDefaultCloseOperation(3);
// 设置窗体居中
jf.setLocationRelativeTo(null);
// 设置窗体始终在其他窗体之上
jf.setAlwaysOnTop(true);
// 设置布局
jf.setLayout(null);
//设置提示信息
JLabel messageLable = new JLabel("系统产生了一个1-100之间的数据,请猜一猜");
// 设置JLable的位置和大小
messageLable.setBounds(70,50,350,20);
// 将文本加入窗体
jf.add(messageLable);
// 输入要猜的数字
JTextField numberField = new JTextField();
// 设置文本框的位置和大小
numberField.setBounds(120,100,150,20);
// 将文本框加入窗体
jf.add(numberField);
// 编写猜数字的按钮
JButton guessButton = new JButton("我猜");
// 设置按钮大小和位置
guessButton.setBounds(150,150,100,20);
// 将按钮加入窗体
jf.add(guessButton);
// 设置窗体可见
jf.setVisible(true);
}
}
案例四:手机日期和时间显示
需求:如图所示,做出界面
分析:
①4个JLable实现
package com.itheima_05;
import javax.swing.*;
/*
* 手机日期和时间显示
* */
public class ShowDateTimeTest {
public static void main(String[] args) {
// 创建窗体对象
JFrame jf = new JFrame();
// 设置窗体标题
jf.setTitle("手机日期和时间显示");
// 设置窗口大小
jf.setSize(400,300);
// 设置窗口关闭时退出程序
jf.setDefaultCloseOperation(3);
// 设置窗口居中
jf.setLocationRelativeTo(null);
// 设置窗口在其他窗口之上
jf.setAlwaysOnTop(true);
// 取消默认布局
jf.setLayout(null);
// 提示日期
JLabel dateLable = new JLabel("日期");
// 设置位置大小
dateLable.setBounds(50,50,100,20);
// 添加入窗体
jf.add(dateLable);
// 按格式显示字符串
JLabel showDateLable = new JLabel("XXXX年XX月XX日");
showDateLable.setBounds(50,80,200,20);
jf.add(showDateLable);
// 提示时间
JLabel timeLable = new JLabel("时间");
timeLable.setBounds(50,150,100,20);
jf.add(timeLable);
// 显示时间
JLabel showTimeLable = new JLabel("XXXX");
showTimeLable.setBounds(50,180,200,20);
jf.add(showTimeLable);
// 设置窗体可见
jf.setVisible(true);
}
}