GUI
Java GUI(图形用户界面)是使用Java编程语言创建的用户界面。Java提供了许多库和工具,可以轻松地创建各种GUI应用程序。
package com.ffyc.javagui.listener;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Demo3Frame extends JFrame {
public Demo3Frame() throws HeadlessException {
this.setTitle("欢迎登录");
this.setSize(300,300);//设置宽高
this.setLocationRelativeTo(null); //水平垂直居中
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗口时退出程序
this.setResizable(false);//设置禁止窗口拖拽调整大小
//创建一个面板
JPanel jPanel = new JPanel();
JTextField jTextField = new JTextField(15);
JButton jButton = new JButton("登录");
jPanel.add(jTextField);
jPanel.add(jButton);
//把面板添加到窗口上
this.add(jPanel);
this.setVisible(true);//让窗口显示 放在设置的最后一行
//为组件添加事件处理程序
//jButton.addActionListener(new B());
//new 接口 创建一个匿名内部类的对象, 是为了简化语法
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//获得文本框输入的内容
String account = jTextField.getText();
if(account.length()==0){
//消息提示框
//JOptionPane.showMessageDialog(null,"请输入账号!");
//JOptionPane.showMessageDialog(null,"请输入账号!","操作提示",JOptionPane.WARNING_MESSAGE);
int res = JOptionPane.showConfirmDialog(null, "您确定要退出吗?","操作提示",JOptionPane.OK_CANCEL_OPTION);
System.out.println(res);// 点击确定返回0 , 点击取消返回2
if(res==0){
//执行退出操作
}
return;
}
if(account.length()<6 ||account.length()>10){
JOptionPane.showMessageDialog(null,"请输入一个6-10位之间的账号!");
return;
}
}
});
}
public static void main(String[] args) {
new Demo3Frame();//创建一个窗口对象
}
}
这是一个简单的聊天窗口案例,其实GUI界面是非常简单的一个内容,下面是一个聊天室实现:
import java.util.Date;
public class Chat extends JFrame {
JTextArea jTextArea;
public Chat(String account, Socket socket) throws IOException {
DataOutputStream dataOutputStream=new DataOutputStream(socket.getOutputStream());
this.setTitle("欢迎"+account+"使用");
this.setSize(400,400);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
/*
DO_NOTHING_ON_CLOSE
是在点击关闭之后什么都不做
但是如果用EXIT,则会退出程序
*/
this.setResizable(false);
JPanel jPanel=new JPanel(new BorderLayout());
//历史记录文本
jTextArea=new JTextArea();
jTextArea.setEditable(false);
JScrollPane jScrollPane=new JScrollPane(jTextArea);
jPanel.add(jScrollPane);
//发送窗口
JPanel sendPanel=new JPanel();
JTextArea jTextArea1=new JTextArea(3,20);
jTextArea1.setLineWrap(true);
JScrollPane jScrollPane1=new JScrollPane(jTextArea1);
JButton sendButton=new JButton("发送");
sendPanel.add(jScrollPane1);
sendPanel.add(sendButton);
jPanel.add(sendPanel,BorderLayout.SOUTH);
this.add(jPanel);
new ClientThread(socket).start();
//为窗口添加事件监听
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
int res=JOptionPane.showConfirmDialog(null,"确定要退出聊天吗?","操作提示",JOptionPane.OK_CANCEL_OPTION);
if(res==0){
//点击确定
new Login();
dispose();
}
}
});
//点击发送按钮把内容发送出去
sendButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//获得消息内容
String msg=account+"说:"+"\t"+DateUtil.DateToString(new Date(),"yyyy-MM-dd")+"\n";
if(jTextArea1.getText().length()==0){
//为空
JOptionPane.showConfirmDialog(null,"输入内容不能为空!请重新输入!");
return;
}
//不为空
// System.out.println());
msg+=jTextArea1.getText();
try {
dataOutputStream.writeUTF(msg);
jTextArea1.setText("");
} catch (IOException ioException) {
ioException.printStackTrace();
JOptionPane.showMessageDialog(null,"信息发送失败,请尝试重新连接网络");
}
}
});
}
boolean temp=true;
class ClientThread extends Thread{
DataInputStream dataInputStream;
public ClientThread(Socket socket) throws IOException {
dataInputStream=new DataInputStream(socket.getInputStream());
}
@Override
public void run() {
while(temp){
try {
String msg=dataInputStream.readUTF();
System.out.println(msg);
jTextArea.append(msg+"\n");
} catch (IOException e) {
e.printStackTrace();
temp=false;
}
}
}
}
}
import java.util.Date;
public class DateUtil {
//让Date类型转换为日期
public static String DateToString(Date date){
//创立一个SDF类型的对象,通过对象调用函数转化
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
String str_date=sdf.format(date);
return str_date;
}
//方法重载,用户指定格式
public static String DateToString(Date date,String p){
SimpleDateFormat sdf=new SimpleDateFormat(p);
String str_date=sdf.format(date);
return str_date;
}
//方法重载:将当前时间格式转换为指定格式的字符串
public static String DateToString(String p){
SimpleDateFormat sdf=new SimpleDateFormat(p);
String str_date=sdf.format(new Date());//直接创建一个对象
return str_date;
}
//将日期转化为Date类型
public static Date StringToDate(String str) throws Exception{
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
Date date_str=sdf.parse(str);
return date_str;
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.Socket;
public class Login extends JFrame {
public Login(){
this.setTitle("欢迎使用");
this.setSize(300,300);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
JPanel jPanel=new JPanel(new GridLayout(4,1));
JPanel welconPanel=new JPanel();
JLabel welcomelabel=new JLabel("登陆");
welconPanel.add(welcomelabel);
JPanel account=new JPanel();
JLabel accountLabel=new JLabel("账号");
JTextField accountField=new JTextField(15);
account.add(accountLabel);
account.add(accountField);
JPanel password=new JPanel();
JLabel passwordLabel=new JLabel("密码");
JPasswordField passwordField=new JPasswordField(15);
password.add(passwordLabel);
password.add(passwordField);
JPanel button=new JPanel();
JButton loginButton=new JButton("登录");
JButton regButton=new JButton("注册");
button.add(loginButton);
button.add(regButton);
jPanel.add(welconPanel);
jPanel.add(account);
jPanel.add(password);
jPanel.add(button);
this.add(jPanel);
this.setVisible(true);
//监听事件
loginButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String account=accountField.getText();
String password=new String(passwordField.getPassword());
if(account.length()==0){
JOptionPane.showMessageDialog(null,"账号不能为空");
}
if(password.length()==0){
JOptionPane.showMessageDialog(null,"密码不能为空");
}
try {
Socket socket=new Socket("127.0.0.1",9999);
new Chat(account,socket).setVisible(true);
dispose();
} catch (IOException ioe) {
ioe.printStackTrace();
JOptionPane.showMessageDialog(null,"当前服务器忙,请稍后再试!");
}
}
});
}
}
public class Start {
public static void main(String[] args) {
new Login();
// Chat();
}
}
import java.util.ArrayList;
public class Server extends JFrame {
//存放所有连接到服务器的Socket集合
ArrayList<Socket> sockets=new ArrayList<>();
JTextArea jTextArea;
public Server(){
this.setTitle("监控");
this.setSize(300,300);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
JPanel jPanel=new JPanel(new BorderLayout());
//历史记录文本
jTextArea=new JTextArea();
jTextArea.setEditable(false);
JScrollPane jScrollPane=new JScrollPane(jTextArea);
jPanel.add(jScrollPane);
//发送窗口
JPanel sendPanel=new JPanel();
JTextArea jTextArea1=new JTextArea(3,20);
jTextArea1.setLineWrap(true);
JScrollPane jScrollPane1=new JScrollPane(jTextArea1);
JButton sendButton=new JButton("发送");
sendPanel.add(jScrollPane1);
sendPanel.add(sendButton);
jPanel.add(sendPanel,BorderLayout.SOUTH);
this.add(jPanel);
}
public void serverStart(){
try {
//启动服务
ServerSocket serverSocket=new ServerSocket(9999);
//监听多个客户端的的连接
while(true){
Socket socket=serverSocket.accept();
sockets.add(socket);
System.out.println("有服务器连接成功!当前数量为:"+sockets.size());
new socketThread(socket).start();
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("服务器启动失败");
}
}
//写一个内部类,用来监听各自客户端有没有发送消息
class socketThread extends Thread{
DataInputStream dataInputStream;
Socket socket;
public socketThread(Socket socket) throws IOException {
this.socket=socket;
this.dataInputStream=new DataInputStream(socket.getInputStream());
}
@Override
public void run() {
//接受客户端的消息
try {
while (true){
String msg=dataInputStream.readUTF();
System.out.println(msg);
//传回信息
for(Socket sock:sockets){
DataOutputStream dataOutputStream=new DataOutputStream(sock.getOutputStream());
dataOutputStream.writeUTF(msg);
}
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("客户端异常!请检查客户端连接!");
sockets.remove(socket);
}
}
}
}
public class ServerStart {
public static void main(String[] args) {
new Server().serverStart();
}
}