当前位置: 代码迷 >> Java面试 >> 刚刚改写的swing类程序,编译过了,执行不了,请各位帮忙,附源代码,该怎么解决
  详细解决方案

刚刚改写的swing类程序,编译过了,执行不了,请各位帮忙,附源代码,该怎么解决

热度:84   发布时间:2016-04-17 20:02:49.0
刚刚改写的swing类程序,编译过了,执行不了,请各位帮忙,附源代码
原程序是两个按钮的事件处理,即按BUTTON A标签出现文本XXX,按BUTTON B标签出现文本YYY
现在我改写成三个按钮
但是编译过了,就是不能执行,请各位指点一下 分数不多,只能放20了,在这谢谢了
代码如下

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyFirstWindow extends JFrame implements ActionListener{//ActionListener 监听器
  private static final String MYFIRST="My First Window";
  private static final String CONGRUATULATIONS="Congratulations";
  private static final String THANKS="Thanks for your using";
  private String message=MYFIRST;
  private JPanel counterPanel;
  private JButton counterButton1;
  private JButton counterButton2;
private JButton counterButton3;
  private JLabel counterLabel;
  public MyFirstWindow(){
super("My First Window");
counterLabel=new JLabel(message,SwingConstants.SOUTH);
counterButton1=new JButton("My First Window");
counterButton2=new JButton("Congratulations");
counterButton3=new JButton("Thanks For your using");
counterButton1.addActionListener(this);
counterButton2.addActionListener(this);
counterButton3.addActionListener(this);
 
 
counterPanel.add(counterLabel,BorderLayout.NORTH);
counterPanel.add(counterButton1,BorderLayout.WEST);
counterPanel.add(counterButton2,BorderLayout.CENTER);
counterPanel.add(counterButton3,BorderLayout.EAST);
getContentPane().add(counterPanel);//
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
pack();
setVisible(true);
  }
  public void actionPerformed(ActionEvent e){
if(message.equals(MYFIRST)){
message=MYFIRST;
}

if(message.equals(CONGRUATULATIONS)){

message=CONGRUATULATIONS;
}
if(message.equals(THANKS)){
message=THANKS;
}
counterLabel.setText(message);
  }
public static void main(final String[] args)
{final MyFirstWindow app=new MyFirstWindow();
}
}

 


------解决方案--------------------

//但愿我没改错
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
// implements ActionListener 

public class MyFirstWindow extends JFrame{// ActionListener
// 监听器
private static final String MYFIRST = "My First Window ";

private static final String CONGRUATULATIONS = "Congratulations ";

private static final String THANKS = "Thanks for your using ";

private String message = MYFIRST;

private JPanel counterPanel;

private JButton counterButton1;

private JButton counterButton2;

private JButton counterButton3;

private JLabel counterLabel;

public MyFirstWindow() {
super("My First Window ");
this.setSize(500, 400);
//counterLabel = new JLabel(message, SwingConstants.SOUTH);
counterPanel = new JPanel();

counterLabel = new JLabel(message);
counterButton1 = new JButton(MYFIRST);
counterButton2 = new JButton(CONGRUATULATIONS);
counterButton3 = new JButton(THANKS);

// counterButton1.addActionListener(this);
// 监听器,内部类
counterButton1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {