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

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

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

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

Swing

SwingはJavaに標準で付属するグラフィック関連のクラスライブラリを指します。

Q&A

解決済

2回答

499閲覧

jframeを継承した外部クラスのsetDefaultCloseOperation(EXIT_ON_CLOSE)が内部クラスのjframeにも適応されるのはなぜですか?

Mitika-t

総合スコア4

Java

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

Swing

SwingはJavaに標準で付属するグラフィック関連のクラスライブラリを指します。

0グッド

0クリップ

投稿2023/08/28 10:06

編集2023/08/28 20:57

実現したいこと

私はswingをつかってテキストRPG(ボタン入力で別ウィンドウが開く)を作成しています。
外部クラスにjframeを継承したメインウィンドウを作り、
その中のボタンをクリックによって新しいウィンドウを描画しています。
setDefaultCloseOperation(EXIT_ON_CLOSE)メソッドは、外部クラスのjframeにコードを書いています。
内部クラスの新しいウィンドウの×ボタンをクリックすると、プログラムがすべて終了してしまいます。
なぜなのかを教えて下さい。

おそらく該当箇所ですが、メインフレームを変数に入れなければ、新しいウィンドウを消しても古いウィンドウは残り思った通りに動きます。

↓すべて終了してしまうコード

java

1public class StartController { 2 static MainFrame mainFrame = null; 3 public StartController() { 4 File file = new File("C:/java/pref.properties"); 5 6 if (file.exists()) { 7 PrefModel.loadPrefModel(); 8 } else if (!(file.exists())) { 9 PrefModel.createPrefModel(); 10 } 11 12 PrefModel.loadPrefModel(); 13 14 mainFrame = new MainFrame(); 15 } 16 17}

↓古いウィンドウは残るコード

java

1public class StartController { 2 public StartController() { 3 File file = new File("C:/java/pref.properties"); 4 5 if (file.exists()) { 6 PrefModel.loadPrefModel(); 7 } else if (!(file.exists())) { 8 PrefModel.createPrefModel(); 9 } 10 11 PrefModel.loadPrefModel(); 12 13 new MainFrame(); 14 } 15 16}

コード全体

java

1import java.awt.Dimension; 2import java.awt.FlowLayout; 3import java.awt.GridLayout; 4import java.awt.event.ActionEvent; 5import java.awt.event.ActionListener; 6import java.io.File; 7 8import javax.swing.JButton; 9import javax.swing.JComboBox; 10import javax.swing.JFrame; 11import javax.swing.JLabel; 12import javax.swing.JMenu; 13import javax.swing.JMenuBar; 14import javax.swing.JMenuItem; 15import javax.swing.JPanel; 16import javax.swing.JTextField; 17import javax.swing.JTextPane; 18 19public class StartController { 20 static MainFrame mainFrame = null; 21 public StartController() { 22 File file = new File("C:/java/pref.properties"); 23 24 if (file.exists()) { 25 PrefModel.loadPrefModel(); 26 } else if (!(file.exists())) { 27 PrefModel.createPrefModel(); 28 } 29 30 PrefModel.loadPrefModel(); 31 32 mainFrame = new MainFrame(); 33 } 34 35} 36 37class MainFrame extends JFrame implements ActionListener { 38 JMenuBar menuBar = new JMenuBar(); 39 JMenu confMenu = new JMenu("conf"); 40 JMenuItem prefItem = new JMenuItem("pref"); 41 HomePanel homePanel = new HomePanel(); 42 JsonHelper jsonHelper = new JsonHelper(); 43 44 MainFrame() { 45 setDefaultCloseOperation(EXIT_ON_CLOSE); 46 setSize(PrefModel.WIDTH, PrefModel.HEIGHT); 47 setLocationRelativeTo(rootPane); 48 49 confMenu.add(prefItem); 50 menuBar.add(confMenu); 51 setJMenuBar(menuBar); 52 getContentPane().add(homePanel); 53 prefItem.addActionListener(this); 54 55 pack(); 56 setResizable(false); 57 setVisible(true); 58 59 } 60 61 public void actionPerformed(ActionEvent e) { 62 if (e.getSource() == prefItem) { 63 new PrefFrame(); 64 } 65 } 66 67 class HomePanel extends JPanel implements ActionListener { 68 JPanel viewPanel = new JPanel(); 69 JTextPane textPane = new JTextPane(); 70 JLabel imageLabel = new JLabel(); 71 JButton exprollBtn = new JButton("探検"); 72 JButton equipBtn = new JButton("装備"); 73 JPanel movePanel = new JPanel(); 74 75 HomePanel() { 76 setPreferredSize(new Dimension(PrefModel.WIDTH,PrefModel.HEIGHT)); 77 setLayout(new GridLayout(1, 2)); 78 viewPanel.setLayout(new GridLayout(2, 1)); 79 viewPanel.add(imageLabel); 80 viewPanel.add(textPane); 81 add(viewPanel); 82 83 movePanel.setLayout(new GridLayout(1, 2)); 84 movePanel.add(exprollBtn); 85 movePanel.add(equipBtn); 86 add(movePanel); 87 exprollBtn.addActionListener(this); 88 equipBtn.addActionListener(this); 89 90 } 91 92 public void actionPerformed(ActionEvent e) { 93 94 if (e.getSource() == exprollBtn) { 95 new ExprollFrame(); 96 StartController.mainFrame.setEnabled(false); 97 } 98 if (e.getSource() == equipBtn) { 99 new EquipmentFrame(); 100 StartController.mainFrame.setEnabled(false); 101 102 } 103 104 } 105 } 106 107 class ExprollFrame extends JFrame implements ActionListener { 108 JButton questBtn = new JButton("探索"); 109 JComboBox<String> dungeonBox; 110 String queDungeonName = ""; 111 112 ExprollFrame() { 113 setLayout(new FlowLayout()); 114 115 dungeonBox = new JComboBox<String>(jsonHelper.getDungeonName()); 116 dungeonBox.setPreferredSize(new Dimension(200, 50)); 117 getContentPane().add(dungeonBox); 118 questBtn.addActionListener(this); 119 getContentPane().add(questBtn); 120 121 setBounds(60, 60, 500, 500); 122 setVisible(true); 123 } 124 125 public void actionPerformed(ActionEvent e) { 126 if (e.getSource() == questBtn) { 127 DungeonModel.setDungeonData((String) dungeonBox.getSelectedItem()); 128 StartController.mainFrame.setEnabled(true); 129 dispose(); 130 } 131 } 132 } 133 134 class EquipmentFrame extends JFrame { 135 EquipmentFrame() { 136 setBounds(60, 60, 500, 500); 137 setVisible(true); 138 } 139 } 140 141 class PrefFrame extends JFrame implements ActionListener { 142 JPanel p1 = new JPanel(); 143 JPanel p2 = new JPanel(); 144 JTextField textField = new JTextField(); 145 JButton saveBtn = new JButton("save"); 146 JButton cancelBtn = new JButton("cancel"); 147 148 PrefFrame() { 149 setBounds(60, 60, 500, 500); 150 setLayout(new GridLayout(3, 1)); 151 getContentPane().add(textField); 152 getContentPane().add(saveBtn); 153 getContentPane().add(cancelBtn); 154 155 saveBtn.addActionListener(this); 156 cancelBtn.addActionListener(this); 157 158 setVisible(true); 159 } 160 161 public void actionPerformed(ActionEvent e) { 162 if (e.getSource() == saveBtn) { 163 PrefModel.WIDTH = Integer.valueOf(textField.getText()); 164 PrefModel.savePrefModel(); 165 } else if (e.getSource() == cancelBtn) { 166 dispose(); 167 } 168 } 169 }

java

1import java.io.FileReader; 2import java.io.FileWriter; 3import java.io.IOException; 4import java.util.Properties; 5 6public class PrefModel { 7 8 public static int WIDTH, HEIGHT; 9 10 public static void loadPrefModel() { 11 12 FileReader fr; 13 try { 14 fr = new FileReader("C:/java/pref.properties"); 15 Properties p = new Properties(); 16 p.load(fr); 17 WIDTH = Integer.parseInt(p.getProperty("widthSize")); 18 HEIGHT = Integer.parseInt(p.getProperty("heightSize")); 19 p.getProperty("widthSize"); 20 fr.close(); 21 } catch (IOException e) { 22 e.printStackTrace(); 23 } 24 25 } 26 27 public static void savePrefModel() { 28 FileWriter fw; 29 try { 30 fw = new FileWriter("C:/java/pref.properties"); 31 Properties p = new Properties(); 32 p.setProperty("widthSize", Integer.valueOf(WIDTH).toString()); 33 p.setProperty("heightSize", Integer.valueOf(HEIGHT).toString()); 34 p.store(fw, "sizePref"); 35 fw.close(); 36 } catch (IOException e) { 37 // TODO 自動生成された catch ブロック 38 e.printStackTrace(); 39 } 40 } 41 42 public static void createPrefModel() { 43 WIDTH = 700; 44 HEIGHT = 700; 45 FileWriter fw; 46 try { 47 fw = new FileWriter("C:/java/pref.properties"); 48 Properties p = new Properties(); 49 p.setProperty("widthSize", Integer.valueOf(WIDTH).toString()); 50 p.setProperty("heightSize", Integer.valueOf(HEIGHT).toString()); 51 p.store(fw, "sizePref"); 52 fw.close(); 53 } catch (IOException e) { 54 e.printStackTrace(); 55 } 56 } 57}

java

1import java.io.Serializable; 2 3public class PrefDate implements Serializable{ 4public int HEIGT,WIDTH; 5} 6

java

1import java.io.File; 2import java.io.IOException; 3import java.util.Vector; 4 5import com.fasterxml.jackson.databind.JsonNode; 6import com.fasterxml.jackson.databind.ObjectMapper; 7 8public class JsonHelper { 9 public Vector<String> getDungeonName() { 10 Vector<String> nameVector = new Vector<String>(); 11 File file = new File("C:/java/dungeonname.json"); 12 ObjectMapper mapper = new ObjectMapper(); 13 JsonNode rootNode; 14 try { 15 rootNode = mapper.readTree(file); 16 JsonNode currentNode; 17 for(int i=0;(currentNode = rootNode.get("dungeon").get(i)) != null; i++) { 18nameVector.add(currentNode.get("name").textValue()); 19 } 20 } catch (IOException e) { 21 e.printStackTrace(); 22 } 23 24 return nameVector; 25 } 26} 27

java

1 2public class DungeonModel { 3public static String queDungeonName = ""; 4public static boolean queBool; 5public static void setDungeonData(String nameString,boolean que){ 6 queDungeonName = nameString; 7 queBool = que; 8} 9public static String getDungeonName() { 10 return queDungeonName; 11} 12}

java

1public class TextDungeon { 2 3 public static void main(String[] args) { 4new StartController(); 5 } 6}

ファイル
pref.properties
#sizePref
#Sun Aug 27 20:03:34 JST 2023
widthSize=1200
heightSize=700

gungeonname.json
{
"dungeon":[
{"name":"はじまりのもり"},
{"name":"げんそうのさばく"},
{"name":"しんぴのうみ"}
]
}

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

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

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

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

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

TN8001

2023/08/28 13:13

> 内部クラスの新しいウィンドウのばってんボタンをクリックすると、プログラムがすべて終了してしまいます。 「×ボタンで閉じると終了し、探索ボタンで閉じたときは終了しない」という意味ですか? 「内部クラスでなく外部クラスなら終了しない」ということですか? > メインフレームを変数に入れなければ、新しいウィンドウを消しても古いウィンドウは残り思った通りに動きます。 逆なら「GCされてしまった」ような可能性がありますが、「入れなければ終了しない」ってのは謎ですね。 雑に不足コードを補って試しましたが、症状を再現できません。 提示以外の部分で「例外が出て丸ごと落ちている」ようなことはありませんか? > StartController.mainFrame.setEnabled(false); モーダル的に出すなら、JFrameよりJDialogがいいと思います。
jimbe

2023/08/28 14:34 編集

「コード全体」のコードをコピペしましたが、幾つかクラスが足りず実行できません。 問題を解決する場合に再現することが一番大事なことです。 問題を再現できる、コンパイル・実行可能なコードをご提示ください。 それと、 TN8001 さんが仰っている通り、 JFrame は通常は常に表示されているメインとなる画面 1 つのみに使用し、一時的なウインドウには JDialog 関係を使用するべきです。 JFrame 内を入れ替えるようなことなら、 JPanel を切り替えるような処理を行ってください。 ついでに「// TODO 自動生成されたコンストラクター・スタブ」のようなコメントはさっさと消してください。取っておく意味はありません。
Mitika-t

2023/08/28 20:11

ご指摘ありがとうございます。 コードの追加と編集をしました。 >TN8001さん ”「×ボタンで閉じると終了し、探索ボタンで閉じたときは終了しない」という意味ですか?” そうです。 ”「内部クラスでなく外部クラスなら終了しない」ということですか?” 内部クラスで作ったフレーム(ウィンドウ)の×ボタンをおすと、すべてのプログラムが終了してしまいます。外部クラスのウィンドウの×ボタンを押しても終了します "提示以外の部分で「例外が出て丸ごと落ちている」ようなことはありませんか?" eclipseのコンソール画面にはエラー表示はないみたいです。 ”モーダル的に出すなら、JFrameよりJDialogがいいと思います。” 私の勉強不足でした。ご指摘ありがとうございます。 >jimbeさん "JFrame は通常は常に表示されているメインとなる画面 1 つのみに使用し、一時的なウインドウには JDialog 関係を使用するべきです。" 私の勉強不足でした。ご指摘ありがとうございます。 ”「// TODO 自動生成されたコンストラクター・スタブ」のようなコメントはさっさと消してください。取っておく意味はありません。” 編集し削除しました。
jimbe

2023/08/29 02:36

編集ありがとうございます。
guest

回答2

0

自己解決

コードを読み直したところ{}ブロックの位置が間違っており、
そもそもインナークラスの構造になっておりませんでした。ごめんなさい。
コンソールにエラーは出ないものの、間違ったコードによっておかしい挙動をしていたみたいです。
修正後のコードを載せます

java

1 2import java.awt.Dimension; 3import java.awt.FlowLayout; 4import java.awt.GridLayout; 5import java.awt.event.ActionEvent; 6import java.awt.event.ActionListener; 7import java.io.File; 8 9import javax.swing.JButton; 10import javax.swing.JComboBox; 11import javax.swing.JDialog; 12import javax.swing.JFrame; 13import javax.swing.JLabel; 14import javax.swing.JMenu; 15import javax.swing.JMenuBar; 16import javax.swing.JMenuItem; 17import javax.swing.JPanel; 18import javax.swing.JTextField; 19import javax.swing.JTextPane; 20 21public class StartController { 22 static MainFrame mainFrame = null; 23 24 public StartController() { 25 // TODO 自動生成されたコンストラクター・スタブ 26 File file = new File("C:/java/pref.properties"); 27 28 if (file.exists()) { 29 PrefModel.loadPrefModel(); 30 } else if (!(file.exists())) { 31 PrefModel.createPrefModel(); 32 } 33 34 PrefModel.loadPrefModel(); 35 36 mainFrame = new MainFrame(); 37 } 38 39} 40 41class MainFrame extends JFrame implements ActionListener { 42 JMenuBar menuBar = new JMenuBar(); 43 JMenu confMenu = new JMenu("conf"); 44 JMenuItem prefItem = new JMenuItem("pref"); 45 HomePanel homePanel = new HomePanel(); 46 JsonHelper jsonHelper = new JsonHelper(); 47 48 MainFrame() { 49 // TODO 自動生成されたコンストラクター・スタブ 50 //setDefaultCloseOperation(EXIT_ON_CLOSE); 51 setSize(PrefModel.WIDTH, PrefModel.HEIGHT); 52 setLocationRelativeTo(rootPane); 53 54 confMenu.add(prefItem); 55 menuBar.add(confMenu); 56 setJMenuBar(menuBar); 57 getContentPane().add(homePanel); 58 prefItem.addActionListener(this); 59 60 pack(); 61 setResizable(false); 62 setVisible(true); 63 64 } 65 66 public void actionPerformed(ActionEvent e) { 67 // TODO 自動生成されたメソッド・スタブ 68 if (e.getSource() == prefItem) { 69new PrefFrame(); 70 } 71 } 72 73 class questPanel extends JPanel { 74 questPanel() { 75 // TODO 自動生成されたコンストラクター・スタブ 76 77 } 78 } 79 80 class HomePanel extends JPanel implements ActionListener { 81 JPanel viewPanel = new JPanel(); 82 JTextPane textPane = new JTextPane(); 83 JLabel imageLabel = new JLabel(); 84 JButton exprollBtn = new JButton("探検"); 85 JButton equipBtn = new JButton("装備"); 86 JPanel movePanel = new JPanel(); 87 88 HomePanel() { 89 setPreferredSize(new Dimension(PrefModel.WIDTH, PrefModel.HEIGHT)); 90 setLayout(new GridLayout(1, 2)); 91 viewPanel.setLayout(new GridLayout(2, 1)); 92 viewPanel.add(imageLabel); 93 viewPanel.add(textPane); 94 add(viewPanel); 95 96 movePanel.setLayout(new GridLayout(1, 2)); 97 movePanel.add(exprollBtn); 98 movePanel.add(equipBtn); 99 add(movePanel); 100 exprollBtn.addActionListener(this); 101 equipBtn.addActionListener(this); 102 103 } 104 105 public void actionPerformed(ActionEvent e) { 106 107 if (e.getSource() == exprollBtn) { 108 new ExprollFrame(); 109 } 110 if (e.getSource() == equipBtn) { 111 new EquipmentFrame(); 112 113 } 114 115 } 116 } 117 118 class ExprollFrame extends JDialog implements ActionListener { 119 JButton questBtn = new JButton("探索"); 120 JComboBox<String> dungeonBox; 121 String queDungeonName = ""; 122 123 ExprollFrame() { 124 setLayout(new FlowLayout()); 125 dungeonBox = new JComboBox<String>(jsonHelper.getDungeonName()); 126 dungeonBox.setPreferredSize(new Dimension(200, 50)); 127 getContentPane().add(dungeonBox); 128 questBtn.addActionListener(this); 129 getContentPane().add(questBtn); 130 131 setBounds(60, 60, 500, 500); 132 setVisible(true); 133 } 134 135 public void actionPerformed(ActionEvent e) { 136 // TODO 自動生成されたメソッド・スタブ 137 if (e.getSource() == questBtn) { 138 DungeonModel.setDungeonData((String) dungeonBox.getSelectedItem(), true); 139 StartController.mainFrame.setEnabled(true); 140 dispose(); 141 } 142 } 143 } 144 145 class EquipmentFrame extends JDialog { 146 EquipmentFrame() { 147 // TODO 自動生成されたコンストラクター・スタブ 148 setBounds(60, 60, 500, 500); 149 setVisible(true); 150 } 151 } 152 153 class PrefFrame extends JDialog implements ActionListener { 154 JPanel p1 = new JPanel(); 155 JPanel p2 = new JPanel(); 156 JTextField textField = new JTextField(); 157 JButton saveBtn = new JButton("save"); 158 JButton cancelBtn = new JButton("cancel"); 159 160 PrefFrame() { 161 // TODO 自動生成されたコンストラクター・スタブ 162 setBounds(60, 60, 500, 500); 163 setLayout(new GridLayout(3, 1)); 164 getContentPane().add(textField); 165 getContentPane().add(saveBtn); 166 getContentPane().add(cancelBtn); 167 168 saveBtn.addActionListener(this); 169 cancelBtn.addActionListener(this); 170 171 setVisible(true); 172 } 173 174 public void actionPerformed(ActionEvent e) { 175 // TODO 自動生成されたメソッド・スタブ 176 if (e.getSource() == saveBtn) { 177 PrefModel.WIDTH = Integer.valueOf(textField.getText()); 178 PrefModel.savePrefModel(); 179 } else if (e.getSource() == cancelBtn) { 180 dispose(); 181 } 182 } 183 } 184} 185

なぜおかしい挙動になっていたのかわかる方がいればコメント下さい。
よろしくお願いします。

投稿2023/08/28 21:11

編集2023/08/28 21:12
Mitika-t

総合スコア4

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

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

TN8001

2023/08/28 22:17 編集

> コードを読み直したところ{}ブロックの位置が間違っており、 > そもそもインナークラスの構造になっておりませんでした。 それぞれの子ウィンドウはstatic classにできそうでしたので、内部か外部かは関係ない気がします。 > コンソールにエラーは出ないものの、間違ったコードによっておかしい挙動をしていたみたいです。 かっこが足りない等そもそもコンパイルできてなくって、(コンパイルできていたころの)古いコードが実行されていたとか?? --- JDialogベースにされたようですね^^ でも現状モーダルになってません。 class EquipmentFrame extends JDialog { EquipmentFrame() { setBounds(60, 60, 500, 500); setModal(true); setVisible(true); } } のようにsetModal(true);をいれてください。 それで勝手にモーダルにしてくれるので、↓のようなコードは不要になります。 StartController.mainFrame.setEnabled(false); StartController.mainFrame.setEnabled(true); モーダル(ウィンドウ/ダイアログ): メモ帳の開くダイアログのように、閉じるまで元(メモ帳本体)のウィンドウをいじれないウィンドウ(やその状態) 対義語:モードレス(ウィンドウ/ダイアログ) [モーダルウィンドウ - Wikipedia](https://ja.wikipedia.org/wiki/%E3%83%A2%E3%83%BC%E3%83%80%E3%83%AB%E3%82%A6%E3%82%A3%E3%83%B3%E3%83%89%E3%82%A6)
guest

0

何がどこで必要になるのか分かりませんが編集・改造してみました。
まだ 1 ファイルに纏めたほうが簡単そうでしたので纏めています。
json 等はダミーにしています。

TextDungeon.java

java

1import java.awt.*; 2import java.io.*; 3import java.util.Properties; 4 5import javax.swing.*; 6 7public class TextDungeon extends JFrame { 8 public static void main(String[] args) throws IOException { 9 PrefModel prefModel = new PrefModel(new File("C:/java/pref.properties")); 10 SwingUtilities.invokeLater(() -> new TextDungeon(prefModel).setVisible(true)); 11 } 12 13 TextDungeon(PrefModel prefModel) { 14 super("TextDungeon"); 15 setDefaultCloseOperation(EXIT_ON_CLOSE); 16 setSize(prefModel.getWidth(), prefModel.getHeight()); 17 setResizable(false); 18 setLocationRelativeTo(null); 19 20 JsonHelper jsonHelper = new JsonHelper(); 21 22 JMenuBar menuBar = new JMenuBar(); 23 JMenu confMenu = new JMenu("conf"); 24 JMenuItem prefItem = new JMenuItem("pref"); 25 prefItem.addActionListener(v -> new PrefDialog(TextDungeon.this, prefModel).setVisible(true)); 26 confMenu.add(prefItem); 27 menuBar.add(confMenu); 28 setJMenuBar(menuBar); 29 30 add(new HomePanel(jsonHelper)); 31 } 32 33 private class HomePanel extends JPanel { 34 HomePanel(JsonHelper jsonHelper) { 35 super(new GridLayout(1, 2)); 36 37 JPanel viewPanel = new JPanel(new GridLayout(2, 1)); 38 JLabel imageLabel = new JLabel(); 39 viewPanel.add(imageLabel); 40 JTextPane textPane = new JTextPane(); 41 viewPanel.add(textPane); 42 add(viewPanel); 43 44 JPanel movePanel = new JPanel(new GridLayout(1, 2)); 45 JButton exprollBtn = new JButton("探検"); 46 exprollBtn.addActionListener(v -> new ExprollDialog(TextDungeon.this, jsonHelper).setVisible(true)); 47 movePanel.add(exprollBtn); 48 JButton equipBtn = new JButton("装備"); 49 equipBtn.addActionListener(v -> new EquipmentDialog(TextDungeon.this).setVisible(true)); 50 movePanel.add(equipBtn); 51 add(movePanel); 52 } 53 } 54 55 private static class DialogContentPanel extends JPanel { 56 DialogContentPanel(LayoutManager layoutManager) { 57 super(layoutManager); 58 setBorder(BorderFactory.createEmptyBorder(10,10,10,10));//ダイアログ共通の装飾 59 } 60 } 61 62 private static class ExprollDialog extends JDialog { 63 ExprollDialog(Frame owner, JsonHelper jsonHelper) { 64 super(owner, "ExprollDialog", true); 65 setBounds(60, 60, 500, 500); 66 setContentPane(new ContentPane(jsonHelper)); 67 setLocationRelativeTo(owner); 68 } 69 70 private class ContentPane extends DialogContentPanel { 71 ContentPane(JsonHelper jsonHelper) { 72 super(new FlowLayout()); 73 74 JComboBox<String> dungeonBox = new JComboBox<String>(jsonHelper.getDungeonName()); 75 add(dungeonBox); 76 77 JButton questBtn = new JButton("探索"); 78 questBtn.addActionListener(v -> { 79 DungeonModel.setDungeonData((String)dungeonBox.getSelectedItem()); 80 dispose(); 81 }); 82 add(questBtn); 83 } 84 } 85 } 86 87 private static class EquipmentDialog extends JDialog { 88 EquipmentDialog(Frame owner) { 89 super(owner, "EquipmentDialog", true); 90 setBounds(60, 60, 500, 500); 91 92 setLocationRelativeTo(owner); 93 } 94 } 95 96 private static class PrefDialog extends JDialog { 97 PrefDialog(Frame owner, PrefModel prefModel) { 98 super(owner, "PrefDialog", true); 99 setResizable(false); 100 setContentPane(new ContentPane(prefModel)); 101 pack(); 102 setLocationRelativeTo(owner); 103 } 104 105 private class ContentPane extends DialogContentPanel { 106 ContentPane(PrefModel prefModel) { 107 super(new GridBagLayout()); 108 109 GridBagConstraints gbc = new GridBagConstraints(); 110 gbc.gridx = 0; 111 gbc.gridy = 0; 112 gbc.anchor = GridBagConstraints.EAST; 113 gbc.insets = new Insets(0,0,0,10); 114 add(new JLabel("Frame Width:"), gbc); 115 116 JTextField textField = new JTextField(10); 117 String text = "" + prefModel.getWidth(); 118 textField.setText(text); 119 textField.setSelectionStart(0); 120 textField.setSelectionEnd(text.length()); 121 gbc.gridx = 1; 122 gbc.anchor = GridBagConstraints.WEST; 123 gbc.insets = new Insets(0,0,0,0); 124 add(textField, gbc); 125 126 gbc.gridx = 0; 127 gbc.gridy = 1; 128 add(Box.createVerticalStrut(16), gbc); 129 130 JButton cancelBtn = new JButton("Cancel"); 131 cancelBtn.addActionListener(v -> dispose()); 132 gbc.gridy = 2; 133 gbc.anchor = GridBagConstraints.WEST; 134 add(cancelBtn, gbc); 135 136 JButton saveBtn = new JButton("Save"); 137 saveBtn.addActionListener(v -> prefModel.setWidth(Integer.valueOf(textField.getText()))); 138 gbc.gridx = 1; 139 gbc.anchor = GridBagConstraints.EAST; 140 add(saveBtn, gbc); 141 } 142 } 143 } 144} 145 146//ダミー? 147class PrefModel { 148 private static final String PROPKEY_WIDTH = "widthSize"; 149 private static final String PROPKEY_HEIGHT = "heightSize"; 150 private static final int DEFAULT_WIDTH = 700; 151 private static final int DEFAULT_HEIGHT = 700; 152 153 private File file; 154 private Properties properties; 155 156 PrefModel(File file) { 157 this.file = file; 158 properties = new Properties(); 159 if(!file.exists()) return; 160 try { 161 properties.load(new FileInputStream(file)); 162 } catch(IOException e) { 163 System.err.println(e.getLocalizedMessage()); 164 } 165 } 166 void savePrefModel() { 167 try { 168 properties.store(new FileOutputStream(file), ""); 169 } catch(IOException e) { 170 System.err.println(e.getLocalizedMessage()); 171 } 172 } 173 int getWidth() { 174 try { 175 return Integer.parseInt(properties.getProperty(PROPKEY_WIDTH, ""+DEFAULT_WIDTH)); 176 } catch(NumberFormatException e) { 177 System.err.println(e.getLocalizedMessage()); 178 } 179 return DEFAULT_WIDTH; 180 } 181 void setWidth(int width) { 182 properties.put(PROPKEY_WIDTH, ""+width); 183 savePrefModel(); 184 } 185 int getHeight() { 186 try { 187 return Integer.parseInt(properties.getProperty(PROPKEY_HEIGHT, ""+DEFAULT_HEIGHT)); 188 } catch(NumberFormatException e) { 189 System.err.println(e.getLocalizedMessage()); 190 } 191 return DEFAULT_HEIGHT; 192 } 193 void setHeight(int height) { 194 properties.put(PROPKEY_HEIGHT, ""+height); 195 savePrefModel(); 196 } 197} 198//ダミー 199class JsonHelper { 200 String[] getDungeonName() { return new String[]{"はじまりのもり","げんそうのさばく","しんぴのうみ"}; } 201} 202//ダミー 203class DungeonModel { 204 static void setDungeonData(String string) {}; 205}

投稿2023/08/29 02:27

編集2023/08/29 02:40
jimbe

総合スコア12648

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問