2014-12-02 1 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

0

코드를 약간 변경했습니다.

먼저 SwingUtilities.invokeLater를 호출하여 Swing 응용 프로그램을 시작해야합니다.

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

둘째, 스윙 구성 요소를 사용합니다. 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); 
    } 

} 
관련 문제