目录
七、组件综合练习
7.1 练习-为空判断.
7.2 练习-数字校验.
7.3 练习-账号密码验证.
7.4 练习-黄鹤.
7.5 练习-复利计算器.
7.6 练习-进度条.
7.7 练习-显示文件夹复制进度条.
这是how2Java网站的【图形界面】--组件综合练习 答案
主要参考评论区,可能存在bug。
链接:(七)组件综合练习
在JTextField中输入数据,在旁边加一个按钮JButton,当点击按钮的时候,判断JTextFiled中有没有数据,并使用JOptionPane进行提示。
1、JButton事件响应:actionPerformed(ActionEvent e)
2、JTextFiled组件内容获取
3、JOptionPane弹窗组件使用。
JOptionPane.showMessageDialog(jf,"内容");
package gui;
import javax.swing.*;
import java.awt.*;
import java.sql.*;
public class TestGUI {
public static void main(String[] args){
JFrame jf = new JFrame("LOL");
jf.setSize(416,300);
jf.setLocation(500,300);
jf.setLayout(null);
JPanel jp = new JPanel();
jp.setBounds(8,8,400,300);
jp.setLayout(new FlowLayout());
JTextField content = new JTextField("");
content.setPreferredSize(new Dimension(80,30));
JButton jb = new JButton("检测");
jb.setSize(50,30);
jb.addActionListener(e->{
String cont = content.getText();
if(cont.isEmpty()){
JOptionPane.showMessageDialog(jf,"文本内容为空");}
else{
JOptionPane.showMessageDialog(jf,"文本内容为:"+cont);
}
});
jp.add(content);
jp.add(jb);
jf.add(jp);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
}
在JTextField中输入数据,在旁边加一个按钮JButton,当点击按钮的时候,判断JTextFiled中的数据是否是数字,并使用JOptionPane进行提示
判断是否为数字: https://blog.csdn.net/qq_42133100/article/details/92158507?
public static boolean isNumeric(String str){
Pattern pattern = Pattern.compile("[0-9]*");
return pattern.matcher(str).matches();
}
package gui;
import javax.swing.*;
import java.awt.*;
import java.sql.*;
import java.util.regex.Pattern;
public class TestGUI {
public static boolean isNumeric(String str){
Pattern pattern = Pattern.compile("[0-9]*");
return pattern.matcher(str).matches();
}
public static void main(String[] args){
JFrame jf = new JFrame("LOL");
jf.setSize(416,300);
jf.setLocation(500,300);
jf.setLayout(null);
JPanel jp = new JPanel();
jp.setBounds(8,8,400,300);
jp.setLayout(new FlowLayout());
JTextField content = new JTextField("");
content.setPreferredSize(new Dimension(80,30));
JButton jb = new JButton("检测");
jb.setSize(50,30);
jb.addActionListener(e->{
String cont = content.getText();
if(cont.isEmpty()) {
JOptionPane.showMessageDialog(jf,"输入框内容为空");}
else{
if(isNumeric(cont))
JOptionPane.showMessageDialog(jf,"输入框内容是数字:"+cont);
else
JOptionPane.showMessageDialog(jf,"输入框内容不是数字");
}
});
jp.add(content);
jp.add(jb);
jf.add(jp);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
}
准备两个JTextFiled,一个用于输入账号,一个用于输入密码。
使用密码框:
JPasswordField pf = new JPasswordField("");
再准备一个JButton,上面的文字是登陆
点击按钮之后,首先进行为空判断,如果都不为空,则把账号和密码,拿到数据库中进行比较(SQL语句判断账号密码是否正确),根据判断结果,使用JOptionPane进行提示。
package gui;
import javax.swing.*;
import java.awt.*;
import java.sql.*;
public class TestGUI {
private static String url = "jdbc:mysql://127.0.0.1:3306/how2java?characterEncoding=UTF-8";
private static String Name = "root";
private static String Pwd = "root";
public static void main(String[] args){
try {
Class.forName("com.mysql.jdbc.Driver");
}catch (Exception e){
e.printStackTrace();
}
JFrame jf = new JFrame("register");
jf.setSize(416,300);
jf.setLocation(500,300);
jf.setLayout(null);
JPanel jp1 = new JPanel();
jp1.setBounds(8,8,400,30);
jp1.setLayout(new FlowLayout());
JLabel name = new JLabel("账号:");
JTextField EnterName = new JTextField("");
EnterName.setPreferredSize(new Dimension(80,30));
JLabel password = new JLabel("密码:");
JPasswordField EnterPwd = new JPasswordField("");
EnterPwd.setPreferredSize(new Dimension(80,30));
jp1.add(name);
jp1.add(EnterName);
jp1.add(password);
jp1.add(EnterPwd);
JPanel jp2 = new JPanel();
jp2.setBounds(8,46,400,30);
jp2.setLayout(new FlowLayout());
JButton jb = new JButton("登录");
jb.setSize(50,30);
jb.addActionListener(e->{
if(!EnterName.getText().isEmpty() && !EnterPwd.getText().isEmpty()){
try(Connection conn = DriverManager.getConnection(url,Name,Pwd)){
String str = "select * from user where name = ? and password = ?";
PreparedStatement ps = conn.prepareStatement(str);
ps.setString(1,EnterName.getText());
ps.setString(2,EnterPwd.getText());
ResultSet rs = ps.executeQuery();
if(rs.next()){
JOptionPane.showMessageDialog(jf,"登录成功");
}
else{
JOptionPane.showMessageDialog(jf,"账户名或者密码错误");
}
}catch (Exception ee){
ee.printStackTrace();
}
}
});
jp2.add(jb);
jf.add(jp1);
jf.add(jp2);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
}
把练习-黄鹤改成Swing来完成
用到组件Jlabel、JTextField、JButton
要有为空和数字判断。
package gui;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.regex.Pattern;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class TestGUI {
public static boolean isNumeric(String str){
Pattern pattern = Pattern.compile("[0-9]*");
return pattern.matcher(str).matches();
}
public static void main(String[] args) {
JFrame f=new JFrame("LOL");
f.setSize(400,500);
f.setLocation(500,500);
f.setLayout(null);
JPanel p1=new JPanel();
p1.setBounds(0, 0, 90, 170);
p1.setLayout(new FlowLayout(FlowLayout.LEFT,10,18));
JPanel p2=new JPanel();
p2.setBounds(80, 0, 110, 170);
p2.setLayout(new FlowLayout(FlowLayout.LEFT,20,15));
JPanel p3=new JPanel();
p3.setBounds(190, 0, 80, 170);
p3.setLayout(new FlowLayout(FlowLayout.LEFT,10,18));
JPanel p4=new JPanel();
p4.setBounds(260, 0, 110, 170);
p4.setLayout(new FlowLayout(FlowLayout.LEFT,20,15));
JLabel l1=new JLabel("地名:");
JLabel l2=new JLabel("公司类型:");
JLabel l3=new JLabel("公司名称:");
JLabel l4=new JLabel("老板名称:");
JLabel l5=new JLabel("金额:");
JLabel l6=new JLabel("产品:");
JLabel l7=new JLabel("价格计量单位:");
JTextField tf1=new JTextField();
tf1.setPreferredSize(new Dimension(90,22));
JTextField tf2=new JTextField();
tf2.setPreferredSize(new Dimension(90,22));
JTextField tf3=new JTextField();
tf3.setPreferredSize(new Dimension(90,22));
JTextField tf4=new JTextField();
tf4.setPreferredSize(new Dimension(90,22));
JTextField tf5=new JTextField();
tf5.setPreferredSize(new Dimension(90,22));
JTextField tf6=new JTextField();
tf6.setPreferredSize(new Dimension(90,22));
JTextField tf7=new JTextField();
tf7.setPreferredSize(new Dimension(90,22));
p1.add(l1);p1.add(l3);p1.add(l5);p1.add(l7);
p2.add(tf1);p2.add(tf3);p2.add(tf5);p2.add(tf7);
p3.add(l2);p3.add(l4);p3.add(l6);
p4.add(tf2);p4.add(tf4);p4.add(tf6);
f.add(p1);f.add(p2);f.add(p3);f.add(p4);
JButton b=new JButton("生成");
b.setBounds(160,170,80,30);
JTextArea ta=new JTextArea();
ta.setBounds(10, 220, 365, 230);
ta.setLineWrap(true);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!tf1.getText().isEmpty()&&!tf2.getText().isEmpty()&&!tf3.getText().isEmpty()&&!tf4.getText().isEmpty()&&!tf5.getText().isEmpty()&&!tf6.getText().isEmpty()&&!tf7.getText().isEmpty())
{if (isNumeric(tf5.getText()))
{String str=tf1.getText()+"最大"+tf2.getText()+tf3.getText()+"倒闭了,"
+ "王八蛋老板"+tf4.getText()+"吃喝嫖赌,欠下了"+tf5.getText()+","
+ "带着他的小姨子跑了!我们没有办法,拿着"+tf6.getText()+"抵工资!"
+ "原价都是一"+tf7.getText()+"多、两"+tf7.getText()+"多、三"+tf7.getText()+"多的"+tf6.getText()+","
+ "现在全部只卖二十块,统统只要二十块!"+tf4.getText()+"王八蛋,"
+ "你不是人!我们辛辛苦苦给你干了大半年,你不发工资,你还我血汗钱,还我血汗钱!";
ta.setText(str);}
else
JOptionPane.showMessageDialog(f, "金额不是数字!");
}
else
JOptionPane.showMessageDialog(f, "有内容为空!");
}
});
f.add(b);
f.add(ta);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
参考练习-百万富翁,把 网页版的复利计算器 改成swing来做。
package gui;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class TestGUI {
public static void main(String[] args) {
JFrame f=new JFrame("LOL");
f.setSize(420,400);
f.setLayout(null);
f.setLocation(450,500);
JPanel p1=new JPanel();
p1.setBounds(0,0,100,120);
p1.setLayout(new FlowLayout(FlowLayout.LEFT,10,11));
JPanel p2=new JPanel();
p2.setBounds(135,0,130,120);
p2.setLayout(new FlowLayout(FlowLayout.LEFT,10,9));
JPanel p3=new JPanel();
p3.setBounds(270,0,25,120);
p3.setLayout(new FlowLayout(FlowLayout.LEFT,10,11));
JPanel p4=new JPanel();
p4.setBounds(0,140,400,35);
p4.setLayout(new FlowLayout());
JPanel p5=new JPanel();
p5.setBounds(0,195,70,120);
p5.setLayout(new FlowLayout(FlowLayout.LEFT,10,11));
JPanel p6=new JPanel();
p6.setBounds(135,195,130,120);
p6.setLayout(new FlowLayout(FlowLayout.LEFT,10,9));
JPanel p7=new JPanel();
p7.setBounds(270,195,25,120);
p7.setLayout(new FlowLayout(FlowLayout.LEFT,10,11));
JLabel l1=new JLabel("起始资金");
JLabel l2=new JLabel("每年收益");
JLabel l3=new JLabel("复利年数");
JLabel l4=new JLabel("每年追加资金");
JLabel l5=new JLabel("¥");
JLabel l6=new JLabel("%");
JLabel l7=new JLabel("年");
JLabel l8=new JLabel("¥");
JLabel l9=new JLabel("本金和");
JLabel l10=new JLabel("利息和");
JLabel l11=new JLabel("本息和");
JLabel l12=new JLabel("¥");
JLabel l13=new JLabel("¥");
JLabel l14=new JLabel("¥");
JTextField tf1=new JTextField();
tf1.setPreferredSize(new Dimension(120,20));
JTextField tf2=new JTextField();
tf2.setPreferredSize(new Dimension(120,20));
JTextField tf3=new JTextField();
tf3.setPreferredSize(new Dimension(120,20));
JTextField tf4=new JTextField();
tf4.setPreferredSize(new Dimension(120,20));
JTextField tf5=new JTextField();
tf5.setPreferredSize(new Dimension(120,20));
JTextField tf6=new JTextField();
tf6.setPreferredSize(new Dimension(120,20));
JTextField tf7=new JTextField();
tf7.setPreferredSize(new Dimension(120,20));
JButton b=new JButton("计算");
b.setPreferredSize(new Dimension(80,30));
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int initMoney,year,each;
float rate;
initMoney=Integer.valueOf(tf1.getText());//获取起始资金
rate=(Integer.valueOf(tf2.getText()));//获取每年收益
year=Integer.valueOf(tf3.getText());//获取复利年数
each=Integer.valueOf(tf4.getText());//每年追加资金
int F1=(year-1)*each+initMoney;//本金和
int F3=(int) (each* fuli( (1+rate/100),(year-1)) + initMoney*p( (1+rate/100) ,year));//本息和
int F2=F3-F1;//利息和
tf5.setText(clauses(F1));
tf6.setText(clauses(F2));
tf7.setText(clauses(F3));
}
});
p1.add(l1);p1.add(l2);p1.add(l3);p1.add(l4);
p2.add(tf1);p2.add(tf2);p2.add(tf3);p2.add(tf4);
p3.add(l5);p3.add(l6);p3.add(l7);p3.add(l8);
p4.add(b);
p5.add(l9);p5.add(l10);p5.add(l11);
p6.add(tf5);p6.add(tf6);p6.add(tf7);
p7.add(l12);p7.add(l13);p7.add(l14);
f.add(p1);f.add(p2);f.add(p3);
f.add(p4);
f.add(p5);f.add(p6);f.add(p7);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
public static float p(float base,int power) {
if(1==power)
return base;
if(0==power)
return 1;
float result = base;
for(int i=0;i0;i--) {
result +=p(rate,i);
}
return result;
}
protected static String clauses(int f1) {
return String.format("%,d", f1);
}
}
设计一个线程,每隔100毫秒,就把进度条的进度+1。
从0%一直加到100%
刚开始加的比较快,以每隔100毫秒的速度增加,随着进度的增加,越加越慢,让处女座的使用者,干着急
变得多慢,根据你们和处女座同学,朋友的相处体验,自己把控。
1、进度条组件
JProgressBar pb=new JProgressBar();
pb.setValue(i);
package gui;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JProgressBar;
public class TestGUI {
public static void main(String[] args) {
JFrame f=new JFrame("LOL");
f.setSize(400,300);
f.setLocation(500,500);
f.setLayout(new FlowLayout(FlowLayout.CENTER,100,20));
JProgressBar pb=new JProgressBar();
pb.setMaximum(100);
Thread t=new Thread() {
public void run() {
try {
for (int i = 0; i <=100; i++) {
if(i<40) {
pb.setValue(i);
Thread.sleep(10);
}
if(i<60&&i>40) {
pb.setValue(i);
Thread.sleep(100);
}
if(i<80&&i>60) {
pb.setValue(i);
Thread.sleep(200);
}
if(i<100&&i>80) {
pb.setValue(i);
Thread.sleep(400);
}
if(i==100) {
pb.setValue(0);
i = 0;
JOptionPane.showMessageDialog(f, "下载失败!点击重试!");
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
JButton b=new JButton("开始下载");
b.addActionListener(e-> t.start());
pb.setStringPainted(true);
f.add(pb);
f.add(b);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
改进练习-复制文件夹提供进度条,把文件复制的进度显示出来。
package gui;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JTextField;
public class TestGUI {
static long allFileSize = 0; // 所有需要复制的文件大小
static long currentFileSizeCopied = 0;// 已复制的文件总大小
public static void main(String[] args) {
JFrame f=new JFrame("复制文件");
f.setSize(400,200);
f.setLocation(500,500);
f.setLayout(null);
JPanel p1=new JPanel();
p1.setBounds(0,0,400,40);
p1.setLayout(new FlowLayout(FlowLayout.CENTER,10,10));
JLabel l1=new JLabel("源文件地址:");
JLabel l2=new JLabel("复制到:");
JTextField tf1=new JTextField();
tf1.setPreferredSize(new Dimension(80,30));
tf1.setText("C:\\迅雷下载");
JTextField tf2=new JTextField();
tf2.setText("C:\\Users\\LH\\Desktop\\迅雷下载");
tf2.setPreferredSize(new Dimension(80,30));
p1.add(l1);
p1.add(tf1);
p1.add(l2);
p1.add(tf2);
JButton b=new JButton("开始复制");
JProgressBar pg=new JProgressBar();
pg.setMaximum(100);
pg.setValue(0);
pg.setStringPainted(true);
JPanel p2=new JPanel();
p2.setBounds(0,40,400,50);
p2.setLayout(new FlowLayout(FlowLayout.CENTER,10,10));
JLabel l3=new JLabel("文件复制进度:");
p2.add(b);
p2.add(l3);
p2.add(pg);
f.add(p1);
f.add(p2);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
calclateAllFilesize(new File(tf1.getText()));
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String A=tf1.getText();
String B=tf2.getText();
new Thread(() -> copyFolder(A,B)).start();
b.setEnabled(false);
}
public void copyFolder(String srcFolder, String destFolder){
File sf = new File(srcFolder);
File df = new File(destFolder);
if (!sf.exists())
return;
if (!sf.isDirectory())
return;
if (df.isFile())
return;
if (!df.exists())
df.mkdirs();
// 创建文件引用集合
File[] f = sf.listFiles();
// 创建文件名称集合
String[] name = sf.list();
// for读出
for(int i=0;i