请大家看看这个程序有什么问题,该如何改?

来源:百度知道 编辑:UC知道 时间:2024/06/05 11:09:57
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Test{
public static void main(String[] args){
try{
UIManager.setLookAndFeel(
UIManager.getCrossPlatformLookAndFeelClassName());
}catch(Exception e){System.out.println("Caught exception");}

private static String labelPrefix = "number of button clicks:";
private int numClicks = 0;
final JLabel label = new JLabel(labelPrefix + "0 ");
JButton button = new JButton("i'm a swing button");
button.setMnemonic('i');
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
numClicks++;
label.setText(labelPrefix + numClicks);
}
});

label.setLabelFor(button);
}
}

我不是很清楚你要实现什么功能,只能大体猜测,修改的代码如下:
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;

public class Test2 extends JFrame{
private static String labelPrefix = "number of button clicks:";
private int numClicks = 0;
final JLabel label = new JLabel(labelPrefix + "0 ");
JButton button = new JButton("i'm a swing button");

public Test2() {
init();
setLayout(new FlowLayout());
setSize(300,300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void init() {
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createLineBorder(Col