如何通过任何类型的事件更改标签位置(在这种情况下为按钮) [英] How to change a labels location through any type of event (a button in this case)
本文介绍了如何通过任何类型的事件更改标签位置(在这种情况下为按钮)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class GUI extends JFrame{
private JLabel pic;
private JButton up,down,right,left;
private int a = 10;
private int b = 10;
public GUI(){
super("AWESOME");
setLayout(null);
Icon xbox = new ImageIcon(getClass().getResource("xbox.png"));
pic = new JLabel(xbox);
pic.setBounds(a, b,50,50);
pic.setVisible(true);
up = new JButton("");
up.setBounds(40, 200, 20, 20);
up.setVisible(true);
up.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
b+=5;
}
}
);
add(up);
add(pic);
public class bro {
public static void main(String args[]) {
GUI fr = new GUI();
fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fr.setSize(300, 300);
fr.setVisible(true);
}
}
推荐答案
将b + = 5
替换为
Replaceb+=5
with
pic.setBounds(a, b,50,50);
a
和 b
被 setBounds
调用复制,因此除非再次调用 setBounds
,否则之后更改它们不会产生任何影响。
希望这会有所帮助,
Fredrik
The values of a
and b
are copied by the setBounds
call so changing them afterwards have no impact unless you call setBounds
again.
Hope this helps,
Fredrik
这篇关于如何通过任何类型的事件更改标签位置(在这种情况下为按钮)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文