fork download
  1. package Game;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5.  
  6. import javax.swing.*;
  7.  
  8.  
  9. public class Main {
  10.  
  11.  
  12. static JButton btn_StartGame = new JButton ("開始遊戲");
  13. static JButton btn_ComputerGame = new JButton ("電腦對戰");
  14. static JButton btn_InternetGame = new JButton ("網路對戰");
  15.  
  16. public static void main(String[] args) {
  17.  
  18. new Main().Do();
  19.  
  20. }
  21.  
  22. void Do(){
  23.  
  24. JFrame frm = new JFrame ("井字遊戲") ;
  25. Container cp = new Container();
  26. cp = frm.getContentPane () ;
  27.  
  28. // 取消預設之BorderLayout
  29. cp.setLayout(null);
  30.  
  31. // 調整"開始遊戲"的 Button
  32. btn_StartGame.setBounds(150, 80, 100, 40);
  33. btn_StartGame.addActionListener(new Play(frm)); // 傳入frm
  34.  
  35. // 調整"電腦對戰"的Button
  36. btn_ComputerGame.setBounds(150, 150 , 100, 40);
  37.  
  38. // 調整"網路對戰"的Button
  39. btn_InternetGame.setBounds(150, 220 , 100, 40);
  40.  
  41. // 把button加入Layout
  42. cp.add(btn_StartGame);
  43. cp.add(btn_ComputerGame);
  44. cp.add(btn_InternetGame);
  45.  
  46. // 調整 Layout
  47. frm.setSize(400,400);
  48. frm.setLocationRelativeTo(null); // 置中
  49.  
  50. frm.setVisible(true);
  51.  
  52. }
  53.  
  54. static class Play implements ActionListener{
  55.  
  56. // 建構子接收frm
  57. JFrame frm ;
  58. public Play(JFrame frm){
  59. this.frm = frm ;
  60. }
  61.  
  62. public void actionPerformed(ActionEvent e){
  63.  
  64. btn_StartGame.setVisible(false);
  65. btn_ComputerGame.setVisible(false);
  66. btn_InternetGame.setVisible(false);
  67.  
  68. new GameButton(frm) ;
  69.  
  70. }
  71.  
  72. }
  73.  
  74. }
  75.  
  76.  
  77. /////
  78.  
  79.  
  80.  
  81. package Game;
  82.  
  83. import java.awt.Container;
  84. import java.awt.event.ActionEvent;
  85. import java.awt.event.ActionListener;
  86.  
  87. import javax.swing.*;
  88. import javax.swing.event.*;
  89.  
  90. class GameButton implements ActionListener {
  91.  
  92. // 建構子接收frm
  93. JFrame frm ;
  94. Container cp ;
  95. public GameButton(JFrame frm){
  96.  
  97. // 設定 frm , 從frm得到cp
  98. this.frm = frm ;
  99. cp = frm.getContentPane () ;
  100.  
  101. JButton button1 = new JButton ("");
  102. button1.setBounds(50, 80, 100, 40);
  103. cp.add(button1);
  104.  
  105. JButton button2 = new JButton ("");
  106. button2.setBounds(150, 80, 100, 40);
  107. cp.add(button2);
  108.  
  109. JButton button3 = new JButton ("");
  110. button3.setBounds(250, 80, 100, 40);
  111. cp.add(button3);
  112.  
  113. JButton button4 = new JButton ("");
  114. button4.setBounds(50, 120, 100, 40);
  115. cp.add(button4);
  116.  
  117. JButton button5 = new JButton ("");
  118. button5.setBounds(150, 120, 100, 40);
  119. cp.add(button5);
  120.  
  121. JButton button6 = new JButton ("");
  122. button6.setBounds(250, 120, 100, 40);
  123. cp.add(button6);
  124.  
  125. JButton button7 = new JButton ("");
  126. button7.setBounds(50, 160, 100, 40);
  127. cp.add(button7);
  128.  
  129. JButton button8 = new JButton ("");
  130. button8.setBounds(150, 160, 100, 40);
  131. cp.add(button8);
  132.  
  133. JButton button9 = new JButton ("");
  134. button9.setBounds(250, 160, 100, 40);
  135. cp.add(button9);
  136.  
  137. }
  138.  
  139. public void actionPerformed(ActionEvent e){
  140.  
  141.  
  142. }
  143.  
  144. }
  145.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:81: error: class, interface, or enum expected
package Game;
^
Main.java:83: error: class, interface, or enum expected
import java.awt.Container;
^
Main.java:84: error: class, interface, or enum expected
import java.awt.event.ActionEvent;
^
Main.java:85: error: class, interface, or enum expected
import java.awt.event.ActionListener;
^
Main.java:87: error: class, interface, or enum expected
import javax.swing.*;
^
Main.java:88: error: class, interface, or enum expected
import javax.swing.event.*;
^
6 errors
stdout
Standard output is empty