JAVA设置的按钮监听没有反应
这是代码
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Test extends JFrame implements ActionListener{
JPanel mb1,mb2,mb3;//把需要的组件全部在这里定义
JButton an1,an2;
JLabel bq1,bq2;
JTextField wbk1,wbk2;
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("取消");
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){
}else if(e.getSource()==an2){
System.exit(0);
}
}
}
原来我自己忘记给两个按钮设置监听器了。。。 展开
你还没给按钮添加监听器。
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 < 1 || 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 < rows.length; i++) {
for (int j = 0; j < rows[i].length; 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 < rows.length; i++) {
if(i==0){
for (int j = 0; j < rows[i].length; j++) {
if(j < start){
rows[i][j] = "";
}else{
rows[i][j] = String.valueOf(count+1);
count++;
}
}
}else{
for (int j = 0; j < rows[i].length; j++) {
if(count < max_day_of_month){
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);
}
}
}
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Test extends JFrame implements ActionListener{
JPanel mb1,mb2,mb3;//把需要的组件全部在这里定义
JButton an1,an2;
JLabel bq1,bq2;
JTextField wbk1,wbk2;
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){
Test a=new Test();
}else if(e.getSource()==an2){
System.exit(0);
}
}
}
这样就可以的 。。在an1 的监听上面 写上 弹出一个新的窗口 我给你写的 是弹出你本来的Test 窗口了。。。
if(e.getSource()==an1){
Test a=new Test();
如果是完全新的空白窗口呢?
当然也可以啦。。。。需要吗????
an1.addActionListener(this);
an2.addActionListener(this);