十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
哈哈~网上很多哈,GUI我也不会,现学现卖一个
目前创新互联已为1000多家的企业提供了网站建设、域名、雅安服务器托管、网站托管运营、企业网站设计、扎赉诺尔网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。
package swing;
import javafx.embed.swing.JFXPanel;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @author wenxy
* @create 2020-05-01
*/
public class JavaFxDate {
public static void main(String[] args) {
// 创建 JFrame 实例
JFrame frame = new JFrame();
// Setting the width and height of frame
frame.setSize(310, 180);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/* 创建面板,这个类似于 HTML 的 div 标签
* 我们可以创建多个面板并在 JFrame 中指定位置
* 面板中我们可以添加文本字段,按钮及其他组件。
*/
JPanel panel = new JPanel();
// 添加面板
frame.add(panel);
/*
* 调用用户定义的方法并添加组件到面板
*/
placeComponents(panel);
// 设置界面可见
frame.setVisible(true);
}
private static void placeComponents(JPanel panel) {
/* 布局部分我们这边不多做介绍
* 这边设置布局为 null
*/
panel.setLayout(null);
// 创建 JLabel
JLabel userLabel = new JLabel("请输入日期字符串");
userLabel.setBounds(5, 5, 300, 25);
panel.add(userLabel);
/*
* 创建文本域用于用户输入
*/
JTextField userText = new JTextField(20);
userText.setBounds(5, 40, 200, 25);
panel.add(userText);
// 创建 JLabel
JLabel showLable = new JLabel();
showLable.setBounds(5, 70, 300, 25);
panel.add(showLable);
// 创建登录按钮
JButton loginButton = new JButton("转换");
loginButton.setBounds(180, 40, 100, 25);
loginButton.addActionListener(new ActionListener() {
DateFormat input = new SimpleDateFormat("yyyy-MM-dd");
DateFormat output = new SimpleDateFormat("yyyy年MM月dd日");
{
input.setLenient(false); // 设置严格按格式匹配
output.setLenient(false);
}
@Override
public void actionPerformed(ActionEvent actionEvent) {
try {
Date date = convert(userText.getText());
showLable.setText("成功:" + output.format(date));
showLable.setForeground(Color.GREEN);
} catch (WrongDateException e) {
showLable.setText(e.getMessage());
showLable.setForeground(Color.RED);
}
}
private Date convert(String text) throws WrongDateException {
try {
return input.parse(text);
} catch (ParseException e) {
throw new WrongDateException(text);
}
}
});
panel.add(loginButton);
}
static class WrongDateException extends Exception {
WrongDateException(String s) {
super(s + "不是合法的日期字符串");
}
}
}
Employee .java
public class Employee {
private int empId;
private String empName;
private String empsex;
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public String getEmpsex() {
return empsex;
}
public void setEmpsex(String empsex) {
this.empsex = empsex;
}
}
PartTimeEmployee.java
public class PartTimeEmployee extends Employee{
private double hourlyPay;
public double getHourlyPay() {
return hourlyPay;
}
public void setHourlyPay(double hourlyPay) {
this.hourlyPay = hourlyPay;
}
}
测试类Test.java
public class Test{
public static void main(String[] args) {
PartTimeEmployee pe=new PartTimeEmployee();
pe.setEmpId(1);
pe.setEmpName("莉莉");
pe.setEmpsex("女");
pe.setHourlyPay(50.0);
System.out.println("雇员编号:"+pe.getEmpId());
System.out.println("雇员姓名:"+pe.getEmpName());
System.out.println("雇员性别:"+pe.getEmpsex());
System.out.println("每小时工资:"+pe.getHourlyPay());
}
}
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import java.text.*;
public class Win extends JFrame implements Runnable
{
public static void main(String[] args)
{
new Win();
}
Win()
{
setLayout(new GridLayout(2,2));
add(new JLabel("当前时间:"));
add(timetxt=new JTextField(25));
add(new JLabel("当前数字:"));
add(numtxt=new JTextField(25));
// setSize(400,300);
// setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setResizable(false);
setLocationRelativeTo(null);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
exit=true;
dispose();
System.exit(0);
}
});
new Thread(this).start();
new Thread(new Runnable()
{
public void run()
{
while(!exit)
{
try
{
Thread.sleep(2000);
n++;
numtxt.setText(String.valueOf(n));
n%=10;
}
catch(Exception ex)
{
}
}
}
private int n=0;
}).start();
setVisible(true);
}
public void run()
{
while(!exit)
{
try
{
timetxt.setText(sdf.format(new Date()));
Thread.sleep(1000);
}
catch(Exception ex)
{
}
}
}
private boolean exit=false;
private JTextField timetxt,numtxt;
private final SimpleDateFormat sdf=new SimpleDateFormat("H:m:s");
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class PainterPanel extends JPanel implements MouseListener{
int shape=-1; //图案类型
Point[] point=new Point[2]; //记录鼠标拖动的起始点和终点
public PainterPanel(){
super(); //调用父类构造函数
this.setBackground(Color.white); //设置背景颜色
point[0]=new Point(-1,-1); //初始化变量
point[1]=new Point(-1,-1);
addMouseListener(this); //增加鼠标事件
}
public void mouseReleased(MouseEvent e){ //鼠标释放事件
point[1]=new Point(e.getX(),e.getY()); //设置终点位置
repaint(); //重绘屏幕
}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void mousePressed(MouseEvent e){ //鼠标按下时事件
point[0]=new Point(e.getX(),e.getY()); //设置起始位置
}
public void paint(Graphics g){
super.paint(g);
switch (shape){ //根据shape绘制图形
case 0:
g.drawLine(point[0].x,point[0].y,point[1].x,point[1].y); //绘线
break;
case 1:
int width=point[1].x-point[0].x;
int height=point[1].y-point[0].y;
g.drawOval(point[0].x,point[0].y,width,height); //绘椭圆
break;
case 2:
width=point[1].x-point[0].x;
height=point[1].y-point[0].y;
g.drawRect(point[0].x,point[0].y,width,height); //绘矩形
break;
}
}
public void drawShape(int shape){
this.shape=shape;
}
}