BorderLayout不工作JFrame

时间:2014-10-03 07:23:06

标签: java swing jframe jpanel layout-manager

出于某种原因,我无法让BorderLayout设定它应该采用的方式。只是想知道我哪里出错了。

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class ColorFactory extends JFrame
{

    final int width = 500;
    final int height = 300;

    private JPanel buttonPanel;
    private JPanel radioButtonPanel;
    private JLabel msgChangeColor;

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

        setLayout(new BorderLayout());

        createTopPanel();
        add(buttonPanel, BorderLayout.NORTH);

        createBottomPanel();
        add(radioButtonPanel, BorderLayout.SOUTH);

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

        pack();
    }

    private void createTopPanel()
    {   
        buttonPanel = new JPanel();
        setLayout(new FlowLayout());

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

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

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

        buttonPanel.add(redButton);
        buttonPanel.add(orangeButton);
        buttonPanel.add(yellowButton);

    }

    private void createBottomPanel()
    {
        radioButtonPanel = new JPanel();
        setLayout(new FlowLayout());

        JRadioButton greenRadioButton = new JRadioButton("Green");
        greenRadioButton.setBackground(Color.GREEN);
        greenRadioButton.addActionListener(new RadioButtonListener());
        greenRadioButton.setActionCommand("G");

        JButton blueRadioButton = new JButton("Blue");
        blueRadioButton.setBackground(Color.BLUE);
        blueRadioButton.addActionListener(new RadioButtonListener());
        blueRadioButton.setActionCommand("B");

        JButton cyanRadioButton = new JButton("Cyan");
        cyanRadioButton.setBackground(Color.CYAN);
        cyanRadioButton.addActionListener(new RadioButtonListener());
        cyanRadioButton.setActionCommand("C");

        radioButtonPanel.add(greenRadioButton);
        radioButtonPanel.add(blueRadioButton);
        radioButtonPanel.add(cyanRadioButton);
    }

    private class ButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            String actionColor = e.getActionCommand();
            if(actionColor.equals("R"))
            {
                buttonPanel.setBackground(Color.RED);
                radioButtonPanel.setBackground(Color.RED);
            }

            if(actionColor.equals("O"))
            {
                buttonPanel.setBackground(Color.ORANGE);
                radioButtonPanel.setBackground(Color.ORANGE);
            }

            if(actionColor.equals("Y"))
            {
                buttonPanel.setBackground(Color.YELLOW);
                radioButtonPanel.setBackground(Color.YELLOW);
            }
        }
    }
        private class RadioButtonListener implements ActionListener
        {
            public void actionPerformed(ActionEvent e)
            {
                String actionTextColor = e.getActionCommand();
                if(actionTextColor.equals("G"))
                {
                    msgChangeColor.setForeground(Color.GREEN);
                }

                if(actionTextColor.equals("B"))
                {
                    msgChangeColor.setForeground(Color.BLUE);
                }

                if(actionTextColor.equals("C"))
                {
                    msgChangeColor.setForeground(Color.CYAN);
                }
            }
    }

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

}

2 个答案:

答案 0 :(得分:1)

问题是您在创建顶部和底部面板时正在更改框架的布局管理器...

private void createTopPanel() {
    buttonPanel = new JPanel();
    setLayout(new FlowLayout()); // <--- This is call setLayout on the frame

这就是为什么......危险的原因。

  • 直接从JFrame延伸......
  • 动态构建组件

很容易丢失上下文并开始影响你实际上并不想要的组件......

答案 1 :(得分:1)

另一个问题(除了MadProgrammer发布的问题)是你将组件添加到JFrame本身。

您应该通过调用JFrame.getContentPane()将内容添加到框架的内容窗格中。

示例:

JFrame f = new JFrame("Test");
Container c = f.getContentPane();
c.add(new JButton("In Center"), BorderLayout.CENTER);
c.add(new JButton("At the Bottom"), BorderLayout.SOUTH);
c.add(new JButton("At the Top"), BorderLayout.NORTH);
c.add(new JButton("On the Left"), BorderLayout.WEST);
c.add(new JButton("On the Right"), BorderLayout.EAST);

您可以致电JFrame.setContentPane()来设置/更改内容面板。默认内容面板已经有BorderLayout,因此您甚至无需更改它,也无需设置新面板。