📄 buttondemo.java
字号:
import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JTextField;import java.awt.event.ActionListener;import java.awt.Container;import javax.swing.ImageIcon;import java.awt.BorderLayout;import java.awt.event.ActionEvent;import javax.swing.Icon;public class ButtonDemo extends JFrame implements ActionListener{ private JButton jbLeft; private JButton jbRight; private JButton jbUp; private JButton jbDown; private JTextField jtfMove; int x, y; private JPanel movePanel; private Container container = getContentPane(); public static void main(String[] args) { ButtonDemo bd = new ButtonDemo(); bd.setLayout(); bd.setVisible(true); } public ButtonDemo() { super("位图按钮"); setSize(260,200); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //创建图片按钮 ImageIcon icLeft = new ImageIcon("left.gif"); jbLeft = new JButton(icLeft); //注册监听器 jbLeft.addActionListener(this); //设置提示字符串 jbLeft.setToolTipText("控制文本向左移动"); //创建图片按钮 ImageIcon icRight = new ImageIcon("right.gif"); jbRight = new JButton(icRight); //注册监听器 jbRight.addActionListener(this); //设置提示字符串 jbRight.setToolTipText("控制文本向右移动"); //创建图片按钮 ImageIcon icUp = new ImageIcon("up.gif"); jbUp = new JButton(icUp); //注册监听器 jbUp.addActionListener(this); //设置提示字符串 jbUp.setToolTipText("控制文本向上移动"); //创建图片按钮 ImageIcon icDown = new ImageIcon("down.gif"); jbDown = new JButton(icDown); //注册监听器 jbDown.addActionListener(this); //设置提示字符串 jbDown.setToolTipText("控制文本向下移动"); jtfMove = new JTextField("位图按钮示例"); movePanel = new JPanel(); } public void setLayout(){ container.setLayout(new BorderLayout()); container.add(jbLeft,BorderLayout.WEST); container.add(jbRight,BorderLayout.EAST); container.add(jbUp,BorderLayout.NORTH); container.add(jbDown,BorderLayout.SOUTH); movePanel.setLayout(null); movePanel.add(jtfMove); x = 0; y = 0; jtfMove.setBounds(x, y, 80, 20); container.add(movePanel, BorderLayout.CENTER); } public void actionPerformed(ActionEvent e){ if(e.getSource() == jbLeft){ x -= 2; } else if(e.getSource() == jbRight){ x += 2; } else if(e.getSource() == jbUp){ y -= 2; } else if(e.getSource() == jbDown){ y += 2; } jtfMove.setBounds(x, y, 80, 20); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -