質問編集履歴

3

Mainクラス MainWindowクラスを追記しました

2023/01/13 07:17

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -261,7 +261,89 @@
261
261
 
262
262
 
263
263
  ````
264
-
264
+ ````Java
265
+ package mainPackage;
266
+
267
+ public class Main{
268
+ static MainWindow mainWindow;
269
+ public static void main(String[] args) {
270
+ mainWindow = new MainWindow();
271
+ mainWindow.preparePanels();
272
+ mainWindow.prepareComponents();
273
+ mainWindow.setFrontScreenAndFocus(ScreenMode.TITLE);
274
+
275
+ mainWindow.setVisible(true);
276
+ }
277
+ }
278
+ ````
279
+ ````Java
280
+ package mainPackage;
281
+
282
+ import java.awt.CardLayout;
283
+ import java.awt.Color;
284
+ import java.awt.Dimension;
285
+
286
+ import javax.swing.JFrame;
287
+
288
+ public class MainWindow extends JFrame{
289
+ private static final long serialVersionUID = 1L;
290
+
291
+ ScreenMode screenMode = ScreenMode.GAME;
292
+
293
+ final int WIDTH = 800;
294
+ final int HEIGHT = 600;
295
+
296
+ CardLayout layout = new CardLayout();
297
+
298
+ TitlePanel titlePanel;
299
+ GamePanel gamePanel;
300
+
301
+ MainWindow(){
302
+ this.setTitle("SheepInComa");
303
+ //ImageIcon icon = new ImageIcon(getClass().getClassLoader().getResource(null));
304
+ //this.setIconImage(icon.getImage());
305
+
306
+ this.setDefaultCloseOperation(EXIT_ON_CLOSE);
307
+ this.setResizable(false);
308
+ this.getContentPane().setBackground(Color.green);
309
+ this.setLayout(layout);
310
+ this.setPreferredSize(new Dimension(WIDTH,HEIGHT));
311
+ this.pack();
312
+ this.setLocationRelativeTo(null);
313
+ }
314
+
315
+ public void preparePanels(){
316
+
317
+ titlePanel = new TitlePanel();
318
+ this.add(titlePanel,"タイトル画面");
319
+ gamePanel = new GamePanel();
320
+ this.add(gamePanel,"ゲーム画面");
321
+ this.pack();
322
+ }
323
+
324
+ public void prepareComponents() {
325
+ titlePanel.prepareComponents();
326
+ gamePanel.prepareComponents();
327
+
328
+ }
329
+
330
+ public void setFrontScreenAndFocus(ScreenMode s) {
331
+ screenMode = s;
332
+
333
+ switch(screenMode) {
334
+ case TITLE:
335
+ layout.show(this.getContentPane(),"タイトル画面");
336
+ titlePanel.requestFocus();
337
+ break;
338
+ case GAME:
339
+ layout.show(this.getContentPane(),"ゲーム画面");
340
+ gamePanel.requestFocus();
341
+ break;
342
+ }
343
+ }
344
+
345
+ }
346
+ ````
265
347
 
266
348
  ### 試したこと
267
349
 

2

修正しました

2023/01/13 06:40

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -107,22 +107,6 @@
107
107
 
108
108
  ````Java
109
109
  package mainPackage;
110
-
111
- import java.awt.Color;
112
- import java.awt.Font;
113
- import java.awt.event.KeyEvent;
114
- import java.awt.event.KeyListener;
115
-
116
- import javax.swing.BorderFactory;
117
- import javax.swing.JLabel;
118
- import javax.swing.JPanel;
119
- import javax.swing.SwingConstants;
120
- import javax.swing.border.Border;
121
-
122
- public class TitlePanel extends JPanel{
123
- private static final long serialVersionUID = 1L;
124
-
125
- package mainPackage;
126
110
 
127
111
  import java.awt.Color;
128
112
  import java.awt.Font;
@@ -275,6 +259,7 @@
275
259
  }
276
260
  }
277
261
 
262
+
278
263
  ````
279
264
 
280
265
 

1

ファイル毎にソースコードを分割しました。

2023/01/13 06:06

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -103,8 +103,9 @@
103
103
  }
104
104
  }
105
105
  }
106
-
106
+ ````
107
+
107
- //以下TitlePanelコード(質問用)
108
+ ````Java
108
109
  package mainPackage;
109
110
 
110
111
  import java.awt.Color;