質問するログイン新規登録

質問編集履歴

2

タイトル変更

2016/01/03 15:59

投稿

ryo-flat
ryo-flat

スコア21

title CHANGED
@@ -1,1 +1,1 @@
1
- javaのJPanelの使い方について
1
+ javaのJPanelの上での描画について
body CHANGED
@@ -139,4 +139,71 @@
139
139
  }
140
140
  }
141
141
 
142
+ ```
143
+
144
+ 以下が少し前に作った別のJPanelを作るクラスです。このような感じでコンストラクタを呼び出せば表示されるようにしたいと考えています。
145
+ ```java
146
+ package gui;
147
+
148
+ import java.awt.Dimension;
149
+ import java.awt.event.ActionEvent;
150
+ import java.awt.event.ActionListener;
151
+
152
+ import javax.swing.JButton;
153
+ import javax.swing.JPanel;
154
+
155
+ public class JobSelect extends JPanel implements ActionListener {
156
+
157
+ public int job;
158
+
159
+ JButton fa;
160
+ JButton ma;
161
+ JButton se;
162
+
163
+
164
+ public JobSelect() {
165
+ fa = new JButton("ファイター");
166
+ fa.setPreferredSize(new Dimension(100, 100));
167
+ fa.addActionListener(this);
168
+ ma = new JButton("マジシャン");
169
+ ma.setPreferredSize(new Dimension(100, 100));
170
+ ma.addActionListener(this);
171
+ se = new JButton("シールドセージ");
172
+ se.setPreferredSize(new Dimension(100, 100));
173
+ se.addActionListener(this);
174
+
175
+ add(fa);
176
+ add(ma);
177
+ add(se);
178
+ }
179
+
180
+ public int getJob() {
181
+ return job;
182
+ }
183
+
184
+ public void setJob(int job) {
185
+ this.job = job;
186
+ }
187
+
188
+ public void actionPerformed(ActionEvent e) {
189
+ if(e.getSource() == fa){
190
+ job = 0;
191
+ System.out.println(job);
192
+ }
193
+ if(e.getSource() == ma){
194
+ job = 1;
195
+ System.out.println(job);
196
+ }
197
+ if(e.getSource() == se){
198
+ job = 2;
199
+ System.out.println(job);
200
+ }
201
+ }
202
+
203
+ public static void main(String[] args){
204
+ new JobSelect();
205
+ }
206
+
207
+ }
208
+
142
209
  ```

1

MainScreenクラスのコード追加

2016/01/03 15:59

投稿

ryo-flat
ryo-flat

スコア21

title CHANGED
File without changes
body CHANGED
@@ -1,1 +1,142 @@
1
- javaでターン制のゲームを作っています。現在作っているBattleScreenクラスはバトル画面をJPanelに描画するクラスであり、MainScreenクラスのJFrameに追加できることを想定しています。しかし、単純に名前を表示したりすることはできるのですがそれを四角の枠で囲ったり、その枠の下に画像を表示を表示させる方法、及びきれいなレイアウトで描画する方法が分かりません。
1
+ javaでターン制のゲームを作っています。現在作っているBattleScreenクラスはバトル画面をJPanelに描画するクラスであり、MainScreenクラスのJFrameに追加できることを想定しています。しかし、単純に名前を表示したりすることはできるのですがそれを四角の枠で囲ったり、その枠の下に画像を表示を表示させる方法、及びきれいなレイアウトで描画する方法が分かりません。下記がMainScreenクラスで、このJFrameにBorderLayoutのCENTERでaddしたいと考えています。
2
+ ```java
3
+ package gui;
4
+
5
+ import java.awt.BorderLayout;
6
+ import java.awt.Container;
7
+ import java.awt.Dimension;
8
+ import java.awt.Font;
9
+ import java.awt.event.ActionEvent;
10
+ import java.awt.event.ActionListener;
11
+
12
+ import javax.swing.ButtonGroup;
13
+ import javax.swing.JButton;
14
+ import javax.swing.JFrame;
15
+ import javax.swing.JLabel;
16
+ import javax.swing.JPanel;
17
+ import javax.swing.JRadioButton;
18
+ import javax.swing.JTextField;
19
+
20
+ import state.BattleText;
21
+
22
+
23
+ public class MainScreen extends JFrame implements ActionListener {
24
+
25
+ JFrame f;
26
+ JPanel p1;
27
+ JPanel p2;
28
+ JPanel p3;
29
+ JPanel p4;
30
+ JPanel p5;
31
+ JPanel p6;
32
+ JPanel battleScreen;
33
+ JTextField ip;
34
+ JTextField na;
35
+ JButton connect;
36
+ JButton kettei1;
37
+ JButton kettei2;
38
+ JButton atk;
39
+ JButton def;
40
+ JButton sp;
41
+ JRadioButton fi;
42
+ JRadioButton ma;
43
+ JRadioButton se;
44
+ ButtonGroup group;
45
+ JLabel commandInfo;
46
+ JLabel count;
47
+ String address;
48
+ String eff="a";
49
+ String name;
50
+ int job = 0;
51
+
52
+ public MainScreen() {
53
+ f = new JFrame("DENDAI QUEST");
54
+ f.setVisible(true);
55
+ f.setSize(1100, 700);
56
+ setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
57
+
58
+ p1 = new JPanel();
59
+ p2 = new JPanel();
60
+ p3 = new JPanel();
61
+ p4 = new JPanel();
62
+ p5 = new JPanel();
63
+ p6 = new JPanel();
64
+ battleScreen = new JPanel();
65
+
66
+ commandInfo = new JLabel();
67
+ count = new JLabel();
68
+
69
+
70
+
71
+
72
+ ip = new JTextField("IP", 20);
73
+ na = new JTextField("名前", 20);
74
+
75
+ fi = new JRadioButton("ファイター", true);
76
+ ma = new JRadioButton("マジシャン");
77
+ se = new JRadioButton("シールドセージ");
78
+
79
+ group = new ButtonGroup();
80
+
81
+ group.add(fi);
82
+ group.add(ma);
83
+ group.add(se);
84
+
85
+ connect = new JButton("接続");
86
+ connect.addActionListener(this);
87
+ kettei1 = new JButton("決定");
88
+ kettei1.addActionListener(this);
89
+ kettei2 = new JButton("決定");
90
+ kettei2.addActionListener(this);
91
+
92
+ commandInfo.setText("<html>コマンド情報<br>攻撃:通常攻撃<br>防御:防御"
93
+ + "<br>特殊<br>ファイター:大攻撃<br>マジシャン:回復" + "<br>シールドセージ:跳ね返し</html>");
94
+ commandInfo.setBounds(0, 50, 0, 0);
95
+
96
+ atk = new JButton("攻撃");
97
+ atk.setPreferredSize(new Dimension(200, 100));
98
+ atk.addActionListener(this);
99
+ def = new JButton("防御");
100
+ def.setPreferredSize(new Dimension(200, 100));
101
+ def.addActionListener(this);
102
+ sp = new JButton("特殊");
103
+ sp.setPreferredSize(new Dimension(200, 100));
104
+ sp.addActionListener(this);
105
+
106
+ p1.add(commandInfo);
107
+
108
+ p2.add(atk);
109
+ p2.add(def);
110
+ p2.add(sp);
111
+
112
+
113
+ p3.add(ip);
114
+ p3.add(connect);
115
+ p3.add(na);
116
+ p3.add(kettei1);
117
+ p3.add(fi);
118
+ p3.add(ma);
119
+ p3.add(se);
120
+ p3.add(kettei2);
121
+
122
+ p5.add(battleScreen, BorderLayout.CENTER);
123
+
124
+
125
+ Container contentPane = f.getContentPane();
126
+ contentPane.add(p1, BorderLayout.EAST);
127
+
128
+ contentPane.add(p3, BorderLayout.NORTH);
129
+
130
+ //contentPane.add(p5, BorderLayout.CENTER);
131
+
132
+
133
+ //contentPane.add(jobSelectPanel, BorderLayout.CENTER);
134
+
135
+ contentPane.add(p2, BorderLayout.SOUTH);
136
+ }
137
+ public static void main(String[] args) {
138
+ new MainScreen();
139
+ }
140
+ }
141
+
142
+ ```