2014-06-11 35 views
0

我正在編寫一個使用JButton的程序。當用戶點擊一個按鈕時,背景應該改變顏色,但是不能通過actionPerformed()方法訪問JFrame。有人可以告訴我如何使其工作?使用JButton更改JFrame中的背景色

import java.awt.event.*; 
import java.awt.*; 
import javax.swing.JFrame; 
import javax.swing.JButton; 
import javax.swing.JPanel; 
public class HandlerClass implements ActionListener{ 

    public static void main(String[] args){ 
    HandlerClass handler = new HandlerClass(); 
    final JFrame f = new JFrame("Testing out these JPanels"); 
    f.setSize(400, 100); 
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    f.setLocationRelativeTo(null); 
    f.setLayout(new GridLayout(2, 3)); 
    JButton b = new JButton("button 1"); 
    b.addActionListener(new HandlerClass()); 
    JButton butt = new JButton("button 2"); 

    JButton bug = new JButton("button 3"); 

    JButton button = new JButton("button 4"); 

    JButton button5 = new JButton("button 5"); 

    JButton button6 = new JButton("button 6"); 

    JPanel p = new JPanel(); 
    p.setVisible(true); 
    JPanel pnl = new JPanel(); 
    p.add(b); 
    p.add(butt); 
    p.add(bug); 
    pnl.add(button); 
    pnl.add(button5); 
    pnl.add(button6); 
    f.add(p, BorderLayout.CENTER); 
    f.add(pnl, BorderLayout.SOUTH); 
    f.setVisible(true); 
    f.setBackground(Color.RED); 
    } 
    public void actionPerformed(ActionEvent e){ 
     f.setBackground(Color.WHITE); 
     } 

    } 

回答

4
  1. 不要寫在一個巨大的主要方法,所有的代碼。這是針對排名初學者計劃的,如果你希望你的代碼比hello世界做得更多,你將不得不把它變成一個真正的OOPs計劃。
  2. 使用非靜態字段和方法創建一個類。
  3. ActionListener調用了一個方法,該方法在其主體中更改了您的主JPanel的背景顏色,即您添加到JFrame的contentPane的背景顏色。
  4. 考慮使用匿名內部監聽器類,然後將監聽器代碼的肉卸載到單獨的方法中,無論是在GUI中還是在控制類中。
2

您有幾種選擇:

  1. 移動JFrame實例的主體之外,使之成爲一個實例變量這樣:

    public class HandlerClass { 
        private JFrame frame; 
    
        public HandlerClass() { 
         ... 
        } 
    } 
    
  2. 移動ActionListener內相同的方法:

    public class HandlerClass { 
        public HandlerClass() { 
         final JFrame frame = new JFrame(); 
         JButton button = new JButton(); 
         ... 
         button.addActionListener(new ActionerListener() { 
          @Override 
          public void actionPerformed(ActionEvent e){ 
           frame.setBackground(Color.WHITE); 
          } 
         } 
        } 
    } 
    

    第二個選擇的唯一問題是您必須使用final修飾符參考frame,這意味着you can't change the reference later

  3. 執行上述兩項操作。

0

您的問題:

JFrame can't be accessed from the actionPerformed() 

解決的辦法是,你不能從其他方法來訪問一個局部變量 看看herehere

現在最重要的事情: 你在您的JFrame上有兩個JPanel s。因此,如果您更改JFrame的顏色,那麼您將無法通過 查看顏色變化。所以我的建議是改變JPanel的顏色。 採取下面的代碼的例子:

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.EventQueue; 

import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.border.EmptyBorder; 
import java.awt.GridBagLayout; 
import javax.swing.JButton; 
import java.awt.GridBagConstraints; 
import java.awt.Insets; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

public class HandlerClass extends JFrame { 

    private JPanel contentPane; 

    /** 
    * Launch the application. 
    */ 
    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       try { 
        HandlerClass frame = new HandlerClass(); 
        frame.setVisible(true); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

    /** 
    * Create the frame. 
    */ 
    public HandlerClass() { 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     contentPane = new JPanel(); 
     contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
     setContentPane(contentPane); 
     GridBagLayout gbl_contentPane = new GridBagLayout(); 
     gbl_contentPane.columnWidths = new int[]{0, 0, 0, 0, 0, 0, 0}; 
     gbl_contentPane.rowHeights = new int[]{0, 0, 0, 0}; 
     gbl_contentPane.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE}; 
     gbl_contentPane.rowWeights = new double[]{0.0, 0.0, 0.0, Double.MIN_VALUE}; 
     contentPane.setLayout(gbl_contentPane); 

     JButton btnRed = new JButton("Red"); 
     GridBagConstraints gbc_btnRed = new GridBagConstraints(); 
     gbc_btnRed.insets = new Insets(0, 0, 5, 5); 
     gbc_btnRed.gridx = 3; 
     gbc_btnRed.gridy = 1; 
     contentPane.add(btnRed, gbc_btnRed); 

     JButton btnBlue = new JButton("Blue"); 
     GridBagConstraints gbc_btnBlue = new GridBagConstraints(); 
     gbc_btnBlue.insets = new Insets(0, 0, 5, 0); 
     gbc_btnBlue.gridx = 5; 
     gbc_btnBlue.gridy = 1; 
     contentPane.add(btnBlue, gbc_btnBlue); 

     JButton btnWhite = new JButton("White"); 
     GridBagConstraints gbc_btnWhite = new GridBagConstraints(); 
     gbc_btnWhite.insets = new Insets(0, 0, 0, 5); 
     gbc_btnWhite.gridx = 3; 
     gbc_btnWhite.gridy = 2; 
     contentPane.add(btnWhite, gbc_btnWhite); 

     JButton btnGreen = new JButton("Green"); 
     GridBagConstraints gbc_btnGreen = new GridBagConstraints(); 
     gbc_btnGreen.gridx = 5; 
     gbc_btnGreen.gridy = 2; 
     contentPane.add(btnGreen, gbc_btnGreen); 

     btnRed.addActionListener(new ActionListener() { 

      public void actionPerformed(ActionEvent e) { 
       contentPane.setBackground(Color.red); 

      } 
     }); 
     btnBlue.addActionListener(new ActionListener() { 

      public void actionPerformed(ActionEvent e) { 

       contentPane.setBackground(Color.blue); 
      } 
     }); 
     btnWhite.addActionListener(new ActionListener() { 

      public void actionPerformed(ActionEvent e) { 
       contentPane.setBackground(Color.WHITE); 

      } 
     }); 
     btnGreen.addActionListener(new ActionListener() { 

      public void actionPerformed(ActionEvent e) { 

       contentPane.setBackground(Color.green); 
      } 
     }); 

     pack(); 
    } 

} 

屏幕截圖: enter image description here