为什么我的程序的快捷键没法用,怎么改啊!
code=Java]
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;public class JButtonExample2 extends WindowAdapter implements ActionListener{ JFrame f ;
JPanel p;
JButton b1,b2,b3;
JTextArea ta;
int tag = 0;
//定义变量
public static void main(String[] args){
JButtonExample2 th = new JButtonExample2();
th.go();
}
//执行
public void go(){
f = new JFrame("JButton Text Exmaple2");
b1 = new JButton("Sample");
b1.setMnemonic(KeyEvent.VK_S);
b1.setActionCommand("Sample");
b1.addActionListener(this);
b2 = new JButton("disable");
b2.setMnemonic(KeyEvent.VK_A);
b2.setActionCommand("disable");
b2.addActionListener(this);
b3 = new JButton("enable");
b3.setMnemonic(KeyEvent.VK_E);
b3.setActionCommand("enable");
b3.addActionListener(this);
p = new JPanel();
p.add(b1);
p.add(b2);
p.add(b3);
Container c = f.getContentPane();
c.add(p,"South");
ta = new JTextArea(5,5);
c.add(ta,"Center");
f.addWindowListener(this);
f.setSize(200,300);
f.setVisible(true);
}
public void actionPerformed(ActionEvent e){
String s1 = "you have pressed the key";
String s2 = "You press the key another time";
if(e.getActionCommand()== "Sample"){
if(tag==0){
ta.append(s1);
tag=1;
}
else
{
ta.append(s2);
tag=0;
}
}
if(e.getActionCommand()=="disable"){
b1.setEnabled(false);
b2.setText("Enable test");
b2.setActionCommand("enable");
}
if(e.getActionCommand()=="enable"){
b1.setEnabled(true);
b3.setText("disable test");
b3.setActionCommand("disable");
}
}
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
[/code]
code=Java]
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;public class JButtonExample2 extends WindowAdapter implements ActionListener{ JFrame f ;
JPanel p;
JButton b1,b2,b3;
JTextArea ta;
int tag = 0;
//定义变量
public static void main(String[] args){
JButtonExample2 th = new JButtonExample2();
th.go();
}
//执行
public void go(){
f = new JFrame("JButton Text Exmaple2");
b1 = new JButton("Sample");
b1.setMnemonic(KeyEvent.VK_S);
b1.setActionCommand("Sample");
b1.addActionListener(this);
b2 = new JButton("disable");
b2.setMnemonic(KeyEvent.VK_A);
b2.setActionCommand("disable");
b2.addActionListener(this);
b3 = new JButton("enable");
b3.setMnemonic(KeyEvent.VK_E);
b3.setActionCommand("enable");
b3.addActionListener(this);
p = new JPanel();
p.add(b1);
p.add(b2);
p.add(b3);
Container c = f.getContentPane();
c.add(p,"South");
ta = new JTextArea(5,5);
c.add(ta,"Center");
f.addWindowListener(this);
f.setSize(200,300);
f.setVisible(true);
}
public void actionPerformed(ActionEvent e){
String s1 = "you have pressed the key";
String s2 = "You press the key another time";
if(e.getActionCommand()== "Sample"){
if(tag==0){
ta.append(s1);
tag=1;
}
else
{
ta.append(s2);
tag=0;
}
}
if(e.getActionCommand()=="disable"){
b1.setEnabled(false);
b2.setText("Enable test");
b2.setActionCommand("enable");
}
if(e.getActionCommand()=="enable"){
b1.setEnabled(true);
b3.setText("disable test");
b3.setActionCommand("disable");
}
}
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
[/code]
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货