gui代码在windows& linux但不是mac

时间:2011-03-16 05:36:35

标签: java macos swing user-interface

我刚刚切换到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());
     }
}

1 个答案:

答案 0 :(得分:1)

Mac Look&amp; Feel覆盖了一些默认设置。您可以尝试切换到不同的外观和感觉。