2014-04-08 3 views
0

GUI에서 작업 중이며 창에 문제가 없습니다.
내 GUI는 (topPanebottomPane)의 두 부분으로 나뉩니다.ContentPane의 배경색 변경

두 창에는 버튼과 라벨이 있지만 버튼 배경 중 하나 인 의 배경색을으로 변경하려고했지만 버튼 기능이 작동하지 않습니다.

내가 한 것은 Container (thisContentPane)을 사용하여 전체 GUI의 배경색을 변경 한 것입니다. 나는 버튼이나 메뉴 항목을 클릭하면

import java.awt.*; 
import javax.swing.*; 
import java.awt.event.*; 

public class TempConverter extends JFrame 
{ 
    //Creating a contentPane down inside the inner class 
    Container thisContentPane; 
    //class scope variables : DO NOT CREATE THIS OBJECTS HERE. 
    JButton calculateButton, clearButton; 
    JTextField celsiusField, fahrenheitField, kelvinField; 

    //menu 
    JMenuBar menuBar = new JMenuBar(); 
    JMenu backgroundColor = new JMenu("Background Color"); 
    JMenu help = new JMenu("Help"); 

    JMenuItem lightGray, white, black, blue, howToUse, about; 


    //constructor 
    TempConverter() 
    { 
     super("Temperature Converter App"); 

     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     this.setLayout(new BorderLayout()); 
     this.setSize(400,200);; 
     this.setLocationRelativeTo(null); 


     //menuBar 
     this.setJMenuBar(menuBar); 


     menuBar.add(backgroundColor); 

     //adding JMenu to JMenuBar 
     menuBar.add(backgroundColor); 
     menuBar.add(help); 

     //adding JMenuItems 
     lightGray = backgroundColor.add("LIGHTGRAY"); 
     white = backgroundColor.add("WHITE"); 
     black = backgroundColor.add("BLACK"); 
     blue = backgroundColor.add("BLUE"); 

     howToUse = help.add("How To Use"); 
     about = help.add("Help"); 


     //babysitter 
     MaryPoppins babysitter = new MaryPoppins(); 

     //adding action listener to the menu item 
     lightGray.addActionListener(babysitter); 
     white.addActionListener(babysitter); 
     black.addActionListener(babysitter); 
     blue.addActionListener(babysitter); 
     howToUse.addActionListener(babysitter); 
     about.addActionListener(babysitter); 


     //building JPanels 
     JPanel topPanel = new JPanel(); 
     topPanel.setLayout(new GridLayout(3,2,0,20)); 

     //add this to JFrame in centerzone 
     this.add(topPanel, BorderLayout.CENTER); 

     //bottom panel 
     JPanel bottomPanel = new JPanel(); 
     bottomPanel.setLayout(new FlowLayout()); 

     //add this to JFrame in bottom 
     this.add(bottomPanel, BorderLayout.SOUTH); 

     //add components to the panels 
     //add the buttons 
     calculateButton = new JButton("Calculate"); 
     clearButton = new JButton("Clear"); 

     //add buttons 
     bottomPanel.add(calculateButton); 
     bottomPanel.add(clearButton); 

     //register listeners 
     calculateButton.addActionListener(babysitter); 
     clearButton.addActionListener(babysitter); 

     //add components to the top panel 
     JLabel labelOne = new JLabel("Celsius:"); 
     JLabel secondOne = new JLabel("Fahrenheit:"); 
     JLabel thirdOne = new JLabel("Kelvin:"); 

     celsiusField = new JTextField(""); 
     fahrenheitField = new JTextField(""); 
     kelvinField = new JTextField(""); 

     //add the label and text fields 
     topPanel.add(labelOne); 
     topPanel.add(celsiusField); 
     topPanel.add(secondOne); 
     topPanel.add(fahrenheitField); 
     topPanel.add(thirdOne); 
     topPanel.add(kelvinField); 

     this.setVisible(true); 

    } // end constructor 

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


    private class MaryPoppins implements ActionListener 
    { 

     //implement the abstract method from the interface 
     public void actionPerformed(ActionEvent ev) 
     { 
      thisContentPane = getContentPane(); 

      if(ev.getActionCommand().equals("LIGHTGRAY")) 
      { 
       thisContentPane.setBackground(Color.lightGray); 
      } 
      else if (ev.getActionCommand().equals("BLUE")) 
      { 
       thisContentPane.setBackground(Color.BLUE); 
      } 
      else if(ev.getActionCommand().equals("WHITE")) 
      { 
       thisContentPane.setBackground(Color.WHITE); 
      } 
      else if (ev.getActionCommand().equals("BLACK")) 
      { 
       thisContentPane.setBackground(Color.BLACK); 
      }else if (ev.getActionCommand().equals("Clear")) 
      { 
       thisContentPane.setBackground(Color.BLACK); 
      } 
      else if (ev.getActionCommand().equals("BLACK")) 
      { 
       thisContentPane.setBackground(Color.BLACK); 
      } 



     }//end ActionPerformed() 

    }//end inner class 

} // end class 

는 아무것도하지 않습니다

여기에 내 현재 코드입니다.

답변

2

귀하의 문제는 contentPanel의 배경 색상이 "볼 수"없습니다 점이다 : topPanel하고 bottomPanel이 그 위에 :

에있는 당신의 당신은해야 하나 수행

if (ev.getActionCommand().equals("LIGHTGRAY")) { 
    thisTopPanel.setBackground(Color.lightGray); 
    thisBottemPanel.setBackground(Color.lightGray); 
} 

... 그리고 당신의 각 if 조건을 위해 (당신은 내가 의미하는 것을 안다).

하지만 실제로가는 것이 가장 좋은 방법은 아닙니다. 내 의견으로는, 그것은 당신이 찾고있는 정확한 동작을 반영 원인 '완벽한 의미가 있습니다 대안은 다음과 같습니다

topPanel.setOpaque(false); 
bottomPanel.setOpaque(false); 

내가 분명히 두 번째 옵션을 추천 할 것입니다)


나는 그것을 해요 때문에, 나는 이러한 별칭 상수는 대문자해야한다는 내용의 규칙을 존중하기 때문에, 대신 Color.lightGrey의 (등을 Color.BLACK, Color.WHITE) Color.LIGHTGRAY를 사용하는 것을 선호합니다.

+0

당신은 setOpaque (true)를 의미합니까? – Jarvis

+0

어쨌든 나는 멍청이다. – Jarvis

+0

아니, 아니, 실제로는'setOpaque (false)'를 의미한다. 이것은 패널을 투명하게 만들 것이므로'topPanel'과'bottomPanel'이 그것을 덮고 있어도 contentPane의 배경색이 변하는 것을 볼 수 있습니다. 그냥 시도해보고 작동 여부를 알려주십시오.) – ccjmne