【问题标题】:Changing panel backgrounds with a borderlayout Manager使用边框布局管理器更改面板背景
【发布时间】:2014-04-29 12:34:10
【问题描述】:

我正在尝试设置我的按钮以使用边框布局管理器更改按钮所在的面板背景颜色。当我使用 flowlayout 时我没有问题,但无法通过边框布局来解决。我觉得我错过了一些基本的东西。我发现了类似的带有面板和颜色变化的线程,但没有一个能够回答我的问题。到目前为止,这是我所拥有的:

import java.awt.*;     // Needed for BorderLayout class
import javax.swing.*;  // Needed for Swing classes
import java.awt.event.*;//Needed for Action Listener


public class BorderPanelWindow extends JFrame
{
   public BorderPanelWindow()
   { 
      // Set the title bar text.
      setTitle("Border Layout");

      // Specify an action for the close button.
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      // Add a BorderLayout manager to the content pane.
      setLayout(new BorderLayout());

      // Create five panels.
      JPanel panel1 = new JPanel();
      JPanel panel2 = new JPanel();
      JPanel panel3 = new JPanel();
      JPanel panel4 = new JPanel();
      JPanel panel5 = new JPanel();

      // Create five buttons.
      JButton rbutton = new JButton("Red");
      JButton bbutton = new JButton("Blue");
      JButton gbutton = new JButton("Green");
      JButton ybutton = new JButton("Yellow");
      JButton obutton = new JButton("Orange");

      //create actions for the buttons
      ColorChanger yellowAction = new ColorChanger(Color.YELLOW); 
      ColorChanger redAction = new ColorChanger(Color.RED); 
      ColorChanger blueAction = new ColorChanger(Color.BLUE);
      ColorChanger greenAction = new ColorChanger(Color.GREEN);
      ColorChanger orangeAction = new ColorChanger(Color.ORANGE);

      //set actions for the buttons
      rbutton.addActionListener(redAction);
      bbutton.addActionListener(blueAction);
      gbutton.addActionListener(greenAction);
      ybutton.addActionListener(yellowAction);
      obutton.addActionListener(orangeAction);


      // Add the buttons to the panels.
      panel1.add(rbutton);
      panel2.add(bbutton);
      panel3.add(gbutton);
      panel4.add(ybutton);
      panel5.add(obutton);

      // Add the five panels to the content pane.
      add(panel1, BorderLayout.NORTH);
      add(panel2, BorderLayout.SOUTH);
      add(panel3, BorderLayout.EAST);
      add(panel4, BorderLayout.WEST);
      add(panel5, BorderLayout.CENTER);

      // Pack and display the window.
      pack();
      setVisible(true);
   }

   private class ColorChanger implements ActionListener 
   { 
      //fields
      private Color backgroundColor;

      //constructor
      public ColorChanger(Color c) 
      { 
         backgroundColor = c; 
      } 


      public void actionPerformed(ActionEvent e) 
      {
         setBackground(backgroundColor); 
      }  

   } 



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

我已经设法让代码编译

【问题讨论】:

  • 你的问题是什么?
  • 如何使用每个面板中的按钮更改面板背景颜色?

标签: java swing background-color panels border-layout


【解决方案1】:

试试这个

    public void actionPerformed(ActionEvent e) {
        for (int i = 0; i < getContentPane().getComponentCount(); i++) {
            getContentPane().getComponent(i).setBackground(backgroundColor);
        }
    }

您正在更改 JFrame 的颜色,它是添加到其中的所有 JPanel 的父级。您必须设置添加到其中的所有JPanel的背景颜色。


--编辑--

根据您的最后评论 - 我只希望按钮所在的区域改变颜色

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() instanceof JButton) {
            ((JButton) e.getSource()).getParent().setBackground(backgroundColor);
        }
    }

【讨论】:

  • 你太棒了!这是一个巨大的帮助,但你能帮我理解它吗?我不明白 getContentPane().getComponent(i) 是如何/引用什么的?
  • 阅读here
  • 直接来自JFrame 的JavaDoc,上面写着: 根窗格提供的内容窗格通常应包含JFrame 显示的所有非菜单组件。这与 AWT Frame 的情况不同。作为方便的 add 及其变体,remove 和 setLayout 已被覆盖以根据需要转发到 contentPane。
  • 抱歉忽略我之前的帖子。这很接近,但我只希望按钮所在的区域改变颜色?
  • 在我的帖子中查找更新。获取JButton 的父级JPanel 并更改背景颜色。
猜你喜欢
  • 2017-07-01
  • 2013-10-15
  • 2019-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-29
  • 2013-10-02
相关资源
最近更新 更多