2011-03-16 102 views
2

我剛剛切換到mac,我遇到了一個問題。我有一個具有按鈕網格的java程序,但它不顯示顏色,我不能選擇它們。我試過它在Windows上,但它工作完美,我已更新我的Mac,安裝Xcode,更新Xcode。我在eclipse和JGrasp上試過。在這裏感謝gui代碼在windows和linux上工作,但沒有在mac上

的代碼:

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()); 
    } 
} 
+2

爲了更快地獲得更好的幫助,請發佈SSCCE(http://pscode.org/sscce.html),..(與200多行(不可編譯 - 無FloorPlan定義)代碼相對)。 – 2011-03-16 06:29:19

回答

1

Mac電腦看&的感覺是壓倒一切的一些默認值。你可以嘗試切換到不同的外觀&感覺你的目的。

相關問題