fork download
  1.  
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.GridLayout;
  5. import java.util.Random;
  6.  
  7. import javax.swing.JButton;
  8. import javax.swing.JComponent;
  9. import javax.swing.JFrame;
  10. import javax.swing.JLabel;
  11. import javax.swing.JPanel;
  12. import javax.swing.JScrollPane;
  13. import javax.swing.JTextArea;
  14.  
  15.  
  16. public class Tintiro extends JFrame {
  17.  
  18. Random random = new Random();
  19.  
  20. Tintiro() {
  21.  
  22. setLayout(new BorderLayout());
  23.  
  24. // CENTER に入る方々
  25. JComponent dices = new JLabel("さいころの絵");
  26. JTextArea textArea = new JTextArea();
  27.  
  28. // CENTER の構築
  29. JPanel center = new JPanel();
  30. center.setLayout(new GridLayout(1, 2));
  31. center.add(dices);
  32. center.add(new JScrollPane(textArea));
  33. add(BorderLayout.CENTER, center);
  34.  
  35. // SOUTH に入る方々
  36. JButton jb1 = new JButton("まわす");
  37. JButton jb2 = new JButton("りせっと");
  38.  
  39. // SOUTH の構築
  40. JPanel south = new JPanel();
  41. south.setLayout(new GridLayout(1,2));
  42. south.add(jb1);
  43. south.add(jb2);
  44. add(BorderLayout.SOUTH, south);
  45.  
  46. // "まわす" のアクションリスナー
  47. jb1.addActionListener(e -> {
  48.  
  49. // 0-5 の乱数を発生させる
  50. int n = random.nextInt(6);
  51.  
  52. // メッセージを構築する
  53. String text = (n + 1) + " が出ました\n";
  54.  
  55. // テキストエリアに追加する
  56. textArea.append(text);
  57. });
  58.  
  59. // "りせっと" のアクションリスナー
  60. jb2.addActionListener(e -> {
  61.  
  62. textArea.setText("");
  63. });
  64.  
  65. setSize(700,500);
  66. }
  67.  
  68. public static void main(String[] args) {
  69.  
  70. JFrame f = new Tintiro();
  71. f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  72. f.setLocationByPlatform(true);
  73. f.setVisible(true);
  74. }
  75. }
  76.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty