GUI 代码可在 Windows 和 Windows 上运行linux 但不在 mac 上

发布于 2024-10-22 09:44:32 字数 8440 浏览 1 评论 0原文

我最近刚切换到mac,遇到了一个问题。我有一个java程序,有按钮网格,但它不显示颜色,我无法选择它们。我在 Windows 上尝试过,但它工作得很好,我已经更新了我的 mac,安装了 Xcode,更新了 Xcode。我在 eclipse 和 JGrasp 上尝试过。谢谢

这是代码:

import java.awt.*;

import javax.swing.*;
import java.awt.event.*;

public class SafetyMapView extends JFrame {

    // These are the tiles and buttons
    private JPanel          tiles;
    private JButton[]       buttons;
    private JCheckBox       editWallsButton;
    private JCheckBox       selectExitsButton;
    private JButton         compute;
    private JButton         reset;
    // Menu items
    private JMenuItem       openItem;
    private JMenuItem       saveItem;
    private JMenuItem       exitItem;
    private JMenuItem       editInfoItem;
    public JRadioButtonMenuItem         showdistance;

    public JButton getFloorTileButton(int i) { return buttons[i]; }
    public JPanel getTilePanel() { return tiles; }
    public JCheckBox getEditWallsButton() { return editWallsButton; }
    public JCheckBox getSelectExitsButton() { return selectExitsButton; }
    //public JRadioButtonMenuItem getShowDistance(){return showdistance;}
    public JButton getCompute(){return compute;}
    public JButton getReset(){return reset;}
    // Methods for adding listeners to the open/save/distance options
    public void setOpenHandler(ActionListener x) {
        openItem.addActionListener(x); }
    public void setSaveHandler(ActionListener x) {
        saveItem.addActionListener(x); }
    public void setEditInfoHandler(ActionListener x) {
        editInfoItem.addActionListener(x); }

    // This constructor builds the panel
    public SafetyMapView(String title, FloorPlan floorPlan) {
        super(title);

        // Create the panel with the floor tiles on it
        createFloorPlanPanel(floorPlan);

        // Layout the rest of the window's components
        setupComponents();
        addMenus();

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(600, 500);
        setVisible(true);
    }


    // Create the panel to contain the buttons that display the floor plan
    private void createFloorPlanPanel(FloorPlan floorPlan) {
        // Setup the panel with the buttons
        buttons = new JButton[floorPlan.size()*floorPlan.size()];
        tiles = new JPanel();
        tiles.setLayout(new GridLayout(floorPlan.size(),floorPlan.size()));

        // Add the buttons to the tile panel
        for (int r=0; r<floorPlan.size(); r++) {
            for (int c=0; c<floorPlan.size(); c++) {
                int i = r * floorPlan.size() + c;
                buttons[i] = new JButton();
                buttons[i].setBorder(BorderFactory.createLineBorder(Color.lightGray));
                buttons[i].setBackground(Color.white);

                tiles.add(buttons[i]);
            }
        }
    }

    // Here we add all the components to the window accordingly
    private void setupComponents() {
        // Layout the components using a gridbag
        GridBagLayout layout = new GridBagLayout();
        GridBagConstraints layoutConstraints = new GridBagConstraints();
        getContentPane().setLayout(layout);

        // Add the tiles
        layoutConstraints.gridx = 0; layoutConstraints.gridy = 0;
        layoutConstraints.gridwidth = 1; layoutConstraints.gridheight = 6;
        layoutConstraints.fill = GridBagConstraints.BOTH;
        layoutConstraints.insets = new Insets(2, 2, 2, 2);
        layoutConstraints.anchor = GridBagConstraints.NORTHWEST;
        layoutConstraints.weightx = 1.0; layoutConstraints.weighty = 1.0;
        layout.setConstraints(tiles, layoutConstraints);
        getContentPane().add(tiles);

        JLabel aLabel = new JLabel("MODE:");
        layoutConstraints.gridx = 1; layoutConstraints.gridy = 0;
        layoutConstraints.gridwidth = 1; layoutConstraints.gridheight = 1;
        layoutConstraints.fill = GridBagConstraints.NONE;
        layoutConstraints.insets = new Insets(2, 2, 2, 2);
        layoutConstraints.anchor = GridBagConstraints.NORTHWEST;
        layoutConstraints.weightx = 0.0; layoutConstraints.weighty = 0.0;
        layout.setConstraints(aLabel, layoutConstraints);
        getContentPane().add(aLabel);

        JLabel bLabel = new JLabel("ComPutE:");
        layoutConstraints.gridx = 1; layoutConstraints.gridy = 3;
        layoutConstraints.gridwidth = 1; layoutConstraints.gridheight = 1;
        layoutConstraints.fill = GridBagConstraints.NONE;
        layoutConstraints.insets = new Insets(2, 2, 2, 2);
        layoutConstraints.anchor = GridBagConstraints.WEST;
        layoutConstraints.weightx = 0.0; layoutConstraints.weighty = 0.0;
        layout.setConstraints(bLabel, layoutConstraints);
        getContentPane().add(bLabel);

        // Add the EditWalls button
        editWallsButton = new JCheckBox("Edit Walls");
        layoutConstraints.gridx = 1; layoutConstraints.gridy = 1;
        layoutConstraints.gridwidth = 1; layoutConstraints.gridheight = 1;
        layoutConstraints.fill = GridBagConstraints.BOTH;
        layoutConstraints.insets = new Insets(2, 2, 2, 2);
        layoutConstraints.anchor = GridBagConstraints.CENTER;
        layoutConstraints.weightx = 0.0; layoutConstraints.weighty = 0.0;
        layout.setConstraints(editWallsButton, layoutConstraints);
        getContentPane().add(editWallsButton);

        // Add the SelectExits button
        selectExitsButton = new JCheckBox("Select Exits");
        layoutConstraints.gridx = 1; layoutConstraints.gridy = 2;
        layoutConstraints.gridwidth = 1; layoutConstraints.gridheight = 1;
        layoutConstraints.fill = GridBagConstraints.BOTH;
        layoutConstraints.insets = new Insets(2, 2, 2, 2);
        layoutConstraints.anchor = GridBagConstraints.CENTER;
        layoutConstraints.weightx = 0.0; layoutConstraints.weighty = 0.0;
        layout.setConstraints(selectExitsButton, layoutConstraints);
        getContentPane().add(selectExitsButton);

        compute = new JButton("compute safety plan");
        layoutConstraints.gridx = 1; layoutConstraints.gridy = 4;
        layoutConstraints.gridwidth = 1; layoutConstraints.gridheight = 1;
        layoutConstraints.fill = GridBagConstraints.BOTH;
        layoutConstraints.insets = new Insets(2, 2, 2, 2);
        layoutConstraints.anchor = GridBagConstraints.CENTER;
        layoutConstraints.weightx = 0.0; layoutConstraints.weighty = 0.0;
        layout.setConstraints(compute, layoutConstraints);
        getContentPane().add(compute);

        reset = new JButton("reset safety plan");
        layoutConstraints.gridx = 1; layoutConstraints.gridy = 5;
        layoutConstraints.gridwidth = 1; layoutConstraints.gridheight = 1;
        layoutConstraints.fill = GridBagConstraints.NORTH;
        layoutConstraints.insets = new Insets(2, 2, 2, 2);
        layoutConstraints.anchor = GridBagConstraints.CENTER;
        layoutConstraints.weightx = 0.0; layoutConstraints.weighty = 0.0;
        layout.setConstraints(reset, layoutConstraints);
        getContentPane().add(reset);
     }

     private void addMenus() {
        JMenuBar menubar = new JMenuBar();
        setJMenuBar(menubar);

        // Make the file menu
        JMenu file = new JMenu("File");
        file.setMnemonic('F');
        menubar.add(file);

        openItem = new JMenuItem("Open Floor Plan");
        saveItem = new JMenuItem("Save Floor Plan");
        exitItem = new JMenuItem("Quit");
        openItem.setMnemonic('O');
        saveItem.setMnemonic('S');
        exitItem.setMnemonic('Q');
        file.add(openItem);
        file.add(saveItem);
        file.add(exitItem);
        exitItem.addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    System.exit(0);
                }});

        // Make the options menu
        JMenu view = new JMenu("Options");
        view.setMnemonic('O');
        menubar.add(view);

        JMenu edit = new JMenu("Exits");
        edit.setMnemonic('E');
        view.add(edit);

        editInfoItem = new JMenuItem("Edit Info");
        editInfoItem.setMnemonic('E');
        edit.add(editInfoItem);

        showdistance = new JRadioButtonMenuItem("show Distance");
        showdistance.setMnemonic('D');
        view.add(showdistance);


     }

     public static void main(String args[]) {
        new SafetyMapView("Fire Safety Routes", FloorPlan.example1());
     }
}

i just switched to mac recently and i encountered a problem. i have a java program that have grids of buttons but it doesnt shows the color and i cant select them. i tried it on windows but it worked perfectly, i already updatec my mac, installed Xcode, updated the Xcode. i tried it on eclipse and JGrasp. thanks

here's the code:

import java.awt.*;

import javax.swing.*;
import java.awt.event.*;

public class SafetyMapView extends JFrame {

    // These are the tiles and buttons
    private JPanel          tiles;
    private JButton[]       buttons;
    private JCheckBox       editWallsButton;
    private JCheckBox       selectExitsButton;
    private JButton         compute;
    private JButton         reset;
    // Menu items
    private JMenuItem       openItem;
    private JMenuItem       saveItem;
    private JMenuItem       exitItem;
    private JMenuItem       editInfoItem;
    public JRadioButtonMenuItem         showdistance;

    public JButton getFloorTileButton(int i) { return buttons[i]; }
    public JPanel getTilePanel() { return tiles; }
    public JCheckBox getEditWallsButton() { return editWallsButton; }
    public JCheckBox getSelectExitsButton() { return selectExitsButton; }
    //public JRadioButtonMenuItem getShowDistance(){return showdistance;}
    public JButton getCompute(){return compute;}
    public JButton getReset(){return reset;}
    // Methods for adding listeners to the open/save/distance options
    public void setOpenHandler(ActionListener x) {
        openItem.addActionListener(x); }
    public void setSaveHandler(ActionListener x) {
        saveItem.addActionListener(x); }
    public void setEditInfoHandler(ActionListener x) {
        editInfoItem.addActionListener(x); }

    // This constructor builds the panel
    public SafetyMapView(String title, FloorPlan floorPlan) {
        super(title);

        // Create the panel with the floor tiles on it
        createFloorPlanPanel(floorPlan);

        // Layout the rest of the window's components
        setupComponents();
        addMenus();

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(600, 500);
        setVisible(true);
    }


    // Create the panel to contain the buttons that display the floor plan
    private void createFloorPlanPanel(FloorPlan floorPlan) {
        // Setup the panel with the buttons
        buttons = new JButton[floorPlan.size()*floorPlan.size()];
        tiles = new JPanel();
        tiles.setLayout(new GridLayout(floorPlan.size(),floorPlan.size()));

        // Add the buttons to the tile panel
        for (int r=0; r<floorPlan.size(); r++) {
            for (int c=0; c<floorPlan.size(); c++) {
                int i = r * floorPlan.size() + c;
                buttons[i] = new JButton();
                buttons[i].setBorder(BorderFactory.createLineBorder(Color.lightGray));
                buttons[i].setBackground(Color.white);

                tiles.add(buttons[i]);
            }
        }
    }

    // Here we add all the components to the window accordingly
    private void setupComponents() {
        // Layout the components using a gridbag
        GridBagLayout layout = new GridBagLayout();
        GridBagConstraints layoutConstraints = new GridBagConstraints();
        getContentPane().setLayout(layout);

        // Add the tiles
        layoutConstraints.gridx = 0; layoutConstraints.gridy = 0;
        layoutConstraints.gridwidth = 1; layoutConstraints.gridheight = 6;
        layoutConstraints.fill = GridBagConstraints.BOTH;
        layoutConstraints.insets = new Insets(2, 2, 2, 2);
        layoutConstraints.anchor = GridBagConstraints.NORTHWEST;
        layoutConstraints.weightx = 1.0; layoutConstraints.weighty = 1.0;
        layout.setConstraints(tiles, layoutConstraints);
        getContentPane().add(tiles);

        JLabel aLabel = new JLabel("MODE:");
        layoutConstraints.gridx = 1; layoutConstraints.gridy = 0;
        layoutConstraints.gridwidth = 1; layoutConstraints.gridheight = 1;
        layoutConstraints.fill = GridBagConstraints.NONE;
        layoutConstraints.insets = new Insets(2, 2, 2, 2);
        layoutConstraints.anchor = GridBagConstraints.NORTHWEST;
        layoutConstraints.weightx = 0.0; layoutConstraints.weighty = 0.0;
        layout.setConstraints(aLabel, layoutConstraints);
        getContentPane().add(aLabel);

        JLabel bLabel = new JLabel("ComPutE:");
        layoutConstraints.gridx = 1; layoutConstraints.gridy = 3;
        layoutConstraints.gridwidth = 1; layoutConstraints.gridheight = 1;
        layoutConstraints.fill = GridBagConstraints.NONE;
        layoutConstraints.insets = new Insets(2, 2, 2, 2);
        layoutConstraints.anchor = GridBagConstraints.WEST;
        layoutConstraints.weightx = 0.0; layoutConstraints.weighty = 0.0;
        layout.setConstraints(bLabel, layoutConstraints);
        getContentPane().add(bLabel);

        // Add the EditWalls button
        editWallsButton = new JCheckBox("Edit Walls");
        layoutConstraints.gridx = 1; layoutConstraints.gridy = 1;
        layoutConstraints.gridwidth = 1; layoutConstraints.gridheight = 1;
        layoutConstraints.fill = GridBagConstraints.BOTH;
        layoutConstraints.insets = new Insets(2, 2, 2, 2);
        layoutConstraints.anchor = GridBagConstraints.CENTER;
        layoutConstraints.weightx = 0.0; layoutConstraints.weighty = 0.0;
        layout.setConstraints(editWallsButton, layoutConstraints);
        getContentPane().add(editWallsButton);

        // Add the SelectExits button
        selectExitsButton = new JCheckBox("Select Exits");
        layoutConstraints.gridx = 1; layoutConstraints.gridy = 2;
        layoutConstraints.gridwidth = 1; layoutConstraints.gridheight = 1;
        layoutConstraints.fill = GridBagConstraints.BOTH;
        layoutConstraints.insets = new Insets(2, 2, 2, 2);
        layoutConstraints.anchor = GridBagConstraints.CENTER;
        layoutConstraints.weightx = 0.0; layoutConstraints.weighty = 0.0;
        layout.setConstraints(selectExitsButton, layoutConstraints);
        getContentPane().add(selectExitsButton);

        compute = new JButton("compute safety plan");
        layoutConstraints.gridx = 1; layoutConstraints.gridy = 4;
        layoutConstraints.gridwidth = 1; layoutConstraints.gridheight = 1;
        layoutConstraints.fill = GridBagConstraints.BOTH;
        layoutConstraints.insets = new Insets(2, 2, 2, 2);
        layoutConstraints.anchor = GridBagConstraints.CENTER;
        layoutConstraints.weightx = 0.0; layoutConstraints.weighty = 0.0;
        layout.setConstraints(compute, layoutConstraints);
        getContentPane().add(compute);

        reset = new JButton("reset safety plan");
        layoutConstraints.gridx = 1; layoutConstraints.gridy = 5;
        layoutConstraints.gridwidth = 1; layoutConstraints.gridheight = 1;
        layoutConstraints.fill = GridBagConstraints.NORTH;
        layoutConstraints.insets = new Insets(2, 2, 2, 2);
        layoutConstraints.anchor = GridBagConstraints.CENTER;
        layoutConstraints.weightx = 0.0; layoutConstraints.weighty = 0.0;
        layout.setConstraints(reset, layoutConstraints);
        getContentPane().add(reset);
     }

     private void addMenus() {
        JMenuBar menubar = new JMenuBar();
        setJMenuBar(menubar);

        // Make the file menu
        JMenu file = new JMenu("File");
        file.setMnemonic('F');
        menubar.add(file);

        openItem = new JMenuItem("Open Floor Plan");
        saveItem = new JMenuItem("Save Floor Plan");
        exitItem = new JMenuItem("Quit");
        openItem.setMnemonic('O');
        saveItem.setMnemonic('S');
        exitItem.setMnemonic('Q');
        file.add(openItem);
        file.add(saveItem);
        file.add(exitItem);
        exitItem.addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    System.exit(0);
                }});

        // Make the options menu
        JMenu view = new JMenu("Options");
        view.setMnemonic('O');
        menubar.add(view);

        JMenu edit = new JMenu("Exits");
        edit.setMnemonic('E');
        view.add(edit);

        editInfoItem = new JMenuItem("Edit Info");
        editInfoItem.setMnemonic('E');
        edit.add(editInfoItem);

        showdistance = new JRadioButtonMenuItem("show Distance");
        showdistance.setMnemonic('D');
        view.add(showdistance);


     }

     public static void main(String args[]) {
        new SafetyMapView("Fire Safety Routes", FloorPlan.example1());
     }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜光 2024-10-29 09:44:32

Mac 外观和感觉会覆盖一些默认设置。您可以尝试根据您的目的切换到不同的外观和感觉。

The Mac Look&Feel is overriding some of the defaults. You could try switching to a different look&feel for your purpose.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文