怎样实现在java中点击jbutton改变一个jlabel的位置?
importjava.awt.event.*;importjavax.swing.ImageIcon;importjavax.swing.JButton;importja...
import java.awt.event.*;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class GUI {
private static JFrame frame = new JFrame("GUI test");
private static JPanel panel = new JPanel();
private static JButton button = new JButton("Button1");;
private static ImageIcon imageIcon = new ImageIcon("F:\\BluetoothChat\\a5_16.png");
private static JLabel label = new JLabel(imageIcon);
public static void main(String[] args) {
frame.setLayout(null);
panel.setLayout(null);
label.setLayout(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
panel.add(button);
// button.setLocation(100, 100);
button.setBounds(150, 150, 150, 150);
panel.add(label);
label.setBounds(50,50,100,100);
panel.setBounds(0, 0, 300, 300);
frame.add(panel);
frame.setVisible(true);
}
}
这里是我的代码,运行后是一个button和一张图,怎样给button添加单击事件改变这张图的位置? 展开
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class GUI {
private static JFrame frame = new JFrame("GUI test");
private static JPanel panel = new JPanel();
private static JButton button = new JButton("Button1");;
private static ImageIcon imageIcon = new ImageIcon("F:\\BluetoothChat\\a5_16.png");
private static JLabel label = new JLabel(imageIcon);
public static void main(String[] args) {
frame.setLayout(null);
panel.setLayout(null);
label.setLayout(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
panel.add(button);
// button.setLocation(100, 100);
button.setBounds(150, 150, 150, 150);
panel.add(label);
label.setBounds(50,50,100,100);
panel.setBounds(0, 0, 300, 300);
frame.add(panel);
frame.setVisible(true);
}
}
这里是我的代码,运行后是一个button和一张图,怎样给button添加单击事件改变这张图的位置? 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询