JAVA设置按钮无效_JAVA设置的按钮监听没有反应

展开全部

你还没给按钮添加监听器。62616964757a686964616fe59b9ee7ad9431333332643337

an1 = new JButton("确定");// 添加按钮

an2 = new JButton("取消");

你在上面两句话下面添加下面两句话试试:

an1.addActionListener(this);

an2.addActionListener(this);import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.JTextField;

public class Test extends JFrame implements ActionListener {

private static final long serialVersionUID = 1L;

private JPanel mb1, mb2, mb3;// 把需要的组件全部在这里定义

private JButton an1, an2;

private JLabel bq1, bq2;

private JTextField wbk1, wbk2;

private CalendarFrame calendarFrame;

public static void main(String[] args) {

Test a = new Test();// 主函数只需调用即可

}

public Test() {// 把初始化的全部工作放到构造函数中完成。包括设置大小、标题、位置等等

mb1 = new JPanel();// 添加面板

mb2 = new JPanel();

mb3 = new JPanel();

bq1 = new JLabel("年份"); // 添加标签

bq2 = new JLabel("月份");

an1 = new JButton("确定");// 添加按钮

an2 = new JButton("取消");

an1.addActionListener(this);

an2.addActionListener(this);

wbk1 = new JTextField(10);// 添加文本框,长度

wbk2 = new JTextField(10);//

this.setLayout(new GridLayout(3, 1));// 布局管理器;3行1列

mb1.add(bq1);

mb1.add(wbk1);// 按面板添加相对应的组件

mb2.add(bq2);

mb2.add(wbk2);

mb3.add(an1);

mb3.add(an2);

this.add(mb1);

this.add(mb2);

this.add(mb3);

this.setTitle("日历查询器");// 界面标题

this.setSize(230, 150);// 容器大小

this.setLocation(300, 280);// 容器距离

this.setResizable(false);// 不能调整界面大小

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 退出程序

this.setVisible(true);// 显示界面

}

public void actionPerformed(ActionEvent e) {

if (e.getSource() == an1) {

showCalendar();

} else if (e.getSource() == an2) {

System.exit(0);

}

}

private void showCalendar(){

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");

String str_year = wbk1.getText().trim();

String str_month = wbk2.getText().trim();

if(str_year.equals("") || str_month.equals("")){

JOptionPane.showMessageDialog(this,"不能为空","错误",JOptionPane.ERROR_MESSAGE);

return;

}

try {

Integer.parseInt(str_year);

int month = Integer.parseInt(str_month);

if(month 12){

JOptionPane.showMessageDialog(this,"日期输入错误","错误",JOptionPane.ERROR_MESSAGE);

return;

}

Date date = sdf.parse(str_year+"-"+str_month);

Calendar c = Calendar.getInstance();

c.setTime(date);

System.out.println(c.getTime());

if(calendarFrame == null){

calendarFrame = new CalendarFrame();

calendarFrame.showCalendar(c);

}else{

calendarFrame.setVisible(true);

calendarFrame.showCalendar(c);

}

} catch (Exception e) {

JOptionPane.showMessageDialog(this,"日期输入错误","错误",JOptionPane.ERROR_MESSAGE);

}

}

}

import java.awt.BorderLayout;

import java.util.Calendar;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.JScrollPane;

import javax.swing.JTable;

import javax.swing.table.DefaultTableModel;

public class CalendarFrame extends JFrame{

private static final long serialVersionUID = 1L;

private String[][] rows;

private JPanel main;

private JLabel jlabel_date;

private JScrollPane jsp_show;

private JTable jt_show;

private DefaultTableModel tableModel;

public CalendarFrame() {

super();

initGUI();

setResizable(false);

setTitle("日历");

setBounds(10, 10, 600, 400);

setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

setVisible(true);

}

private void initGUI(){

main = new JPanel();

main.setLayout(new BorderLayout());

jlabel_date = new JLabel();

main.add(jlabel_date,BorderLayout.NORTH);

rows = new String[6][7];

for (int i = 0; i 

for (int j = 0; j 

rows[i][j] = "";

}

}

tableModel = new DefaultTableModel(

rows,

new String[]{"星期日","星期一","星期二","星期三","星期四","星期五","星期六"}

){

private static final long serialVersionUID = 1L;

@Override

public boolean isCellEditable(int row, int column) {

return false;

}

};

jt_show = new JTable();

jt_show.setModel(tableModel);

tableModel.removeRow(0);

jsp_show = new JScrollPane();

jsp_show.setViewportView(jt_show);

main.add(jsp_show,BorderLayout.CENTER);

this.add(main);

}

public synchronized void showCalendar(Calendar c){

try {

int day_of_week = c.get(Calendar.DAY_OF_WEEK);

int max_day_of_month = c.getMaximum(Calendar.DAY_OF_MONTH);

int year = c.get(Calendar.YEAR);

int month = c.get(Calendar.MONTH);

jlabel_date.setText(year + "年"+(month + 1)+"月");

System.out.println(day_of_week);

System.out.println(max_day_of_month);

int start = 0;

int count = 0;

start = day_of_week - 1;

for (int i = 0; i 

if(i==0){

for (int j = 0; j 

if(j 

rows[i][j] = "";

}else{

rows[i][j] = String.valueOf(count+1);

count++;

}

}

}else{

for (int j = 0; j 

if(count 

rows[i][j] = String.valueOf(count+1);

count++;

}else{

rows[i][j] = "";

}

}

}

}

tableModel.setDataVector(rows, new String[]{"星期日","星期一","星期二","星期三","星期四","星期五","星期六"});

jt_show.updateUI();

} catch (Exception e) {

JOptionPane.showMessageDialog(this,"未知错误","错误",JOptionPane.ERROR_MESSAGE);

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值