fork download
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5.  
  6. public class JButtonSampleFlow04 extends JFrame {
  7.  
  8. public JButtonSampleFlow04() throws HeadlessException {
  9.  
  10. Container container = getContentPane();
  11. container.setLayout(new GridLayout(3, 1));
  12.  
  13. JPanel upperPanel = new JPanel();
  14. upperPanel.setBackground(Color.BLUE);
  15.  
  16. JPanel middlePanel = new JPanel();
  17. middlePanel.setLayout(new GridLayout(1, 5));
  18.  
  19. JPanel lowerPanel = new JPanel();
  20. lowerPanel.setBackground(Color.BLUE);
  21.  
  22. container.add(upperPanel);
  23. container.add(middlePanel);
  24. container.add(lowerPanel);
  25.  
  26. MyButton button1 = new MyButton("Button-1");
  27. MyButton button2 = new MyButton("Button-2");
  28. MyButton button3 = new MyButton("Button-3");
  29. MyButton button4 = new MyButton("Button-4");
  30. MyButton button5 = new MyButton("Button-5");
  31. MyButton button6 = new MyButton("Button-6");
  32.  
  33. middlePanel.add(button1);
  34. middlePanel.add(button2);
  35. middlePanel.add(button3);
  36.  
  37. JPanel smallerPanel1 = new JPanel();
  38. JPanel smallerPanel2 = new JPanel();
  39. smallerPanel2.setLayout(new GridLayout(3, 1));
  40.  
  41. middlePanel.add(smallerPanel1);
  42. middlePanel.add(smallerPanel2);
  43.  
  44. smallerPanel2.add(button4);
  45. smallerPanel2.add(button5);
  46. smallerPanel2.add(button6);
  47. }
  48.  
  49. class MyButton extends JButton {
  50.  
  51. private final String s;
  52.  
  53. MyButton(final String s) {
  54. super(s);
  55. this.s = s;
  56. this.addActionListener(new ActionListener() {
  57. public void actionPerformed(ActionEvent actionEvent) {
  58. System.out.println(s);
  59. }
  60. });
  61. }
  62. }
  63.  
  64. public static void main(String[] args) {
  65. JButtonSampleFlow04 frame = new JButtonSampleFlow04();
  66. frame.setTitle("課題1");
  67. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  68. frame.setBounds(new Rectangle(500 + 8, 300 + 34));
  69. frame.setVisible(true);
  70. }
  71. }
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty