2017-03-21 66 views
0

私は戦艦(コンピュータ科学の学位を始めたばかり)を作る初期段階にあり、Windows上でEclipseを使用していました。これまではグリッドを作成してjbuttonにactionlistenerを追加していましたので、クリックすると色が変わります。コードはWindowsでは動作しますが、Macでは動作しません

Windows PCで実行するとうまく動作しますが、Macで実行しようとすると基本的なグリッドが表示され、色などは変わりません。

誰でも問題がありましたか?私はなぜそれが1つではなく他のものでは動作しないのか理解できません。それは場合に役立ちます。ここ

おかげ

は、私が言うように、それは私のWindowsのEclipse上ではなく、Mac上で完璧に動作

public class BattleshipFrame { 
    public static void main(String[] args) { 

     //creating JFrame 
     JFrame frame = new JFrame("Battleship"); 
     frame.setLayout(new GridLayout(1, 3)); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     //creating User Panel 
     JPanel userpanel = new JPanel(); 
     userpanel.setLayout(new GridLayout(10, 10)); 
     userpanel.setSize(400, 400); 

     //creating JButton array 
     JButton[] button = new JButton[100]; 

     //putting JButtons in User Panel 
     for (int i = 0; i < 100; i++) { 
      button[i] = new JButton(); 
      button[i].setBackground(Color.BLUE); 
      userpanel.add(button[i]); 


      //changing colour of buttons when clicked 
      button[i].addActionListener(new ActionListener() { 
       @Override 
       public void actionPerformed(ActionEvent ev) { 
        Object source = ev.getSource(); 
        if (source instanceof Component) { 
         ((Component) source).setBackground(Color.GREEN); 
        } 
       } 
      }); 
     } 

     //creating computer panel 
     JPanel comppanel = new JPanel(); 
     comppanel.setLayout(new GridLayout(10, 10)); 
     comppanel.setSize(400, 400); 

     //putting JButtons in User Panel 
     for (int i = 0; i < 100; i++) { 
      button[i] = new JButton(); 
      button[i].setBackground(Color.BLUE); 
      comppanel.add(button[i]); 


      //changing colour of buttons when clicked 
      button[i].addActionListener(new ActionListener() { 
       @Override 
       public void actionPerformed(ActionEvent ev) { 
        Object source = ev.getSource(); 
        if (source instanceof Component) { 
         ((Component) source).setBackground(Color.GREEN); 
        } 
       } 
      }); 
     } 

     //creating menu panel 
     JPanel menupanel = new JPanel(); 


     //creating buttons for menu 
     JButton save = new JButton("Save"); 
     JButton load = new JButton("Load"); 

     save.setPreferredSize(new Dimension(100, 100)); 
     load.setPreferredSize(new Dimension(100, 100)); 

     //adding buttons to menu panel 
     menupanel.add(save); 
     menupanel.add(load); 

     //adding panels into frame 
     frame.add(userpanel, BorderLayout.WEST); 
     frame.add(menupanel, BorderLayout.CENTER); 
     frame.add(comppanel, BorderLayout.EAST); 
     frame.setVisible(true); 
     frame.setSize(2000, 1000); 
    } 

} 

(私はそれが今エレガントか何かではありません知っている)、私のコードです

+2

Macコンピュータでデバッグを試みましたか? –

+0

[なぜ誰かが私を助けることができますか?実際の質問ではありませんか?](http://meta.stackoverflow.com/q/284236) – Andreas

+0

ここでは、ボタンの配列はおそらく悪い選択です。ボードをJPanelの独自のサブクラスにしてみてください。コンポーネントにマウスリスナーをアタッチし、x、y座標のmathを使用してクリックを取得できます。 – Charlie

答えて

0

はい、Mac OSではこのプロパティを設定する必要があります。

button[i] = new JButton(); 
button[i].setOpaque(true); 

が、これはそれを動作させるでしょう。この

UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()) 

を追加するために私の答えを編集しました。

+0

これは私に何か色を与えてくれました。あなたの助けてくれてありがとうございます。しかし、それはちょうどjbuttonのバックグラウンドではなくボーダーを設定するだけです。 – DJCuzzles

+0

これはバックグラウンドカラーを修正しますが、UIはデフォルトになります UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); – 7663233

関連する問題