边框和网格布局 [英] border&grid layouts

查看:56
本文介绍了边框和网格布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我有一个问题.如果有人可以提供帮助,那就太好了.我正在使用边框和网格布局,我正在尝试拆分 GUI,但它没有发生,因为我希望按钮成为整体的一小部分,可以说 1/5,但目前超过 GUI 的一半.我也尝试将按钮放在尺寸上,但我不确定这是否是一个好的做法.我有两个类,一个是 RunFurniture,其中是框架的主要方法,另一种方法是带有 GUI 的 PanelFurniture.我正在使用 eclipse 和程序正在编译和运行.我希望我给出了一个很好的解释.这是代码.

Hi everyone I have a problem. If anyone can help it would be great. I am using border and gridlayout and I am trying to split the GUI but it is not happening as I want the buttons to be a small part of the whole lets say 1/5 but at the moment is more than the half of the GUI. I also trying putting the buttons is dimension but I am not sure if it is a good practice.I have two classes one is RunFurniture where is the main method with the frame and the other method is PanelFurniture with the GUI.I am using eclipse and the program is compiling and running. I hope I gave a good explanation. Here is the code.

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

public class PanelFurniture extends JPanel implements ActionListener
{

    JButton center, east;
    JButton[] commandButtons = {
                                 new JButton(" Add Chair"),
                                 new JButton(" Add Table"),
                                 new JButton(" Add Desk "),
                                 new JButton(" Clear All   "),
                                 new JButton("Total Price"),
                                 new JButton("   Save       "),
                                 new JButton("   Load       "),
                                 new JButton("Summary ")
                                                        };

    JPanel centerPanel, westPanel, eastPanel;

    PanelFurniture()
    {

        this.setLayout(new BorderLayout());


        westPanel = new JPanel();
        westPanel.setLayout(new FlowLayout());

        for(int i=0; i<commandButtons.length; i++)      
        {
            westPanel.add(commandButtons[i]);
            commandButtons[i].addActionListener(this);

        }
//      westPanel.setSize(westDimension);   
        this.add(westPanel, BorderLayout.WEST);

        // start the middle panel       
        centerPanel = new JPanel(new GridLayout(1,2));

        center = new JButton("center");
        centerPanel.add(center);
        east = new JButton("east");
        centerPanel.add(east);  


        this.add(centerPanel, BorderLayout.CENTER);


    }

    public void actionPerformed(ActionEvent ae)
    {




    }
}

RunRurniture

import java.awt.*;
import javax.swing.*;
public class RunRurniture 
{

    /**
     * @param args
     */
    public static void main(String[] args) 
    {


        JFrame application = new JFrame();
        PanelFurniture panel = new PanelFurniture();
        application.add(panel);
        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        application.setSize(300,150);
        application.setLocationByPlatform(true);
        application.setVisible(true);

    }

}

推荐答案

一些额外的建议:

  • 不要使用空格进行布局;使用对齐方式.

  • Don't use spaces to do layout; use alignment.

让布局通过使用组件的首选大小来完成工作.

Let the layout do the work by using the components preferred size.

使用 -each 尽可能构造.

Use the for-each construct where possible.

EDT 开始.

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

/** @see http://stackoverflow.com/q/9793194/230513 */
public class FurnitureTest {

    private static final class FurniturePanel
        extends JPanel implements ActionListener {

        private static final int N = 3;
        private static final Icon icon =
            UIManager.getIcon("OptionPane.informationIcon");
        private JPanel westPanel = new JPanel();
        private JPanel centerPanel = new JPanel();
        private JButton[] commandButtons = {
            new JButton("Add Chair"),
            new JButton("Add Table"),
            new JButton("Add Desk"),
            new JButton("Clear All"),
            new JButton("Total Price"),
            new JButton("Save"),
            new JButton("Load"),
            new JButton("Summary")
        };

        FurniturePanel() {
            this.setLayout(new GridLayout());
            westPanel.setLayout(new BoxLayout(westPanel, BoxLayout.Y_AXIS));

            for (JButton b : commandButtons) {
                b.setAlignmentX(JButton.CENTER_ALIGNMENT);
                westPanel.add(b);
                b.addActionListener(this);
            }
            this.add(westPanel, BorderLayout.WEST);

            centerPanel.setLayout(new GridLayout(N, N, N, N));
            for (int i = 0; i < N * N; i++) {
                centerPanel.add(new JLabel(icon));
            }
            this.add(centerPanel, BorderLayout.CENTER);
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println(e);
        }
    }

    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame application = new JFrame();
                FurniturePanel panel = new FurniturePanel();
                application.add(panel);
                application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                application.pack();
                application.setLocationByPlatform(true);
                application.setVisible(true);
            }
        });

    }
}

这篇关于边框和网格布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆