2014-12-02 56 views
0

我想創建一個包含標籤和按鈕的Java GUI應用程序。點擊按鈕後,第一個面板的背景顏色會發生變化。我有標籤和按鈕,但每次點擊按鈕時都會出現錯誤。另外,我希望第一塊麪板原本有黃色背景,然後切換到任何顏色。這裏是我的代碼:改變背景與按鈕的GUI應用程序

import javax.swing.JFrame; 
import javax.swing.JPanel; 
import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.GridLayout; 
import java.awt.FlowLayout; 
import javax.swing.JButton; 
import java.awt.event.ActionListener; 
import java.awt.event.ActionEvent; 
import javax.swing.JLabel; 

public class ChangeDemo extends JFrame implements ActionListener 
{ 
public static final int WIDTH = 300; 
public static final int HEIGHT= 200; 
private JPanel biggerPanel; 

public static void main(String[] args) 
{ 
    ChangeDemo gui = new ChangeDemo(); 
    gui.setVisible(true); 
} 

public ChangeDemo() 
{ 
    super ("ChangeBackgroundDemo"); 
    setSize(WIDTH,HEIGHT); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setLayout(new GridLayout(2,3));  

    JPanel biggerPanel = new JPanel(); 
    biggerPanel.setLayout(new BorderLayout()); 
    biggerPanel.setBackground(Color.YELLOW); 

    JLabel namePanel = new JLabel("Click the button to change the background color"); 
    biggerPanel.add(namePanel, BorderLayout.NORTH); 


    add(namePanel); 
    JPanel buttonPanel = new JPanel(); 
    buttonPanel.setLayout(new FlowLayout()); 
    buttonPanel.setBackground(Color.LIGHT_GRAY); 

    JButton changeButton = new JButton("Change Color"); 
    changeButton.addActionListener(this); 
    buttonPanel.add(changeButton); 

    add(buttonPanel); 
} 

public void actionPerformed(ActionEvent e) 
{ 
    String buttonString = e.getActionCommand(); 

    if(buttonString.equals("Change Color")) 
     biggerPanel.setBackground(Color.RED);    
    else 
     System.out.println("Unexpected Error!"); 
} 




} 

回答

0

下面是基於修改你的代碼工作演示,還沒來得及整理一下,但希望你會得到它的要點。問題在於你沒有將面板添加到邊界(北部,南部等)以便爲它們着色。希望這有助於。

import javax.swing.JFrame; 
import javax.swing.JPanel; 
import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.GridLayout; 
import java.awt.FlowLayout; 
import javax.swing.JButton; 
import java.awt.event.ActionListener; 
import java.awt.event.ActionEvent; 
import javax.swing.JLabel; 

public class ChangeDemo extends JFrame implements ActionListener 
{ 
public static final int WIDTH = 300; 
public static final int HEIGHT= 200; 
private JPanel biggerPanel = new JPanel(); 
private JPanel namePanel = new JPanel(); 

public static void main(String[] args) 
{ 
    ChangeDemo gui = new ChangeDemo(); 
    gui.setVisible(true); 
} 

public ChangeDemo() 
{ 
super ("ChangeBackgroundDemo"); 
setSize(WIDTH,HEIGHT); 
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
setLayout(new GridLayout(2,3));  

//JPanel biggerPanel = new JPanel(); 
this.biggerPanel.setLayout(new BorderLayout()); 
this.biggerPanel.setBackground(Color.YELLOW); 

JLabel nameLabel = new JLabel("Click the button to change the background color"); 
namePanel.add(nameLabel); 
namePanel.setBackground(Color.YELLOW); 
//this.biggerPanel.add(namePanel, BorderLayout.NORTH); 


add(namePanel); 
JPanel buttonPanel = new JPanel(); 
buttonPanel.setLayout(new FlowLayout()); 
buttonPanel.setBackground(Color.LIGHT_GRAY); 

JButton changeButton = new JButton("Change Color"); 
changeButton.addActionListener(this); 
changeButton.setActionCommand("Change Color"); 
buttonPanel.add(changeButton); 

add(buttonPanel); 
} 

public void actionPerformed(ActionEvent e) 
{ 
    String buttonString = e.getActionCommand(); 
     if(buttonString.equals("Change Color")) 
      this.namePanel.setBackground(Color.RED);    
     else 
     System.out.println("Unexpected Error!"); 
} 

} 
+0

我試過,但沒有工作。當我點擊按鈕時仍然出現錯誤。 – JavaBeginner 2014-12-02 17:22:51

0

我對你的代碼做了一些修改。

首先,您必須通過調用SwingUtilities.invokeLater啓動Swing應用程序。

public static void main(String[] args) { 
    SwingUtilities.invokeLater(new ChangeDemo()); 
} 

其次,您使用Swing組件。當你想覆蓋Swing組件的一個方法時,你只擴展一個Swing組件。

第三,我專門爲你的JButton做了一個動作監聽器。這樣,你不必檢查特定的JButton字符串。您可以根據需要爲GUI創建多個動作偵聽器。

JButton changeButton = new JButton("Change Color"); 
    changeButton.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent event) { 
      isYellow = !isYellow; 

      if (isYellow) buttonPanel.setBackground(Color.YELLOW); 
      else buttonPanel.setBackground(Color.RED); 
     } 
    }); 

最後,我改變了JButton面板的背景顏色。

下面是整個ChangeDemo類。

package com.ggl.testing; 

import java.awt.Color; 
import java.awt.FlowLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.BoxLayout; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 

public class ChangeDemo implements Runnable { 

    private boolean isYellow; 

    private JFrame frame; 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new ChangeDemo()); 
    } 

    @Override 
    public void run() { 
     frame = new JFrame("Change Background Demo"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     JPanel mainPanel = new JPanel(); 
     mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS)); 

     JPanel namePanel = new JPanel(); 
     JLabel nameLabel = new JLabel(
       "Click the button to change the background color"); 
     nameLabel.setAlignmentX(JLabel.CENTER_ALIGNMENT); 
     namePanel.add(nameLabel); 

     mainPanel.add(namePanel); 

     final JPanel buttonPanel = new JPanel(); 
     buttonPanel.setLayout(new FlowLayout()); 
     buttonPanel.setBackground(Color.YELLOW); 
     isYellow = true; 

     JButton changeButton = new JButton("Change Color"); 
     changeButton.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent event) { 
       isYellow = !isYellow; 

       if (isYellow) buttonPanel.setBackground(Color.YELLOW); 
       else buttonPanel.setBackground(Color.RED); 
      } 
     }); 

     buttonPanel.add(changeButton); 

     mainPanel.add(buttonPanel); 

     frame.add(mainPanel); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 

}