代码适用于Windows,但不适用于Mac

时间:2017-03-21 21:40:54

标签: java windows eclipse macos

我正处于制造战舰(刚刚开始计算机科学学位)的早期阶段,并且一直在Windows上使用Eclipse。到目前为止,我刚刚创建了网格,并在jbuttons中添加了一个动作侦测器,以便在单击时更改颜色。

当我在我的Windows PC上运行它时工作正常,但是当我尝试在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);
    }

}

正如我所说,它在我的Windows Eclipse上完美运行,但在Mac上却不行。

0 个答案:

没有答案