以下是我的程序代码,其主要意思是在JFrame中加入画板(画板实现了键盘事件),在画板中写了一串字符,当按动键盘上的右键时字符串能移动,另外我在JFrame中加入了MenuBar,当点MenuItem("下移")的时候字符串同样会下移动,可是如果在JFrame中加入一个按钮后,canvas不在具有键盘功能,而MenuItem的下移功能依旧存在,问题出在哪??
下面的代码加入了canvas MenuBar没有加按钮(按钮被我//了)希望高手能解答下。
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class CC extends JFrame implements KeyListener,ActionListener{
DD dd=new DD();
int z=20;
JMenuBar bar=new JMenuBar();
JMenu btn=new JMenu("文件");
JMenuItem i1=new JMenuItem("下移");
JMenuItem i2=new JMenuItem("上移");
JButton btn2=new JButton("我是按钮");
CC(){
super("小测试");
getContentPane().setLayout(new BorderLayout());
getContentPane().add("Center",dd);
bar.add(btn);
btn.add(i1);
btn.add(i2);
getContentPane().add("South",bar);
//getContentPane().add("East",btn2);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
addKeyListener(this);
i1.addActionListener(this);
i2.addActionListener(this);
//btn2.addActionListener(this);}public static void main(String args[]){
CC cc=new CC();} public void keyTyped(KeyEvent e) {
} public void keyPressed(KeyEvent e) {
if (e.getKeyCode()==KeyEvent.VK_RIGHT);{
dd.right();
dd.b=true;
dd.repaint();
}
} public void keyReleased(KeyEvent e) {
} public void actionPerformed(ActionEvent e) {
if(e.getSource()==i1){
dd.b=true;
dd.repaint();}
if(e.getSource()==btn2){
dd.b=true;
dd.y-=15;
dd.repaint();
}
}
}
class DD extends Canvas{
boolean b=true;
String str="我是魏阳";
public int x=100;
public int y=100;
DD(){
setSize(300,300);}
public void right(){
x+=10;}public void paint(Graphics g){
while(b){
g.setColor(Color.YELLOW);
g.drawString(str,x,y);
y+=10;
b=false;
}
}
}
下面的代码加入了canvas MenuBar没有加按钮(按钮被我//了)希望高手能解答下。
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class CC extends JFrame implements KeyListener,ActionListener{
DD dd=new DD();
int z=20;
JMenuBar bar=new JMenuBar();
JMenu btn=new JMenu("文件");
JMenuItem i1=new JMenuItem("下移");
JMenuItem i2=new JMenuItem("上移");
JButton btn2=new JButton("我是按钮");
CC(){
super("小测试");
getContentPane().setLayout(new BorderLayout());
getContentPane().add("Center",dd);
bar.add(btn);
btn.add(i1);
btn.add(i2);
getContentPane().add("South",bar);
//getContentPane().add("East",btn2);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
addKeyListener(this);
i1.addActionListener(this);
i2.addActionListener(this);
//btn2.addActionListener(this);}public static void main(String args[]){
CC cc=new CC();} public void keyTyped(KeyEvent e) {
} public void keyPressed(KeyEvent e) {
if (e.getKeyCode()==KeyEvent.VK_RIGHT);{
dd.right();
dd.b=true;
dd.repaint();
}
} public void keyReleased(KeyEvent e) {
} public void actionPerformed(ActionEvent e) {
if(e.getSource()==i1){
dd.b=true;
dd.repaint();}
if(e.getSource()==btn2){
dd.b=true;
dd.y-=15;
dd.repaint();
}
}
}
class DD extends Canvas{
boolean b=true;
String str="我是魏阳";
public int x=100;
public int y=100;
DD(){
setSize(300,300);}
public void right(){
x+=10;}public void paint(Graphics g){
while(b){
g.setColor(Color.YELLOW);
g.drawString(str,x,y);
y+=10;
b=false;
}
}
}
下面的代码加入了canvas MenuBar没有加按钮(按钮被我//了)希望高手能解答下。
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class CC extends JFrame implements KeyListener,ActionListener{
DD dd=new DD();
int z=20;
JMenuBar bar=new JMenuBar();
JMenu btn=new JMenu("文件");
JMenuItem i1=new JMenuItem("下移");
JMenuItem i2=new JMenuItem("上移");
JButton btn2=new JButton("我是按钮");
CC(){
super("小测试");
getContentPane().setLayout(new BorderLayout());
getContentPane().add("Center",dd);
bar.add(btn);
btn.add(i1);
btn.add(i2);
getContentPane().add("South",bar);
//getContentPane().add("East",btn2);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
addKeyListener(this);
i1.addActionListener(this);
i2.addActionListener(this);
//btn2.addActionListener(this);}public static void main(String args[]){
CC cc=new CC();} public void keyTyped(KeyEvent e) {
} public void keyPressed(KeyEvent e) {
if (e.getKeyCode()==KeyEvent.VK_RIGHT);{
dd.right();
dd.b=true;
dd.repaint();
}
} public void keyReleased(KeyEvent e) {
} public void actionPerformed(ActionEvent e) {
if(e.getSource()==i1){
dd.b=true;
dd.repaint();}
if(e.getSource()==btn2){
dd.b=true;
dd.y-=15;
dd.repaint();
}
}
}
class DD extends Canvas{
boolean b=true;
String str="我是魏阳";
public int x=100;
public int y=100;
DD(){
setSize(300,300);}
public void right(){
x+=10;}public void paint(Graphics g){
while(b){
g.setColor(Color.YELLOW);
g.drawString(str,x,y);
y+=10;
b=false;
}
}
}
addKeyListener(this);
改成
dd.addKeyListener(this);
加上
dd.requestFocus();你最好是把KeyListener放在DD上