import javax.swing.*; import java.awt.*; public class LeftRightButton4 extends JPanel { private int x = 20; public LeftRightButton4() { setPreferredSize(new Dimension(200, 70)); JButton lBtn = new JButton("Left"); JButton rBtn = new JButton("Right"); lBtn.addActionListener(e -> { x -= 10; repaint(); }); rBtn.addActionListener(e -> { x += 10; repaint(); }); setLayout(new FlowLayout()); add(lBtn); add(rBtn); } @Override public void paintComponent(Graphics g) { super.paintComponent(g); g.drawString("HELLO WORLD!", x, 55); } public static void main(String[] args) { SwingUtilities.invokeLater(() -> { JFrame frame = new JFrame("ボタン"); frame.add(new LeftRightButton4()); frame.pack(); frame.setVisible(true);; frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }); } }