import javax.swing.*; import java.awt.*; import java.awt.event.*; /* */ public class UpDownButton extends JApplet implements ActionListener { int x = 20; JButton lBtn, rBtn; @Override public void init() { lBtn = new JButton("Left"); rBtn = new JButton("Right"); lBtn.addActionListener(this); rBtn.addActionListener(this); setLayout(new FlowLayout()); add(lBtn); add(rBtn); } @Override public void paint(Graphics g) { super.paint(g); g.drawString("HELLO WORLD!", x, 55); } public void actionPerformed(ActionEvent e) { Object source = e.getSource(); if (source == lBtn) { // lBtnが押された x -= 10; } else if (source == rBtn) { // rBtnが押された x += 10; } repaint(); } public static void main(String[] args) { JFrame frame = new JFrame(""); JApplet applet = new UpDownButton(); applet.setPreferredSize(new Dimension(200, 70)); frame.add(applet); frame.pack(); frame.setVisible(true); applet.start(); applet.init(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }