質問編集履歴

1

修正後の追加

2022/11/17 05:43

投稿

ktmr
ktmr

スコア10

test CHANGED
File without changes
test CHANGED
@@ -9,7 +9,22 @@
9
9
 
10
10
  ### 該当のソースコード
11
11
 
12
+ ログイン画面
13
+ ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-11-16/bd5286d0-c079-4f71-980d-0bf7474c990c.png)
14
+ ログイン失敗時のエラーメッセージ
15
+ ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-11-16/3031cc48-4759-4791-8bd6-e2ba34014cd9.png)
16
+ ログイン完了後に遷移させたい画面
17
+ ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-11-16/3e27b3dc-0bca-4b90-8ade-56897fb15a20.png)
18
+
19
+ ### 試したこと
20
+ setVisible()やCardLayoutに関連があると考え、調べながらいろいろ変更を加えてみたものの解決しませんでした。
21
+
22
+ ### 補足情報(FW/ツールのバージョンなど)
23
+
24
+ java SE 1.8
25
+
26
+ ### 修正後
12
- ```ここに言語を入力
27
+ ```ここに言語を入力
13
28
  import java.awt.BorderLayout;
14
29
  import java.awt.CardLayout;
15
30
  import java.awt.Color;
@@ -35,174 +50,227 @@
35
50
  import javax.swing.JOptionPane;
36
51
 
37
52
  public class Test2 extends JFrame {
53
+ Container main_con;
54
+ JFrame main_frame;
38
55
 
56
+
57
+ final String order_itemNames[]={"ホットドック", "サンドウィッチ", "餃子", "ラーメン", "トッポギ", "ハンバーガー"};
58
+ final int order_itemPrices[]={1300, 2600, 3500, 3800, 2500, 4700};
59
+ int order_total=0;
60
+ JTextArea order_ta_list;
39
61
 
40
- public Test2() {
62
+ public Test2() {
41
-
63
+
42
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
64
+ this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
65
+ this.setTitle("ログイン画面");
66
+ this.setSize(850,500);
67
+ this.setLayout(new CardLayout());
68
+
69
+ add(new Login());
70
+ add(new Order());
71
+ setVisible(true);
43
72
 
44
- this.setSize(850,500);
45
- this.setLayout(new CardLayout());
46
-
47
- add(new Login());
48
- setVisible(true);
49
- add(new Order());
50
- //setVisible(true);
51
-
52
- }
73
+ }
53
74
 
54
- class Login extends JPanel {
75
+ class Login extends JPanel {
55
- Login() {
76
+ Login() {
56
- super(new BorderLayout());
77
+ super(new BorderLayout());
57
- //ロゴ
78
+ //ロゴ
58
- JLabel logo = new JLabel(new ImageIcon("./res/main.png"));
79
+ JLabel logo = new JLabel(new ImageIcon("./res/main.png"));
59
- this.add(logo,BorderLayout.NORTH);
80
+ this.add(logo,BorderLayout.NORTH);
60
-
61
-
81
+
82
+
62
- JPanel info = new JPanel();
83
+ JPanel info = new JPanel();
63
- info.setLayout(new FlowLayout());
84
+ info.setLayout(new FlowLayout());
64
- this.add(info,BorderLayout.SOUTH);
85
+ this.add(info,BorderLayout.SOUTH);
65
-
86
+
66
- info.add(new JLabel("ID:"));
87
+ info.add(new JLabel("ID:"));
67
-
88
+
68
- JTextField id = new JTextField("",10);
89
+ JTextField id = new JTextField("",10);
69
- info.add(id);
90
+ info.add(id);
70
-
91
+
71
- info.add(new JLabel("PASSWORD:"));
92
+ info.add(new JLabel("PASSWORD:"));
72
-
93
+
73
- JPasswordField pw = new JPasswordField("",10);
94
+ JPasswordField pw = new JPasswordField("",10);
74
- info.add(pw);
95
+ info.add(pw);
75
-
96
+
76
- JButton btn0 = new JButton("LOGIN");
97
+ JButton btn0 = new JButton("LOGIN");
77
- info.add(btn0);
98
+ info.add(btn0);
78
- btn0.addActionListener(new ActionListener(){
99
+ btn0.addActionListener(new ActionListener(){ //LOGINボタン命令
79
100
 
80
- @Override
101
+ @Override
81
- public void actionPerformed(ActionEvent e) {
102
+ public void actionPerformed(ActionEvent e) {
103
+ //ここにイベント命令
82
- //ID,PASSWORDに入っているテキストを取り出す
104
+ //ID,PASSWORDに入っているテキストを取り出す
83
- String idStr = new String(id.getText());
105
+ String idStr = new String(id.getText());
84
- String pass = new String(pw.getPassword());
106
+ String pass = new String(pw.getPassword());
85
107
  if(idStr.equals("aaa") && pass.equals("1234")) {
86
108
  JOptionPane.showMessageDialog(null, "Success!!", "Success", JOptionPane.INFORMATION_MESSAGE);
109
+ this.next(main_frame); //この部分
110
+
87
111
  }else {
88
112
  String selectvalues[] = {"確認"};
89
113
 
90
114
  int value = JOptionPane.showOptionDialog(null,
91
- "IDかパスワードを間違えました",
115
+ "IDかパスワードを間違えました",
92
- "ログイン失敗",
116
+ "ログイン失敗",
93
- JOptionPane.YES_NO_OPTION,
117
+ JOptionPane.YES_NO_OPTION,
94
- JOptionPane.ERROR_MESSAGE,
118
+ JOptionPane.ERROR_MESSAGE,
95
- null,
119
+ null,
96
- selectvalues,
120
+ selectvalues,
97
- selectvalues[0]);
121
+ selectvalues[0]);
98
122
  id.setText("");
99
123
  pw.setText("");
100
124
  }
101
- }
125
+ }
102
- });
126
+ });
103
-
104
- //setVisible(true);
105
- }
127
+ }
106
- }
128
+ }
107
129
 
108
- class Order extends JPanel {
109
- Order() {
110
- super(new BorderLayout());
111
- //メニュー
112
- JPanel Order = new JPanel();
113
- Order.setLayout(new GridLayout(2,3));
114
- this.add(Order,BorderLayout.WEST);
115
-
116
- ImageIcon img0 = new ImageIcon("./res/0.jpg");
117
- JButton food0 = new JButton(img0);
118
- food0.setBorderPainted(false);
119
- food0.setFocusPainted(false);
120
- food0.setContentAreaFilled(false);
121
- food0.setMargin(new Insets(3,3,5,5));
122
- Order.add(food0);
123
-
124
- ImageIcon img1 = new ImageIcon("./res/1.jpg");
125
- JButton food1 = new JButton(img1);
126
- food1.setBorderPainted(false);
127
- food1.setFocusPainted(false);
128
- food1.setContentAreaFilled(false);
129
- food1.setMargin(new Insets(3,3,5,5));
130
- Order.add(food1);
131
-
132
- ImageIcon img2 = new ImageIcon("./res/2.jpg");
133
- JButton food2 = new JButton(img2);
134
- food2.setBorderPainted(false);
135
- food2.setFocusPainted(false);
136
- food2.setContentAreaFilled(false);
137
- food2.setMargin(new Insets(3,3,5,5));
138
- Order.add(food2);
139
-
140
- ImageIcon img3 = new ImageIcon("./res/3.jpg");
141
- JButton food3 = new JButton(img3);
142
- food3.setBorderPainted(false);
143
- food3.setFocusPainted(false);
144
- food3.setContentAreaFilled(false);
145
- food3.setMargin(new Insets(3,3,5,5));
146
- Order.add(food3);
147
-
148
- ImageIcon img4 = new ImageIcon("./res/4.jpg");
149
- JButton food4 = new JButton(img4);
150
- food4.setBorderPainted(false);
151
- food4.setFocusPainted(false);
152
- food4.setContentAreaFilled(false);
153
- food4.setMargin(new Insets(3,3,5,5));
154
- Order.add(food4);
155
-
156
- ImageIcon img5 = new ImageIcon("./res/5.jpg");
157
- JButton food5 = new JButton(img5);
158
- food5.setBorderPainted(false);
159
- food5.setFocusPainted(false);
160
- food5.setContentAreaFilled(false);
161
- food5.setMargin(new Insets(3,3,5,5));
162
- Order.add(food5);
163
-
164
- //注文リストとボタン
165
- JPanel list = new JPanel();
166
- list.setLayout(new FlowLayout());
167
- this.add(list);
168
-
169
- order_ta_list = new JTextArea("",25,15);
170
- LineBorder border = new LineBorder(Color.gray, 1, false);
171
- order_ta_list.setBorder(border);
172
- order_ta_list.selectAll();
173
- list.add(order_ta_list);
174
-
175
- JPanel listBtn = new JPanel();
176
- listBtn.setLayout(new FlowLayout());
177
- list.add(listBtn);
178
-
179
- JButton btn1 = new JButton("CANCEL");
180
- listBtn.add(btn1);
181
-
182
- JButton btn2 = new JButton("ORDER");
183
- list.add(btn2);
184
-
185
- //setVisible(true); //false
186
- }
187
- }
188
-
189
- public static void main(String[] args) {
190
- new Test2();
191
-
192
- }
130
+ class Order extends JPanel {
131
+ Order() {
132
+ super(new BorderLayout());
133
+ //メニュー
134
+ JPanel Order = new JPanel();
135
+ Order.setLayout(new GridLayout(2,3));
136
+ this.add(Order,BorderLayout.WEST);
137
+
138
+ ImageIcon img0 = new ImageIcon("./res/0.jpg");
139
+ JButton food0 = new JButton(img0);
140
+ food0.setBorderPainted(false);
141
+ food0.setFocusPainted(false);
142
+ food0.setContentAreaFilled(false);
143
+ food0.setMargin(new Insets(3,3,5,5));
144
+ Order.add(food0);
145
+ food0.addActionListener(new ActionListener(){
146
+ @Override
147
+ public void actionPerformed(ActionEvent e) {
148
+ order_ta_list.append(order_itemNames[0]+" "+order_itemPrices[0]+"円\n");
149
+ order_total = order_total + order_itemPrices[0];
150
+ }
151
+ });
152
+
153
+ ImageIcon img1 = new ImageIcon("./res/1.jpg");
154
+ JButton food1 = new JButton(img1);
155
+ food1.setBorderPainted(false);
156
+ food1.setFocusPainted(false);
157
+ food1.setContentAreaFilled(false);
158
+ food1.setMargin(new Insets(3,3,5,5));
159
+ Order.add(food1);
160
+ food1.addActionListener(new ActionListener(){
161
+ @Override
162
+ public void actionPerformed(ActionEvent e) {
163
+ order_ta_list.append(order_itemNames[1]+" "+order_itemPrices[1]+"円\n");
164
+ order_total = order_total + order_itemPrices[1];
165
+ }
166
+ });
167
+
168
+ ImageIcon img2 = new ImageIcon("./res/2.jpg");
169
+ JButton food2 = new JButton(img2);
170
+ food2.setBorderPainted(false);
171
+ food2.setFocusPainted(false);
172
+ food2.setContentAreaFilled(false);
173
+ food2.setMargin(new Insets(3,3,5,5));
174
+ Order.add(food2);
175
+ food2.addActionListener(new ActionListener(){
176
+ @Override
177
+ public void actionPerformed(ActionEvent e) {
178
+ order_ta_list.append(order_itemNames[2]+" "+order_itemPrices[2]+"円\n");
179
+ order_total = order_total + order_itemPrices[2];
180
+ }
181
+ });
182
+
183
+ ImageIcon img3 = new ImageIcon("./res/3.jpg");
184
+ JButton food3 = new JButton(img3);
185
+ food3.setBorderPainted(false);
186
+ food3.setFocusPainted(false);
187
+ food3.setContentAreaFilled(false);
188
+ food3.setMargin(new Insets(3,3,5,5));
189
+ Order.add(food3);
190
+ food3.addActionListener(new ActionListener(){
191
+ @Override
192
+ public void actionPerformed(ActionEvent e) {
193
+ order_ta_list.append(order_itemNames[3]+" "+order_itemPrices[3]+"円\n");
194
+ order_total = order_total + order_itemPrices[3];
195
+ }
196
+ });
197
+
198
+ ImageIcon img4 = new ImageIcon("./res/4.jpg");
199
+ JButton food4 = new JButton(img4);
200
+ food4.setBorderPainted(false);
201
+ food4.setFocusPainted(false);
202
+ food4.setContentAreaFilled(false);
203
+ food4.setMargin(new Insets(3,3,5,5));
204
+ Order.add(food4);
205
+ food4.addActionListener(new ActionListener(){
206
+ @Override
207
+ public void actionPerformed(ActionEvent e) {
208
+ order_ta_list.append(order_itemNames[4]+" "+order_itemPrices[4]+"円\n");
209
+ order_total = order_total + order_itemPrices[4];
210
+ }
211
+ });
212
+
213
+ ImageIcon img5 = new ImageIcon("./res/5.jpg");
214
+ JButton food5 = new JButton(img5);
215
+ food5.setBorderPainted(false);
216
+ food5.setFocusPainted(false);
217
+ food5.setContentAreaFilled(false);
218
+ food5.setMargin(new Insets(3,3,5,5));
219
+ Order.add(food5);
220
+ food5.addActionListener(new ActionListener(){
221
+ @Override
222
+ public void actionPerformed(ActionEvent e) {
223
+ order_ta_list.append(order_itemNames[5]+" "+order_itemPrices[5]+"円\n");
224
+ order_total = order_total + order_itemPrices[5];
225
+ }
226
+ });
227
+
228
+ //注文リストとボタン
229
+ JPanel list = new JPanel();
230
+ list.setLayout(new FlowLayout());
231
+ this.add(list);
232
+
233
+ order_ta_list = new JTextArea("",25,13);
234
+ LineBorder border = new LineBorder(Color.gray, 1, false);
235
+ order_ta_list.setBorder(border);
236
+ order_ta_list.setCaretPosition(0);
237
+ order_ta_list.selectAll();
238
+
239
+ JScrollPane scrollpane = new JScrollPane();
240
+ scrollpane.setViewportView(order_ta_list);
241
+ list.add(scrollpane);
242
+
243
+ JPanel listBtn = new JPanel();
244
+ listBtn.setLayout(new FlowLayout());
245
+ list.add(listBtn);
246
+
247
+ JButton btn1 = new JButton("CANCEL");
248
+ listBtn.add(btn1);
249
+ btn1.addActionListener(new ActionListener(){
250
+ @Override
251
+ public void actionPerformed(ActionEvent e) {
252
+ order_ta_list.setText("");
253
+ order_total=0;
254
+ }
255
+ });
256
+
257
+ JButton btn2 = new JButton("ORDER");
258
+ list.add(btn2);
259
+ btn2.addActionListener(new ActionListener(){
260
+ @Override
261
+ public void actionPerformed(ActionEvent e) {
262
+ order_ta_list.append("---------------\n"+order_total+"円");
263
+
264
+ }
265
+ });
266
+ }
267
+ }
268
+
269
+
270
+ public static void main(String[] args) {
271
+ new Test2();
272
+
273
+ }
193
274
  }
194
275
  ```
195
- ログイン画面
196
- ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-11-16/bd5286d0-c079-4f71-980d-0bf7474c990c.png)
197
- ログイン失敗時のエラーメッセージ
198
- ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-11-16/3031cc48-4759-4791-8bd6-e2ba34014cd9.png)
199
- ログイン完了後に遷移させたい画面
200
- ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-11-16/3e27b3dc-0bca-4b90-8ade-56897fb15a20.png)
201
-
202
- ### 試したこと
203
- setVisible()やCardLayoutに関連があると考え、調べながらいろいろ変更を加えてみたものの解決しませんでした。
204
-
205
- ### 補足情報(FW/ツールジョンなど)
276
+ 文字数都合から、該当のソスコードを削除させていただきました。
206
-
207
- java SE 1.8
208
-