当前位置: 代码迷 >> Java相关 >> [求助]按钮控制文本移动问题
  详细解决方案

[求助]按钮控制文本移动问题

热度:179   发布时间:2007-06-25 20:18:14.0
[求助]按钮控制文本移动问题

以下是我写的小程序,我的意图是想通过单击按纽,使文本移动,可是不知道为什么文本只能移动一次,而且按上文本框却向上,按下却向上,我仔细检查了好几遍,没发现错误,没办法,只好来求助啦~~拜托各位啦~~~

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class buttonDemo extends JFrame implements ActionListener{
private JButton btnLeft=new JButton("左");
private JButton btnRight=new JButton("右");
private JButton btnUp=new JButton("上");
private JButton btnDown=new JButton("下");

private JTextField tfMove=new JTextField("猪头子园");

private JPanel movePanel=new JPanel();

private Container container=getContentPane();

public buttonDemo(){
super("我爱子园");
setSize(400,300);
setLocation(300,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

btnLeft.addActionListener(this);
btnLeft.setToolTipText("控制子园向左移动");

btnRight.addActionListener(this);
btnRight.setToolTipText("控制子园向右移动");

btnUp.addActionListener(this);
btnUp.setToolTipText("控制子园向上移动");

btnDown.addActionListener(this);
btnDown.setToolTipText("控制文本向下移动");
}

public void setLayout(){
container.setLayout(new BorderLayout());
container.add(btnLeft,BorderLayout.WEST);
container.add(btnRight,BorderLayout.EAST);
container.add(btnUp,BorderLayout.NORTH);
container.add(btnDown,BorderLayout.SOUTH);

// 设置移动面板的布局为null
movePanel.setLayout(null);
movePanel.add(tfMove);

//设置文本的显示位置
int x=50;
int y=50;

tfMove.setBounds(x,y,50,50);
container.add(movePanel,BorderLayout.CENTER);
}

public void actionPerformed(ActionEvent e){


int x=50;
int y=50;


if(e.getSource()==btnRight){
x=x+10;
}
if(e.getSource()==btnLeft){
x=x-10;
}
if(e.getSource()==btnUp){
y=y+10;
}
if(e.getSource()==btnDown){
y=y-10;
}
//重新调整文本框的位置
tfMove.setBounds(x,y,50,50);
}

public static void main(String []args){
buttonDemo frame=new buttonDemo();
frame.setLayout();
frame.show();
}
}

搜索更多相关的解决方案: 文本  按钮  

----------------解决方案--------------------------------------------------------
回复:(弑神饕餮)[求助]按钮控制文本移动问题

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class buttonDemo extends JFrame implements ActionListener{
private JButton btnLeft=new JButton("左");
private JButton btnRight=new JButton("右");
private JButton btnUp=new JButton("上");
private JButton btnDown=new JButton("下");

private JTextField tfMove=new JTextField("猪头子园");

private JPanel movePanel=new JPanel();

private Container container=getContentPane();

public buttonDemo(){
super("我爱子园");
setSize(400,300);
setLocation(300,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

btnLeft.addActionListener(this);
btnLeft.setToolTipText("控制子园向左移动");

btnRight.addActionListener(this);
btnRight.setToolTipText("控制子园向右移动");

btnUp.addActionListener(this);
btnUp.setToolTipText("控制子园向上移动");

btnDown.addActionListener(this);
btnDown.setToolTipText("控制文本向下移动");
}

public void setLayout(){
container.setLayout(new BorderLayout());
container.add(btnLeft,BorderLayout.WEST);
container.add(btnRight,BorderLayout.EAST);
container.add(btnUp,BorderLayout.NORTH);
container.add(btnDown,BorderLayout.SOUTH);

// 设置移动面板的布局为null
movePanel.setLayout(null);
movePanel.add(tfMove);

//设置文本的显示位置
int x=50;
int y=50;

tfMove.setBounds(x,y,50,50);
container.add(movePanel,BorderLayout.CENTER);
}

public void actionPerformed(ActionEvent e){


int x=50;//每次调用这个方法的时候X,Y都重新赋值成了50 其实每次都移动了@.@ 只是看上去没移动而已
int y=50;


if(e.getSource()==btnRight){
x=x+10;
}
if(e.getSource()==btnLeft){
x=x-10;
}
if(e.getSource()==btnUp){
y=y+10;
}
if(e.getSource()==btnDown){
y=y-10;//你把上下换一下不就好拉
}
//重新调整文本框的位置
tfMove.setBounds(x,y,50,50);
}

public static void main(String []args){
buttonDemo frame=new buttonDemo();
frame.setLayout();
frame.show();
}
}


----------------解决方案--------------------------------------------------------
哈哈,你+10,当然向下了,坐标是越向下越多。减10当然就向上了。
----------------解决方案--------------------------------------------------------

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class buttonDemo extends JFrame implements ActionListener{
private JButton btnLeft=new JButton("左");
private JButton btnRight=new JButton("右");
private JButton btnUp=new JButton("上");
private JButton btnDown=new JButton("下");

private JTextField tfMove=new JTextField("猪头子园");

private JPanel movePanel=new JPanel();

private Container container=getContentPane();
private int x=50;
private int y=50;

public buttonDemo(){
super("我爱子园");
setSize(400,300);
setLocation(300,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

btnLeft.addActionListener(this);
btnLeft.setToolTipText("控制子园向左移动");

btnRight.addActionListener(this);
btnRight.setToolTipText("控制子园向右移动");

btnUp.addActionListener(this);
btnUp.setToolTipText("控制子园向上移动");

btnDown.addActionListener(this);
btnDown.setToolTipText("控制文本向下移动");
}

public void setLayout(){
container.setLayout(new BorderLayout());
container.add(btnLeft,BorderLayout.WEST);
container.add(btnRight,BorderLayout.EAST);
container.add(btnUp,BorderLayout.NORTH);
container.add(btnDown,BorderLayout.SOUTH);

// 设置移动面板的布局为null
movePanel.setLayout(null);
movePanel.add(tfMove);

//设置文本的显示位置


tfMove.setBounds(x,y,50,50);
container.add(movePanel,BorderLayout.CENTER);
}

public void actionPerformed(ActionEvent e){




if(e.getSource()==btnRight){
x=x+10;
}
if(e.getSource()==btnLeft){
x=x-10;
}
if(e.getSource()==btnUp){
y=y-10;
}
if(e.getSource()==btnDown){
y=y+10;
}
//重新调整文本框的位置
tfMove.setBounds(x,y,50,50);
}

public static void main(String []args){
buttonDemo frame=new buttonDemo();
frame.setLayout();
frame.show();
}
}


----------------解决方案--------------------------------------------------------

屏幕坐标和数学上的迪卡尔坐标y轴方向相反


----------------解决方案--------------------------------------------------------
啊啊啊啊啊啊~~~~大家不要笑话我了~~~~我新手啊~~~第一次做这东西啊~~~~~我以后记住啦,各位大恩大德,在下没牙也忘不了~~~~~3Q3Q


此问题已解决,方法就在上面几层楼上~~~~跟我一样新的手,看看哈,有帮助~~~
----------------解决方案--------------------------------------------------------
  相关解决方案