実現したいこと
私は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":"しんぴのうみ"}
]
}
回答2件
あなたの回答
tips
プレビュー