1

異なるSwing コンポーネントJPanelを含む5 つの異なる が必要なアプリケーションを設計しています。部分的には、適切な解決策が見つからない問題に遭遇しました。「radioSizePanel」は、メイン パネルの上部中央のどこかに配置されるはずです。以前にこの問題を解決した人はいますか? これが私が使用しているコードです: JRadioButton

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

public class PizzaShop extends JFrame 
{
    private static final long serialVersionUID = 1L;
    private JRadioButton[] radio_size = new JRadioButton[3];
    private JRadioButton[] radio_type = new JRadioButton[3];
    public PizzaShop() 
    {
        initializaUI();
    }

    private void initializaUI()
    {
        setSize(700, 600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //Panel container to wrap checkboxes and radio buttons
        JPanel panel = new JPanel();

        //sizes radio buttons
        String Size[] = {"Small: $6.50", "Medium: $8.50", "Large: $10.00"};
        JPanel radioSizePanel = new JPanel(new GridLayout(3, 1));
        ButtonGroup radioSizeGroup = new ButtonGroup();
        for (int i=0; i<3; i++)
        {
            radio_size[i] = new JRadioButton(Size[i]);
            radioSizePanel.add(radio_size[i]);
            radioSizeGroup.add(radio_size[i]);
        }
        radioSizePanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Size"));
        radioSizePanel.setPreferredSize(new Dimension(100, 200));

        //
        panel.add(radioSizePanel);
        setContentPane(panel);
        setContentPane(radioSizePanel);
    }

    public static void main(String[] args) 
    {
        // TODO Auto-generated method stub
        SwingUtilities.invokeLater(new Runnable() 
        {
            public void run() 
            {
                new PizzaShop().setVisible(true);
            }
        });
    }
}

期待される OUTPUT として私が望むものは次のとおりです。

期待される出力

4

1 に答える 1

2

コード例をよく見てくださいJFrame。あなたの例では、setSize()何かがに追加されるずっと前に呼び出しているJFrameため、最初にコンポーネントをコンテナに追加してから、pack()/setSize()そのメソッドを呼び出して、それを適切に実現できるようにします。

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

public class PizzaLayout
{
    /*
     * Five JPanels we will be using.
     */
    private JPanel headerPanel;
    private JPanel footerPanel;
    // This JPanel will contain the middle components.
    private JPanel centerPanel;
    private JPanel toppingPanel;
    private JPanel sizePanel;
    private JPanel typePanel;
    private JPanel buttonPanel;

    private String[] toppings = {
                                    "Tomato",
                                    "Green Pepper",
                                    "Black Olives",
                                    "Mushrooms",
                                    "Extra Cheese",
                                    "Pepproni",
                                    "Sausage"
                                };

    private JCheckBox[] toppingsCBox;

    private String[] sizePizza = {
                                    "Small $6.50", 
                                    "Medium $8.50",
                                    "Large $10.00"
                                 };

    private JRadioButton[] sizePizzaRButton;

    private String[] typePizza = {
                                    "Thin Crust", 
                                    "Medium Crust",
                                    "Pan"
                                 };

    private JRadioButton[] typePizzaRButton;

    private JButton processButton;

    private ButtonGroup bGroupType, bGroupSize;

    private JTextArea orderTArea;

    private StringBuilder sBuilderOrder;

    private void displayGUI()
    {
        JFrame frame = new JFrame("Pizza Shop");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        /*
         * This JPanel is the base of all the
         * other components, and at the end 
         * we will set this as Content Pane
         * for the JFrame.
         */
        JPanel contentPane = new JPanel();
        contentPane.setLayout(new BorderLayout(5, 5));
        contentPane.setBorder(
            BorderFactory.createEmptyBorder(10, 10, 10, 10));
        /*
         * TOP PART of the LAYOUT.
         */
        headerPanel = new JPanel();
        JLabel headerLabel = new JLabel(
                "Welcome to Home Style Pizza Shop"
                                    , JLabel.CENTER);
        headerLabel.setForeground(Color.RED);
        headerPanel.add(headerLabel);

        /*
         * CENTER PART of the LAYOUT.
         */
        centerPanel = new JPanel();
        centerPanel.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.anchor = GridBagConstraints.FIRST_LINE_START;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridheight = 2;
        gbc.weightx = 0.3;
        gbc.weighty = 1.0;

        /*
         * Above Constraints are for this part.
         */
        toppingPanel = new JPanel();
        toppingPanel.setBorder(
            BorderFactory.createTitledBorder(
                BorderFactory.createLineBorder(Color.RED),
                                            "Each Topping $1.50"));
        JPanel checkBoxesPanel = new JPanel();
        checkBoxesPanel.setBorder(
            BorderFactory.createEmptyBorder(5, 5, 5, 5));
        checkBoxesPanel.setLayout(new GridLayout(0, 1, 5, 5));

        toppingsCBox = new JCheckBox[toppings.length];
        for (int i = 0; i < toppings.length; i++)
        {
            toppingsCBox[i] = new JCheckBox(toppings[i]);
            checkBoxesPanel.add(toppingsCBox[i]);
        }

        toppingPanel.add(checkBoxesPanel);
        centerPanel.add(toppingPanel, gbc);

        // Till this.

        gbc.gridx = 1;
        gbc.gridheight = 1;
        gbc.weighty = 0.7;

        /*
         * Above Constraints are for this part.
         */
        sizePanel = new JPanel();
        sizePanel.setBorder(
            BorderFactory.createTitledBorder(
                BorderFactory.createLineBorder(Color.RED),
                                            "Pizza Size"));
        JPanel radioBoxesPanel = new JPanel();
        radioBoxesPanel.setBorder(
            BorderFactory.createEmptyBorder(5, 5, 5, 5));
        radioBoxesPanel.setLayout(new GridLayout(0, 1, 10, 10));

        sizePizzaRButton = new JRadioButton[sizePizza.length];
        bGroupSize = new ButtonGroup();
        for (int i = 0; i < sizePizza.length; i++)
        {
            sizePizzaRButton[i] = new JRadioButton(sizePizza[i]);
            bGroupSize.add(sizePizzaRButton[i]);
            radioBoxesPanel.add(sizePizzaRButton[i]);
        }

        sizePanel.add(radioBoxesPanel);
        centerPanel.add(sizePanel, gbc);
        // Till this.

        gbc.gridx = 2;
        gbc.weighty = 0.7;

        /*
         * Above Constraints are for this part.
         */
        typePanel = new JPanel();
        typePanel.setBorder(
            BorderFactory.createTitledBorder(
                BorderFactory.createLineBorder(Color.RED),
                                            "Pizza Type"));
        JPanel radioBoxesTypePanel = new JPanel();
        radioBoxesTypePanel.setBorder(
            BorderFactory.createEmptyBorder(5, 5, 5, 5));
        radioBoxesTypePanel.setLayout(new GridLayout(0, 1, 10, 10));

        typePizzaRButton = new JRadioButton[typePizza.length];
        bGroupType = new ButtonGroup();
        for (int i = 0; i < typePizza.length; i++)
        {
            typePizzaRButton[i] = new JRadioButton(typePizza[i]);
            bGroupType.add(typePizzaRButton[i]);
            radioBoxesTypePanel.add(typePizzaRButton[i]);
        }

        typePanel.add(radioBoxesTypePanel);
        centerPanel.add(typePanel, gbc);
        // Till this.

        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.weighty = 0.3;
        gbc.gridwidth = 2;

        processButton = new JButton("Process Selection");
        processButton.addActionListener(
            new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent ae)
            {
                sBuilderOrder = new StringBuilder();
                sBuilderOrder.append("Pizza type : ");
                for (int i = 0; i < typePizza.length; i++)
                {
                    if (typePizzaRButton[i].isSelected())
                        sBuilderOrder.append(typePizzaRButton[i].getText());
                }

                sBuilderOrder.append("\n");
                sBuilderOrder.append("Pizza Size : ");

                for (int i = 0; i < sizePizza.length; i++)
                {
                    if (sizePizzaRButton[i].isSelected())
                        sBuilderOrder.append(sizePizzaRButton[i].getText());
                }

                sBuilderOrder.append("\n");
                sBuilderOrder.append("Toppings : ");
                /*
                 * I hope you can do this part yourself now :-)
                 */

                orderTArea.setText(sBuilderOrder.toString());
            }
        });
        centerPanel.add(processButton, gbc);

        footerPanel = new JPanel();
        footerPanel.setLayout(new BorderLayout(5, 5));
        footerPanel.setBorder(
            BorderFactory.createTitledBorder("Your Order : "));
        orderTArea = new JTextArea(10, 10);
        footerPanel.add(orderTArea, BorderLayout.CENTER);

        contentPane.add(headerPanel, BorderLayout.PAGE_START);
        contentPane.add(centerPanel, BorderLayout.CENTER);
        contentPane.add(footerPanel, BorderLayout.PAGE_END);

        frame.setContentPane(contentPane);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                new PizzaLayout().displayGUI();
            }
        });
    }
}

同じの出力は次のとおりです。

ピザレイアウト

于 2012-11-16T15:22:52.483 に答える