解释以下每行代码confirm.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { int inputyear = Integer.parseInt(inputYear.getText()); if(inputyear<=1900){ JOptionPane.showMessageDialog(null,"请输入正确的年份(1900-2300"); inputYear.setText(null); return; } int inputmonth = Integer.parseInt(inputMonth.getText()); if (inputmonth<=0||inputmonth>=13){ JOptionPane.showMessageDialog(null,"请输入正确的月份(1-12)"); inputMonth.setText(null); return; } for (int i=0;i<42;i++){ dayBtn[i].setEnabled(true); dayBtn[i].setForeground(Color.black); dayBtn[i].setBackground(new Color(240, 239, 239)); } setDay(inputyear,inputmonth); dayText.setText(inputYear.getText()+"/"+inputMonth.getText()+"/"+nowDate.get(Calendar.DAY_OF_MONTH)+" "); } });
时间: 2024-02-15 12:28:39 浏览: 17
这段代码是对一个按钮组件"confirm"添加一个监听器,当用户点击该按钮时,会执行监听器内部重写的actionPerformed方法。在该方法内部,首先通过获取用户输入的年份和月份,判断其是否在合法范围内(年份在1900-2300之间,月份在1-12之间)。如果输入的年份或月份不合法,会通过弹出对话框提示用户重新输入,并清空输入框。
如果输入的年份和月份合法,会遍历一个按钮数组,将所有按钮的状态还原。然后调用setDay方法,传入年份和月份,用于设置日历的日期。最后,将日历日期显示在一个文本框中。
相关问题
import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.Calendar; import java.util.GregorianCalendar; class MyCalendar extends JFrame implements ActionListener { private JPanel p1, p2, p3; //三个面板 p1最上面 p2中间 p3下面 private JLabel yearStr, monthStr; //标签 private JTextField inputYear, inputMonth; private JButton confirm; //确认 private JButton lastMonth; private JButton nextMonth; private JLabel dayText;//文本框 private JLabel TimeText;//文本框 //p2面板里控件的声明 private String[] week = {"日", "一", "二", "三", "四", "五", "六"}; private JLabel[] weekLable = new JLabel[week.length];//数组的声明 //p3面板的42个按钮声明 private JButton[] dayBtn = new JButton[42]; private Calendar nowDate = new GregorianCalendar(); //Calendar是抽象类 new不出 用直接子类 private int nowYear = nowDate.get(Calendar.YEAR); private int nowMonth = nowDate.get(Calendar.MONTH); public MyCalendar() throws HeadlessException { p1 = new JPanel(); p2 = new JPanel(); p3 = new JPanel(); yearStr = new JLabel("Year:"); inputYear = new JTextField(4); monthStr = new JLabel(" Month:"); inputMonth = new JTextField(3); confirm = new JButton("确认"); lastMonth = new JButton("上月"); nextMonth = new JButton("下月"); dayText = new JLabel(); TimeText = new JLabel(); new Thread() { //线程内部类用来实时显示时间 public void run() { while (true) { LocalDateTime dateTime = LocalDateTime.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss"); //大写的HH是24小时制的 String nowTime = dateTime.format(formatter); TimeText.setText(nowTime); try { Thread.sleep(1000); } catch (Inter
ruptedException e) { e.printStackTrace(); } } } }.start(); //启动线程 //设置布局管理器 this.setLayout(new BorderLayout()); p1.setLayout(new FlowLayout(FlowLayout.CENTER)); p2.setLayout(new GridLayout(1, 7)); p3.setLayout(new GridLayout(6, 7)); //给p1面板添加控件 p1.add(yearStr); p1.add(inputYear); p1.add(monthStr); p1.add(inputMonth); p1.add(confirm); p1.add(lastMonth); p1.add(nextMonth); p1.add(TimeText); //给p2面板添加控件 for (int i = 0; i < week.length; i++) { weekLable[i] = new JLabel(week[i], SwingConstants.CENTER); p2.add(weekLable[i]); } //给p3面板添加控件 for (int i = 0; i < dayBtn.length; i++) { dayBtn[i] = new JButton(""); dayBtn[i].setContentAreaFilled(false);//将按钮设为透明 p3.add(dayBtn[i]); } confirm.addActionListener(this); lastMonth.addActionListener(this); nextMonth.addActionListener(this); //设置窗口基本属性 this.add(p1, BorderLayout.NORTH); this.add(p2, BorderLayout.CENTER); this.add(p3, BorderLayout.SOUTH); this.setLocation(100, 100); this.setSize(400, 300); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); //初始化 init(); } private void init() { nowDate.set(Calendar.DAY_OF_MONTH, 1); showCalendar(); } @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == confirm) { int year = Integer.parseInt(inputYear.getText().trim()); int month = Integer.parseInt(inputMonth.getText().trim()); nowDate.set(Calendar.YEAR, year); nowDate.set(Calendar.MONTH, month - 1); init(); } else if (e.getSource() == lastMonth) { nowDate.add(Calendar.MONTH, -1); init(); } else if (e.getSource() == nextMonth) { nowDate.add(Calendar.MONTH, 1); init(); } else { for (int i = 0; i < dayBtn.length; i++) { if (e.getSource() == dayBtn[i]) { String dayStr = dayBtn[i].getText().trim(); if (!dayStr.equals("")) { JOptionPane.showMessageDialog(null, "您选择的日期是:" + nowYear + "年" + (nowMonth + 1) + "月" + dayStr + "日"); } break; } } } } private void showCalendar() { nowYear = nowDate.get(Calendar.YEAR); nowMonth = nowDate.get(Calendar.MONTH); inputYear.setText(String.valueOf(nowYear)); inputMonth.setText(String.valueOf(nowMonth + 1)); int maxDay = nowDate.getActualMaximum(Calendar.DAY_OF_MONTH);//获取当月最大天数 int weekNo = nowDate.get(Calendar.DAY_OF_WEEK) - 1;//获取当月第一天是星期几 for (int i = 0; i < dayBtn.length; i++) { dayBtn[i].setText(""); } for (int i = weekNo, n = 1; n <= maxDay; i++, n++) { dayBtn[i].setText(String.valueOf(n)); } dayText.setText(nowYear + "年" + (nowMonth + 1) + "月"); } } public class Main { public static void main(String[] args) { new MyCalendar(); } }
这是一个Java GUI程序,是一个日历软件,实现了对日历的显示和时间的实时更新。用户可以选择日期,程序会弹出对话框,显示用户选择的日期。程序的界面比较简单,分为三个面板,p1面板上放置了输入年月和确认按钮,p2面板上放置了星期几,p3面板上放置了日期。程序主要是通过Calendar类和JButton、JLabel等Swing组件实现的。
生成一个完整的实现登录功能的界面代码,包含有登录、注册、忘记密码等功能,当用户输入正确的用户名和密码后可以成功登录并跳转到一个新的空页面,并可设置图片背景,用Java语言写
以下是一个简单的Java Swing界面代码,实现了登录、注册、忘记密码等功能,同时可以设置图片背景:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class LoginFrame extends JFrame {
private JTextField usernameField;
private JPasswordField passwordField;
private JButton loginButton;
private JButton registerButton;
private JButton forgetButton;
private JLabel backgroundImageLabel;
public LoginFrame() {
setTitle("登录");
setSize(500, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
// 背景图片
ImageIcon backgroundImage = new ImageIcon("background.jpg");
Image image = backgroundImage.getImage();
Image scaledImage = image.getScaledInstance(500, 300, Image.SCALE_SMOOTH);
backgroundImage = new ImageIcon(scaledImage);
backgroundImageLabel = new JLabel(backgroundImage);
backgroundImageLabel.setBounds(0, 0, 500, 300);
add(backgroundImageLabel);
// 用户名
JLabel usernameLabel = new JLabel("用户名:");
usernameLabel.setBounds(100, 50, 100, 30);
backgroundImageLabel.add(usernameLabel);
usernameField = new JTextField();
usernameField.setBounds(200, 50, 200, 30);
backgroundImageLabel.add(usernameField);
// 密码
JLabel passwordLabel = new JLabel("密码:");
passwordLabel.setBounds(100, 100, 100, 30);
backgroundImageLabel.add(passwordLabel);
passwordField = new JPasswordField();
passwordField.setBounds(200, 100, 200, 30);
backgroundImageLabel.add(passwordField);
// 登录按钮
loginButton = new JButton("登录");
loginButton.setBounds(100, 150, 80, 30);
backgroundImageLabel.add(loginButton);
loginButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String username = usernameField.getText();
String password = String.valueOf(passwordField.getPassword());
if (checkUser(username, password)) {
// 登录成功,跳转到新页面
dispose(); // 关闭当前窗口
JFrame frame = new JFrame();
frame.setTitle("新页面");
frame.setSize(500, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
// 新页面背景图片
ImageIcon backgroundImage = new ImageIcon("background2.jpg");
Image image = backgroundImage.getImage();
Image scaledImage = image.getScaledInstance(500, 300, Image.SCALE_SMOOTH);
backgroundImage = new ImageIcon(scaledImage);
JLabel backgroundImageLabel = new JLabel(backgroundImage);
backgroundImageLabel.setBounds(0, 0, 500, 300);
frame.add(backgroundImageLabel);
frame.setVisible(true);
} else {
// 登录失败
JOptionPane.showMessageDialog(LoginFrame.this, "用户名或密码错误!", "错误", JOptionPane.ERROR_MESSAGE);
}
}
});
// 注册按钮
registerButton = new JButton("注册");
registerButton.setBounds(200, 150, 80, 30);
backgroundImageLabel.add(registerButton);
registerButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 打开注册窗口
RegisterFrame registerFrame = new RegisterFrame();
registerFrame.setVisible(true);
}
});
// 忘记密码按钮
forgetButton = new JButton("忘记密码");
forgetButton.setBounds(300, 150, 100, 30);
backgroundImageLabel.add(forgetButton);
forgetButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 打开忘记密码窗口
ForgetFrame forgetFrame = new ForgetFrame();
forgetFrame.setVisible(true);
}
});
}
// 检查用户名和密码是否正确
private boolean checkUser(String username, String password) {
// TODO: 实现用户名和密码的验证逻辑
return true;
}
public static void main(String[] args) {
LoginFrame loginFrame = new LoginFrame();
loginFrame.setVisible(true);
}
}
// 注册窗口
class RegisterFrame extends JFrame {
public RegisterFrame() {
setTitle("注册");
setSize(300, 200);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setResizable(false);
JPanel panel = new JPanel();
panel.setLayout(null);
add(panel);
// 用户名
JLabel usernameLabel = new JLabel("用户名:");
usernameLabel.setBounds(30, 30, 60, 30);
panel.add(usernameLabel);
JTextField usernameField = new JTextField();
usernameField.setBounds(100, 30, 150, 30);
panel.add(usernameField);
// 密码
JLabel passwordLabel = new JLabel("密码:");
passwordLabel.setBounds(30, 70, 60, 30);
panel.add(passwordLabel);
JPasswordField passwordField = new JPasswordField();
passwordField.setBounds(100, 70, 150, 30);
panel.add(passwordField);
// 确认密码
JLabel confirmPasswordLabel = new JLabel("确认密码:");
confirmPasswordLabel.setBounds(10, 110, 80, 30);
panel.add(confirmPasswordLabel);
JPasswordField confirmPasswordField = new JPasswordField();
confirmPasswordField.setBounds(100, 110, 150, 30);
panel.add(confirmPasswordField);
// 提交按钮
JButton submitButton = new JButton("提交");
submitButton.setBounds(100, 150, 80, 30);
panel.add(submitButton);
submitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String username = usernameField.getText();
String password = String.valueOf(passwordField.getPassword());
String confirmPassword = String.valueOf(confirmPasswordField.getPassword());
if (username.isEmpty() || password.isEmpty() || confirmPassword.isEmpty()) {
JOptionPane.showMessageDialog(RegisterFrame.this, "请填写完整!", "错误", JOptionPane.ERROR_MESSAGE);
} else if (!password.equals(confirmPassword)) {
JOptionPane.showMessageDialog(RegisterFrame.this, "两次输入的密码不一致!", "错误", JOptionPane.ERROR_MESSAGE);
} else {
// TODO: 实现注册逻辑
JOptionPane.showMessageDialog(RegisterFrame.this, "注册成功!", "提示", JOptionPane.INFORMATION_MESSAGE);
dispose(); // 关闭当前窗口
}
}
});
}
}
// 忘记密码窗口
class ForgetFrame extends JFrame {
public ForgetFrame() {
setTitle("忘记密码");
setSize(300, 150);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setResizable(false);
JPanel panel = new JPanel();
panel.setLayout(null);
add(panel);
// 用户名
JLabel usernameLabel = new JLabel("用户名:");
usernameLabel.setBounds(30, 30, 60, 30);
panel.add(usernameLabel);
JTextField usernameField = new JTextField();
usernameField.setBounds(100, 30, 150, 30);
panel.add(usernameField);
// 提交按钮
JButton submitButton = new JButton("提交");
submitButton.setBounds(100, 80, 80, 30);
panel.add(submitButton);
submitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String username = usernameField.getText();
if (username.isEmpty()) {
JOptionPane.showMessageDialog(ForgetFrame.this, "请填写用户名!", "错误", JOptionPane.ERROR_MESSAGE);
} else {
// TODO: 实现忘记密码逻辑
JOptionPane.showMessageDialog(ForgetFrame.this, "我们已经将重置密码的链接发送到您的邮箱,请查收!", "提示", JOptionPane.INFORMATION_MESSAGE);
dispose(); // 关闭当前窗口
}
}
});
}
}
```
需要注意的是,这里只是一个简单的示例代码,实际应用中还需要进行更严格的输入验证和安全保护,以确保用户信息不被非法获取或篡改。