質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Q&A

解決済

1回答

1232閲覧

Java ウィンドウ2つ目の中の情報を作りたいです

Kouyan

総合スコア5

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

0グッド

1クリップ

投稿2020/03/09 21:51

ここに質問の内容を詳しく書いてください。

Javaでじゃんけんのゲームを作っています。
グーチョキパーを選んだ後に、決定ボタンで次のウィンドウを立ち上げ中にボタン等を追加していきたいのですが、書き方がわかりません。void Verification()で新しくウィンドウ2を作ることができたのですが、中に情報を入れても反応しません。
ご回答よろしくおねがいします。

該当のソースコード

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Janken extends JFrame
{

private JLabel ChooseLabel;
private String Hand;

//Janken frame1 = new Janken();//frameオブジェクト作成
//
//frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame1.setBounds(100, 10, 300, 200);//frame.setBounds(座標横, 座標高さ, サイズ横, サイズ高さ);
//frame1.setTitle("タイトル1");//frameのタイトル設定
//frame1.setVisible(true);//frame表示

public static void main(String[] args) { Janken frame1 = new Janken();//frameオブジェクト作成 frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame1.setBounds(100, 10, 300, 200);//frame.setBounds(座標横, 座標高さ, サイズ横, サイズ高さ); frame1.setTitle("タイトル1");//frameのタイトル設定 frame1.setVisible(true);//frame表示 } Janken() { JLabel titleLabel = new JLabel("最初はぐーっじゃんけん??"); titleLabel.setOpaque(true); titleLabel.setForeground(Color.BLACK); titleLabel.setFont(new Font("Serif", Font.BOLD,80)); titleLabel.setSize(500,500); JPanel p1 = new JPanel(); p1.add(titleLabel); getContentPane().add(p1, BorderLayout.NORTH); //JLabel ChooseLabel = new JLabel 重要こう書くと上書きされてしまいボタンを押した時反応しなくなります ChooseLabel = new JLabel("選んだものが表示されます"); ChooseLabel.setOpaque(true); ChooseLabel.setForeground(Color.BLACK); ChooseLabel.setFont(new Font("Serif", Font.BOLD,30)); ChooseLabel.setSize(500,500); JPanel p2 = new JPanel(); p2.add(ChooseLabel); getContentPane().add(p2, BorderLayout.SOUTH); JPanel p3 = new JPanel(); p3.setLayout(null); JButton buttonRook = new JButton("グー");//ボタン追加 buttonRook.setBounds(300, 200, 150, 150); // button1.setBounds(座標横, 座標高さ, サイズ横, サイズ高さ); buttonRook.addActionListener(new ButtonRookActionListener()); JButton buttonCissors = new JButton("チョキ");//ボタン追加 buttonCissors.setBounds(600, 200, 150, 150); buttonCissors.addActionListener(new ButtonCissorsActionListener()); JButton buttonsPaper = new JButton("パー");//ボタン追加 buttonsPaper.setBounds(900, 200, 150, 150); buttonsPaper.addActionListener(new ButtonPaperActionListener()); JButton buttonsDecision = new JButton("決定");//ボタン追加 buttonsDecision.setBounds(1100, 500, 200, 100); buttonsDecision.addActionListener(new ButtonDecisionActionListener()); p3.add(buttonRook); p3.add(buttonCissors); p3.add(buttonsPaper); p3.add(buttonsDecision); getContentPane().add(p3, BorderLayout.CENTER);//センターしかなぜか配置できない } void Select() { ChooseLabel.setText(Hand+"でいいですか"); } void Verification()//2つ目のウィンドウの情報を入力かな?? { Verification frame2 = new Verification();//frameオブジェクト作成 frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame2.setBounds(100, 10, 300, 200);//frame.setBounds(座標横, 座標高さ, サイズ横, サイズ高さ); frame2.setTitle("タイトル2");//frameのタイトル設定 frame2.setVisible(true);//frame表示 JPanel p4 = new JPanel(); p4.setLayout(null); JButton buttontest = new JButton("グー");//ボタン追加 buttontest.setBounds(300, 200, 150, 150); p4.add(buttontest); getContentPane().add(p4, BorderLayout.CENTER); } class ButtonRookActionListener implements ActionListener { public void actionPerformed(ActionEvent e)//イベントが発生されたら呼び出される { Hand = "グー"; Select();//ここは新しいクラスなので一番上で改めてChooseLabelを定義してあげよう } } class ButtonCissorsActionListener implements ActionListener { public void actionPerformed(ActionEvent e)//イベントが発生されたら呼び出される { Hand = "チョキ"; Select(); } } class ButtonPaperActionListener implements ActionListener { public void actionPerformed(ActionEvent e)//イベントが発生されたら呼び出される { Hand = "パー"; Select(); } } class ButtonDecisionActionListener implements ActionListener { public void actionPerformed(ActionEvent e)//イベントが発生されたら呼び出される { Verification(); //下記は確認画面に遷移しないパターンなので上記をコメントアウトすれば使える

//// if(Hand == "グー")
//// {
//// ChooseLabel.setText("航也はパーを出した。やっぱグーか");
//// }
//// else if(Hand == "チョキ")
//// {
//// ChooseLabel.setText("航也はグーを出した。よっっっわっ");
//// }
//// else if(Hand == "パー")
//// {
//// ChooseLabel.setText("航也はチョキを出した。何度でも受けてやろう");
// }

} }

// //Verification確認画面のクラスを作成 空白のウィンドウを作りたいならもう1つ作ればいいんじゃないかな?
public class Verification extends JFrame
{

}

}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

jimbe

2020/03/10 04:18

ご質問は編集できます. コードは, 入力枠の上辺にある <code> を押すと出てくる ``` の行の間にある "コード" という文字列を置き換える形で入力(コピペ)してください. 専用の枠に表示されるようになります. 入力枠の下にプレビューがありますので, 確認しながら修正してください.
jimbe

2020/03/10 04:30

確認なら通常はダイアログを使うと思うのですが, なぜフレームを使うのでしょうか.
jimbe

2020/03/10 05:59

Verification ではどういう表示・操作を出来るようにするのでしょうか.
guest

回答1

0

ベストアンサー

以下のコードでは, 画面の各要素をパネルで分割し, Verification もその中の1パネルとして表示しています.

java

1import java.awt.*; 2import java.awt.event.*; 3 4import javax.swing.*; 5 6public class JankenFrame extends JFrame { 7 8 public static void main(String[] args) { 9 new JankenFrame().setVisible(true); 10 } 11 12 private static final String MAIN_PANEL = "Main"; 13 private static final String VERIFICATION_PANEL = "Verification"; 14 15 private CardLayout cardLayout; 16 private MessagePanel messagePanel; 17 private VerificationPanel verificationPanel; 18 19 JankenFrame() { 20 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 21 setBounds(100, 10, 1400, 1000); 22 setTitle("タイトル1"); 23 24 cardLayout = new CardLayout(); 25 getContentPane().setLayout(cardLayout); 26 27 JPanel mainPanel = new JPanel(new BorderLayout()); 28 messagePanel = new MessagePanel(); 29 mainPanel.add(new TitlePanel(), BorderLayout.NORTH); 30 mainPanel.add(new JankenPanel(), BorderLayout.CENTER); 31 mainPanel.add(messagePanel, BorderLayout.SOUTH); 32 add(mainPanel, MAIN_PANEL); 33 34 verificationPanel = new VerificationPanel(); 35 add(verificationPanel, VERIFICATION_PANEL); 36 37 showMainPanel(); 38 } 39 40 void setMessage(String text) { messagePanel.setText(text); } 41 42 void showMainPanel() { 43 cardLayout.show(getContentPane(), MAIN_PANEL); 44 } 45 void showVerificationPanel(String hand) { 46 verificationPanel.setHand(hand); 47 cardLayout.show(getContentPane(), VERIFICATION_PANEL); 48 } 49 50 private static class TitlePanel extends JPanel { 51 TitlePanel() { 52 super(); 53 54 JLabel titleLabel = new JLabel("最初はぐーっじゃんけん??"); 55 titleLabel.setFont(new Font("Serif", Font.BOLD, 80)); 56 titleLabel.setSize(500, 500); 57 add(titleLabel); 58 } 59 } 60 61 private static class MessagePanel extends JPanel { 62 private JLabel chooseLabel; 63 MessagePanel() { 64 super(); 65 66 chooseLabel = new JLabel("選んだものが表示されます"); 67 chooseLabel.setFont(new Font("Serif", Font.BOLD, 30)); 68 chooseLabel.setSize(500, 500); 69 add(chooseLabel); 70 } 71 void setText(String text) { 72 chooseLabel.setText(text); 73 } 74 } 75 76 private class JankenPanel extends JPanel { 77 JankenPanel() { 78 super(null); 79 80 DecisionAction decisionAction = new DecisionAction("決定"); 81 82 ActionListener handActionListener = new ActionListener() { 83 public void actionPerformed(ActionEvent e) { 84 String hand = ((JButton)e.getSource()).getText(); 85 setMessage(hand + "でいいですか"); 86 decisionAction.setHand(hand); 87 } 88 }; 89 90 add(createHandButton("グー", 300, 200, handActionListener)); 91 add(createHandButton("チョキ", 600, 200, handActionListener)); 92 add(createHandButton("パー", 900, 200, handActionListener)); 93 94 JButton decisionButton = new JButton(decisionAction); 95 decisionButton.setBounds(1100, 500, 200, 100); 96 add(decisionButton); 97 } 98 99 private JButton createHandButton(String text, int x, int y, ActionListener l) { 100 JButton button = new JButton(text); 101 button.setBounds(x, y, 150, 150); 102 button.addActionListener(l); 103 return button; 104 } 105 } 106 107 private class DecisionAction extends AbstractAction { 108 private String hand; 109 DecisionAction(String text) { 110 super(text); 111 setHand(null); 112 } 113 void setHand(String hand) { 114 this.hand = hand; 115 setEnabled(hand != null); 116 } 117 @Override 118 public void actionPerformed(ActionEvent e) { 119 showVerificationPanel(hand); 120 121 //下記は確認画面に遷移しないパターンなので上記をコメントアウトすれば使える 122 //if(hand.equals("グー")) { 123 // setMessage("航也はパーを出した。やっぱグーか"); 124 //} else if(hand.equals("チョキ")) { 125 // setMessage("航也はグーを出した。よっっっわっ"); 126 //} else if(hand.equals("パー")) { 127 // setMessage("航也はチョキを出した。何度でも受けてやろう"); 128 //} 129 //setHand(null); 130 } 131 } 132 133 private class VerificationPanel extends JPanel { 134 private String hand; 135 VerificationPanel() { 136 super(new BorderLayout()); 137 setBackground(Color.YELLOW); 138 139 JButton buttontest = new JButton("グー"); 140 buttontest.setBounds(300, 200, 150, 150); 141 add(buttontest, BorderLayout.CENTER); 142 } 143 void setHand(String hand) { this.hand = hand; } 144 } 145}

投稿2020/03/10 06:36

jimbe

総合スコア12632

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Kouyan

2020/03/11 21:26

ご回答ありがとうございます。助かりました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問