import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class keyDemo implements KeyListener{
JFrame frame=new JFrame("移动按钮");
JButton button;
int x,y,x1,y1;
public keyDemo(){
Container con=frame.getContentPane();
con.setLayout(null);
button=new JButton(" ");
button.setBackground(Color.red);
button.setBounds(20,30,100,150);
con.add(button);
frame.addKeyListener(this);
frame.setBounds(300,300,500,600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} public void keyPressed(KeyEvent e){
x=100;
y=150;
if(e.getKeyCode()==KeyEvent.VK_UP){
y+=2;
button.setBounds(20,30,x,y);
button.setText("上");
}
else if(e.getKeyCode()==KeyEvent.VK_DOWN){
x-=2;
button.setBounds(20,30,x,y);
button.setText("下");
}
else if(e.getKeyCode()==KeyEvent.VK_LEFT){
y-=2;
button.setBounds(20,30,x,y);
button.setText("左");
}
else if(e.getKeyCode()==KeyEvent.VK_RIGHT){
y+=2;
button.setBounds(20,30,x,y);
button.setText("右");
}
}
public void keyTyped(KeyEvent e2){
x1=button.getX();
y1=button.getY();
if(e2.getKeyCode()==x){
x1+=5;
con.add(new JButton().setBounds(5,5,x1,y1));
}
}
public void keyReleased(KeyEvent e3){}
public static void main(String arg[]){
new keyDemo();
}
}
import java.awt.event.*;
import javax.swing.*;public class keyDemo implements KeyListener{
JFrame frame=new JFrame("移动按钮");
JButton button;
int x,y,x1,y1;
public keyDemo(){
Container con=frame.getContentPane();
con.setLayout(null);
button=new JButton(" ");
button.setBackground(Color.red);
button.setBounds(20,30,100,150);
con.add(button);
frame.addKeyListener(this);
frame.setBounds(300,300,500,600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} public void keyPressed(KeyEvent e){
x=100;
y=150;
if(e.getKeyCode()==KeyEvent.VK_UP){
y+=2;
button.setBounds(20,30,x,y);
button.setText("上");
}
else if(e.getKeyCode()==KeyEvent.VK_DOWN){
x-=2;
button.setBounds(20,30,x,y);
button.setText("下");
}
else if(e.getKeyCode()==KeyEvent.VK_LEFT){
y-=2;
button.setBounds(20,30,x,y);
button.setText("左");
}
else if(e.getKeyCode()==KeyEvent.VK_RIGHT){
y+=2;
button.setBounds(20,30,x,y);
button.setText("右");
}
}
public void keyTyped(KeyEvent e2){
x1=button.getX();
y1=button.getY();
if(e2.getKeyCode()==x){
x1+=5;
con.add(new JButton().setBounds(5,5,x1,y1));
}
}
public void keyReleased(KeyEvent e3){}
public static void main(String arg[]){
new keyDemo();
}
}
用面板对象接收且记得转型!而且全放在构造里面也不合适,还是做个普通方法。
//下面前置声明你的控件(PS:搭积木了,先把积木摆出来吧。)
JLabel label1,label2;
JFrame frame;
//下面是你的构造;
public mouseDemo(){
//构造里面就做一件事情,调用你的搭积木方法。
try{
搭积木方法名();
}catch(Exception ex){
ex.printStackTrace();
}
}
//搭积木方法
public void 搭积木方法名()throws Exception{
//在这里面写你的逻辑,实例化、布局等等
}class mouse....//内部类该怎么写还怎么写
1 Container con=frame.getContentPane();
既然把con作为全局的变量,就要在开头定义好,可以把这句代码改放在开头定义中。
2 con.add(new JButton().setBounds(5,5,x1,y1));
comp.setbound返回的不是comp(组件),因此应该把按钮的定义和设置边框放在外面,即:
JButton jbtn=new JButton();
jbtn.setBounds(5,5,x1,y1);
con.add(jbtn);
frame.add(jbtn); 3 运行时无法响应监听事件:
我将frame.addKeyListener(this); 做了改动
改为:button.addKeyListener(this); //按纽的键盘监听事件
或者: frame.setFocusable(true);
frame.addKeyListener(this); //将窗体设置为焦点才能获取监听
1 Container con=frame.getContentPane();
既然把con作为全局的变量,就要在开头定义好,可以把这句代码改放在开头定义中。
2 con.add(new JButton().setBounds(5,5,x1,y1));
comp.setbound返回的不是comp(组件),因此应该把按钮的定义和设置边框放在外面,即:
JButton jbtn=new JButton();
jbtn.setBounds(5,5,x1,y1);
con.add(jbtn);
frame.add(jbtn); 3 运行时无法响应监听事件:
我将frame.addKeyListener(this); 做了改动
改为:button.addKeyListener(this); //按纽的键盘监听事件
或者: frame.setFocusable(true);
frame.addKeyListener(this); //将窗体设置为焦点才能获取监听
import java.awt.*;
import java.awt.event.*;
import javax.swing.*; public class keyDemo implements KeyListener{
JFrame frame=new JFrame("移动按钮");
JButton button;
JButton b=new JButton();
int x,y,x1,y1;
Container con=frame.getContentPane();
public keyDemo(){
con.setLayout(null);
button=new JButton(" ");
button.setBackground(Color.red);
button.setBounds(20,30,100,150);
b.setBounds(5,5,x1,y1);
con.add(button);
frame.addKeyListener(this);
frame.setBounds(300,300,500,600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
} public void keyPressed(KeyEvent e){
x=100;
y=150;
if(e.getKeyCode()==KeyEvent.VK_UP){
y+=2;
button.setBounds(20,30,x,y);
button.setText("上");
}
else
if(e.getKeyCode()==KeyEvent.VK_DOWN){
x-=2;
button.setBounds(20,30,x,y);
button.setText("下");
} else
if(e.getKeyCode()==KeyEvent.VK_LEFT){
y-=2;
button.setBounds(20,30,x,y);
button.setText("左");
} else
if(e.getKeyCode()==KeyEvent.VK_RIGHT){
y+=2;
button.setBounds(20,30,x,y);
button.setText("右");
}
}
public void keyTyped(KeyEvent e2){
x1=button.getX();
y1=button.getY();
if(e2.getKeyCode()==x){
x1+=5;
con.add( b );
}
}
public void keyReleased(KeyEvent e3){} public static void main(String arg[]){
new keyDemo();
}
}
修改为
con.add(b);
JButton b=new JButton();
b.setBounds(5,5,x1,y1);
new JButton().setBounds(5,5,x1,y1)返回为void类型,con.add( void类型)错误
public void setBounds(....)