某野球ゲームのミニゲームである地雷原を進むゲームを作り出したところです。
今はボタンを押すと格子状のラベルが描画されるコードを書いていました。
が、ボタンを押しても何の反応もせずウインドウを最小化して戻したり、タイトルバーをつかんでドラッグで描画部分を下に隠して戻したりすると正しく表示されます。
ボーダーが表示されてないだけで実際はそこに描画されてるのかなと思い背景色も変えたりしましたが、やはり表示部分を何らかの方法で隠して再表示しないと描画されません。
ボタンを押したらすぐ表示されるようにするにはどうすればいいのでしょうか?
java
1public class Mine { 2 public static void main(String[] args) { 3 Swing mine = new Swing("地雷ゲーム"); 4 Process.process = mine; //処理クラスに渡す 5 mine.setVisible(true); 6 mine.setLocationRelativeTo(null); 7 mine.setResizable(false); 8 } 9}
java
1import javax.swing.*; 2import java.awt.*; 3import javax.swing.border.*; 4import java.awt.event.*; 5import java.util.*; 6 7class Swing extends JFrame implements ActionListener{ 8 JButton[] difficultyBtns = new JButton[3]; 9 JPanel p1 = new JPanel(); 10 11 Swing(String title){ 12 setTitle(title); 13 setSize(800, 600); 14 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 15 p1 = new JPanel(); 16 p1.setLayout(null); 17 p1.setBounds(0, 0, 800, 600); 18 19 difficultyBtns[0] = new JButton("easy"); 20 difficultyBtns[1] = new JButton("normal"); 21 difficultyBtns[2] = new JButton("hard"); 22 23 difficultyBtns[0].setBounds(220, 10, 100, 30); 24 difficultyBtns[1].setBounds(350, 10, 100, 30); 25 difficultyBtns[2].setBounds(480, 10, 100, 30); 26 27 for (int i=0; i<3; i++) { 28 difficultyBtns[i].setLayout(null); 29 difficultyBtns[i].setBorder(new LineBorder(Color.black, 1, false)); 30 difficultyBtns[i].addActionListener(this); 31 difficultyBtns[i].setFont(new Font("メイリオ", Font.PLAIN, 18)); 32 difficultyBtns[i].setHorizontalAlignment(JLabel.CENTER); 33 difficultyBtns[i].setVerticalAlignment(JLabel.CENTER); 34 p1.add(difficultyBtns[i]); 35 } 36 difficultyBtns[0].setActionCommand("easy"); 37 difficultyBtns[1].setActionCommand("normal"); 38 difficultyBtns[2].setActionCommand("hard"); 39 40 add(p1); 41 } 42 43 public void actionPerformed(ActionEvent e){ 44 String cmd = e.getActionCommand(); 45 int difficulty = 0; 46 if(cmd.equals("easy")) { 47 difficulty = 11; 48 Process.makeBtnEasy(); 49 } else if (cmd.equals("normal")) { 50 difficulty = 21; 51 Process.makeBtnNormal(); 52 } else { 53 difficulty = 31; 54 } 55 Maze.s_main(difficulty); //穴掘り法で迷路を作るメソッド 全て拾い物 56 } 57}
java
1import javax.swing.*; 2import java.awt.*; 3import javax.swing.border.*; 4import java.awt.event.*; 5import java.util.*; 6 7public class Process { 8 public static Swing process = new Swing(""); 9 10 public static void makeBtnEasy() { //easyでの描画 11 JLabel[][] labels = new JLabel[9][9]; 12 13 for(int row=0; row<labels.length; row++) { 14 for(int col=0; col<labels[row].length; col++) { 15 labels[row][col] = new JLabel(); 16 labels[row][col].setBounds(265+(col*30),100+(row*30), 30, 30); 17 //labels[row][col].setLayout(null); 18 //↑これが関係してるかと思いコメントアウトしたが直らなかった 19 labels[row][col].setBorder(new LineBorder(Color.black, 1, false)); 20 process.p1.add(labels[row][col]); 21 } 22 } 23 } 24 25 public static void makeBtnNormal() { //normalでの描画 26 JLabel[][] labels = new JLabel[19][19]; 27 28 for(int row=0; row<labels.length; row++) { 29 for(int col=0; col<labels[row].length; col++) { 30 labels[row][col] = new JLabel(); 31 labels[row][col].setBounds(210+(col*20),100+(row*20), 20, 20); 32 //labels[row][col].setLayout(null); 33 labels[row][col].setBackground(Color.GREEN); //背景色設定してみたがダメだった 34 labels[row][col].setOpaque(true); 35 labels[row][col].setBorder(new LineBorder(Color.black, 1, false)); 36 process.p1.add(labels[row][col]); 37 } 38 } 39 } 40}
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/05/10 11:01