import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MyFrame { MyFrame(){
JFrame frame=new JFrame();
frame.setSize(200,200);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c=frame.getContentPane();
c.setLayout(new FlowLayout());
JButton button=new JButton("I'm a Swing button!");
button.setMnemonic('i');
c.add(button);
final String labelPrefix="Number of button clicks: ";
private static int numClicks=0;
final JLabel label=new JLabel(labelPrefix+"0");
label.setLabelFor(button);
label.setText(labelPrefix+numClicks);
c.add(label);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
numClicks++;
label.setText(labelPrefix+numClicks);
}
});
}
public static void main (String[] args) {
try{
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
}catch(Exception e){}
new MyFrame();
}
编译器说 private static int numClicks=0; 非法表达式的开始,不知道什么原因,请高人指点!
import java.awt.*;
import java.awt.event.*;
public class MyFrame { MyFrame(){
JFrame frame=new JFrame();
frame.setSize(200,200);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c=frame.getContentPane();
c.setLayout(new FlowLayout());
JButton button=new JButton("I'm a Swing button!");
button.setMnemonic('i');
c.add(button);
final String labelPrefix="Number of button clicks: ";
private static int numClicks=0;
final JLabel label=new JLabel(labelPrefix+"0");
label.setLabelFor(button);
label.setText(labelPrefix+numClicks);
c.add(label);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
numClicks++;
label.setText(labelPrefix+numClicks);
}
});
}
public static void main (String[] args) {
try{
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
}catch(Exception e){}
new MyFrame();
}
编译器说 private static int numClicks=0; 非法表达式的开始,不知道什么原因,请高人指点!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货