Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java gui代码在windows&;linux但不在mac上_Java_Macos_Swing_User Interface - Fatal编程技术网

Java gui代码在windows&;linux但不在mac上

Java gui代码在windows&;linux但不在mac上,java,macos,swing,user-interface,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 { //

我最近刚切换到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());
     }
}
import java.awt.*;
导入javax.swing.*;
导入java.awt.event.*;
公共类SafetyMapView扩展了JFrame{
//这些是瓷砖和纽扣
私人JPanel瓷砖;
私有JButton[]按钮;
私人JCheckBox编辑按钮;
专用JCheckBox选择退出按钮;
私有JButton计算;
专用按钮复位;
//菜单项
私有项目openItem;
私有项保存项;
私人物品出口;
私有JMenuItem editInfoItem;
公共JRadioButtonMenuItem显示距离;
公共JButton getFloorTileButton(inti){返回按钮[i];}
public JPanel getTilePanel(){return tiles;}
public JCheckBox getEditWallsButton(){返回editWallsButton;}
公共JCheckBox getSelectExitsButton(){返回selectExitsButton;}
//公共JRadioButtonMenuItem getShowDistance(){return showdistance;}
公共JButton getCompute(){return compute;}
公共JButton getReset(){return reset;}
//将侦听器添加到打开/保存/距离选项的方法
public void setOpenHandler(ActionListener x){
openItem.addActionListener(x);}
公共void setSaveHandler(ActionListener x){
saveItem.addActionListener(x);}
public void setEditInfoHandler(ActionListener x){
editInfoItem.addActionListener(x);}
//此构造函数构建面板
公共安全地图视图(字符串标题、平面布置图平面布置图){
超级(标题);
//创建带有地砖的面板
创建楼层平面面板(楼层平面);
//布局窗口的其余组件
设置组件();
添加菜单();
setDefaultCloseOperation(关闭时退出);
设置大小(600500);
setVisible(真);
}
//创建包含显示楼层平面的按钮的面板
专用void CreateFloorplan面板(FloorPlan FloorPlan){
//使用按钮设置面板
按钮=新的JButton[floorPlan.size()*floorPlan.size()];
tiles=新的JPanel();
tiles.setLayout(新的GridLayout(floorPlan.size(),floorPlan.size());
//将按钮添加到平铺面板

对于(int r=0;rMac Look&Feel正在覆盖一些默认设置。您可以尝试根据自己的目的切换到不同的Look&Feel。

要更快获得更好的帮助,请发布SSCCE()…而不是超过200行(不可编译-无平面图定义)代码。