Я не могу получить цвета фона кнопок или радиокнопок для работы с программой Java GUI - PullRequest
0 голосов
/ 03 апреля 2019

Целью этого домашнего задания является создание графического интерфейса, который позволяет пользователю изменять цвет фона и цвет текста окна. Кажется, я не могу понять, что я делаю неправильно, и почему моя программа не отображает различные цвета фона кнопок верхней панели, и почему переключатели не изменяют цвет текста сообщения в центр.

Я большой нуб, когда дело доходит до Java, поэтому мне не хватает понимания многих вещей. Вот мой код и спасибо за любую помощь.

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

public class ColorFactory extends JFrame 
{
   private final int WIDTH = 500;
   private final int HEIGHT = 300;

   //objects needed for GUI
   private JLabel message;
   private Container contentPane;
   private JPanel topPanel;
   private JPanel bottomPanel;
   private JButton redButton;
   private JButton yellowButton;
   private JButton orangeButton;
   private JRadioButton greenButton;
   private JRadioButton blueButton;
   private JRadioButton cyanButton;
   private ButtonGroup radioButtonGroup;

   public ColorFactory()
   {
      setTitle("Color Factory");
      setSize(WIDTH, HEIGHT);
      setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

      contentPane = getContentPane();
      contentPane.setLayout(new BorderLayout());

      buildTopPanel();
      contentPane.add(topPanel, BorderLayout.NORTH);

      buildBottomPanel();
      contentPane.add(bottomPanel, BorderLayout.SOUTH);

      message = new JLabel("Top buttons change the panel color and bottom radio buttons change the text color.");
      contentPane.add(message, BorderLayout.CENTER);
   }

   private void buildTopPanel()
   {
      topPanel = new JPanel();
      topPanel.setBackground (Color.white);
      topPanel.setLayout(new FlowLayout());

      redButton = new JButton("Red");
      redButton.setBackground(Color.red);
      redButton.setActionCommand("R");
      redButton.addActionListener(new ButtonListener());

      orangeButton = new JButton("Orange");
      orangeButton.setBackground(Color.orange);
      orangeButton.setActionCommand("O");
      orangeButton.addActionListener(new ButtonListener());

      yellowButton = new JButton("Yellow");
      yellowButton.setBackground(Color.yellow);
      yellowButton.setActionCommand("Y");
      yellowButton.addActionListener(new ButtonListener());

      topPanel.add(yellowButton);
      topPanel.add(redButton);
      topPanel.add(orangeButton);
   }

   private void buildBottomPanel()
   {
      bottomPanel = new JPanel();
      bottomPanel.setBackground (Color.white);
      bottomPanel.setLayout(new FlowLayout());

      JRadioButton greenButton = new JRadioButton("Green", true);
      greenButton.setForeground(Color.green);

      JRadioButton blueButton = new JRadioButton("Blue");
      blueButton.setForeground(Color.blue);

      JRadioButton cyanButton = new JRadioButton("Cyan");
      cyanButton.setForeground(Color.cyan);

      radioButtonGroup = new ButtonGroup();
      radioButtonGroup.add(greenButton);
      radioButtonGroup.add(blueButton);
      radioButtonGroup.add(cyanButton);

      greenButton.addActionListener(new RadioButtonListener());
      blueButton.addActionListener(new RadioButtonListener());
      cyanButton.addActionListener(new RadioButtonListener());  

      bottomPanel.add(greenButton);
      bottomPanel.add(blueButton);
      bottomPanel.add(cyanButton);
   }

   private class ButtonListener implements ActionListener
   {
      public void actionPerformed (ActionEvent event)
      {
         String whichButton = event.getActionCommand();
         if(whichButton.equals("R"))
         {
            contentPane.setBackground(Color.red);
         }
         else if (whichButton.equals("O"))
         {
            contentPane.setBackground(Color.orange);
         }
         else if (whichButton.equals("Y"))
        {
            contentPane.setBackground(Color.yellow);
         }
      }
   }

   private class RadioButtonListener implements ActionListener
   {
      public void actionPerformed (ActionEvent event)
      {
         if(event.getSource() == greenButton)
         {
            message.setForeground(Color.green);
        }
         else if (event.getSource() == blueButton)
         {
            message.setForeground(Color.blue);
         }
         else if (event.getSource() == cyanButton)
         {
            message.setForeground(Color.cyan);
         }
      }
   }

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

1 Ответ

0 голосов
/ 04 апреля 2019

Мне кажется, что вы не добавили свою панель контента в свой JFrame.После того, как вы наберете guilab.setVisible(true); вам нужно набрать guilab.add(contentPane);

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...