2014-04-29 108 views
1

儘管我努力改變按鈕的顏色,並且在任何地方閱讀如何操作,我的代碼仍然不會產生所需的效果。點擊時如何更改JButton 的顏色?在ActionPerformed上更改JButton顏色

public void generateButtons 
{ 
    //Global field 
    firstBoard = new JButton[9][9]; 

     for(int x = 0; x < firstBoard.length; x++) 
     { 
      for(int y = 0; y < firstBoard[0].length; y++) 
      { 
       firstBoard[x][y] = new JButton(); 
       firstBoard[x][y].setActionCommand("0|" + (x + (firstBoard.length * y))); 
       firstBoard[x][y].addActionListener(this); 
       //firstBoardPanel.add(firstBoard[x][y]); 
      } 
     } 
} 

@Override 
    public void actionPerformed(ActionEvent e) 
    { 
     if(e.getSource() instanceof JButton) 
     { 
      System.out.println(parseActionCommand(((JButton)e.getSource()).getActionCommand())); 
      ((JButton)e.getSource()).setBackground(Color.BLUE); 
      ((JButton)e.getSource()).setContentAreaFilled(false); 
      ((JButton)e.getSource()).setOpaque(true); 
     } 
    } 
+1

已經運行你的代碼,它只是對我很好。你使用什麼外觀和感覺?我試過Windows,Metal和Nimbus,它們都工作得很好 – MadProgrammer

+0

我在Mac上運行,這可能是外觀和感覺。 –

+0

啊,是的,不能測試:P – MadProgrammer

回答

1

這不是一個答案,但延長的測試用例來測試的Mac OS外觀和雨雲,當其在Windows操作系統上運行的區別,工作

它的出現,這成了一個答案......

試試這個下手......

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.EventQueue; 
import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.UIManager; 
import javax.swing.UIManager.LookAndFeelInfo; 
import javax.swing.UnsupportedLookAndFeelException; 

public class ChangeButton { 

    public static void main(String[] args) { 
     new ChangeButton(); 
    } 

    public ChangeButton() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
       } 

       JFrame frame = new JFrame("Testing"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLayout(new BorderLayout()); 
       frame.add(new TestPane()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class TestPane extends JPanel implements ActionListener { 

     private final JButton[][] firstBoard; 

     public TestPane() { 
      setLayout(new GridLayout(9, 9)); 
      firstBoard = new JButton[9][9]; 

      for (int x = 0; x < firstBoard.length; x++) { 
       for (int y = 0; y < firstBoard[0].length; y++) { 
        firstBoard[x][y] = new JButton(); 
        firstBoard[x][y].setActionCommand("0|" + (x + (firstBoard.length * y))); 
        firstBoard[x][y].addActionListener(this); 
        add(firstBoard[x][y]); 
       } 
      } 
     } 

     public void actionPerformed(ActionEvent e) { 
      if (e.getSource() instanceof JButton) { 
       System.out.println((((JButton) e.getSource()).getActionCommand())); 
       ((JButton) e.getSource()).setBackground(Color.BLUE); 
       ((JButton) e.getSource()).setContentAreaFilled(false); 
       ((JButton) e.getSource()).setOpaque(true); 
      } 
     } 
    } 

} 

然後更換UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());這個...

for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) { 
    if ("Nimbus".equals(info.getName())) { 
     UIManager.setLookAndFeel(info.getClassName()); 
     break; 
    } 
} 

,然後再試一次

-1

不要直接使用e.getSource()你需要首先它的對象引用,這使用它:

例如:

ublic void actionPerformed(ActionEvent e) 
{ 
    Object ob = e.getSource(); 
    if(ob instanceof JButton) 
    { 
     System.out.println(parseActionCommand(((JButton)e.getSource()).getActionCommand())); 
     JButton button = ((JButton)ob); 
     button.setBackground(Color.BLUE); 
     button.setContentAreaFilled(false); 
     button.setOpaque(true); 
    } 
+0

這不會改變任何東西。它仍然不會改變顏色。 –

+0

@AlanW並打印?點擊按鈕? –

+0

是的,它確實打印。 –

1

嘗試重新粉刷它包含按鈕的容器。

 

button.getParent().validate(); 
button.getParent().repaint(); 

+0

'revalidate'會是一個更好的方法,當我使用Windows測試它時,Metal和Nimbus的外觀和感覺如何不需要... – MadProgrammer