【问题标题】:border&grid layouts边框和网格布局
【发布时间】:2012-03-21 04:05:41
【问题描述】:

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

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);

    }

}

【问题讨论】:

  • WEST 中的那些按钮可能更适合放在JToolBar 中的NORTH 中。 OTOH 我不完全清楚你希望这个 GUI 如何出现,或者在调整大小时它应该如何重新分配空间。你能添加一些 ASCII 艺术或图画来更好地解释吗?
  • ..另外,我猜RunRurniture 应该是RunFurniture。 ;)
  • 你说的很对类名
  • 我正在尝试做一个简单的设计
  • 我认为,LINE_STARTWEST 上的JPanel 必须与BoxLayoutPAGE_AXIS 一起使用,而CENTER JPanel 可以与GridLayout 一起使用容纳这些图片:-)

标签: java swing border-layout


【解决方案1】:

一些额外的建议:

  • 布局时不要使用空格;使用对齐方式。

  • 让布局使用组件的首选大小来完成工作。

  • 尽可能使用for-each 构造。

  • 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);
            }
        });

    }
}

【讨论】:

  • 很好的例子。和往常一样,漂亮的屏幕截图。 :)
【解决方案2】:

最新编辑:

我认为,LINE_STARTWEST 上的JPanel 必须与GridLayout 一起使用,而CENTER JPanel 可以与GridLayout 一起使用以容纳这些图像:-)。当你接受这个答案时,我正在处理它,当@AndrewThompson 添加关于GridLayout thingy 的评论时,但似乎将 JPanel 放在 GridLayout 上将允许相同大小的 JButtons。这是我在代码中对GridLayout 的解释:

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

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

    /**
     * @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.pack();
        application.setLocationByPlatform(true);
        application.setVisible(true);

    }

}

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 GridLayout(0, 1));
        //westPanel.setLayout(new BoxLayout(westPanel, BoxLayout.PAGE_AXIS));
        westPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

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

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

        // 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)
    {




    }
}

输出:

这是我对代码中BoxLayout 的解释:

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

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

    /**
     * @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.pack();
        application.setLocationByPlatform(true);
        application.setVisible(true);

    }

}

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 GridLayout(0, 1));
        //westPanel.setLayout(new BoxLayout(westPanel, BoxLayout.PAGE_AXIS));
        westPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

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

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

        // 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)
    {




    }
}

这是输出:

【讨论】:

  • 啊,是的,那是我正在玩的另一个布局。 +1 请参阅How do I create screenshots?(有关制作出色屏幕截图的提示)。
  • 感谢您抽出宝贵时间,您的解决方案适合我,干杯
  • @Kiril : 欢迎您并保持微笑 :-)
  • 我的第一条评论被误导了,我以为你是在建议一个单独的列 GridLayout 来保持所有按钮的宽度相同! GL 还可以轻松地在按钮之间添加额外的空间(尽管我猜想在 BoxLayout 中使用 JSeparator 实例也可以做到这一点)。
  • +1 个例子;关于alignment 的小注释,地址为here
【解决方案3】:

这是我在评论中提到的一个例子(还有一些其他的调整)。

package test;

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

public class PanelFurniture extends JPanel
{

    private static final long serialVersionUID = 4231608548183463223L;

    JButton center, east;
    // leading/trailing spaces in these labels will be ignored.
    JButton[] commandButtons = {
            new JButton("Add Chair"),
            new JButton("Add Table"),
            new JButton("Add Desk "),
            new JButton("Clear All   "),
            new JButton("Total Price"),
            new JButton("Summary "),
            new JButton("Save"),
            new JButton("Load")
    };

    JPanel centerPanel, eastPanel;

    PanelFurniture()
    {
        this.setLayout(new BorderLayout());

        JToolBar toolBar = new JToolBar(); 

        for(int i=0; i<commandButtons.length; i++)      
        {
            if (i==3 || i==6) {
                toolBar.addSeparator();
            }
            toolBar.add(commandButtons[i]);
        }
        this.add(toolBar, BorderLayout.NORTH);

        // 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 static void main(String[] args) 
    {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame application = new JFrame();
                PanelFurniture panel = new PanelFurniture();
                application.add(panel);
                application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                application.pack();
                application.setLocationByPlatform(true);
                application.setVisible(true);
            }
        });
    }
}

更新

此 GUI 基于图像。

package test;

import java.awt.BorderLayout;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;

public class PanelFurniture extends JPanel
{

    private static final long serialVersionUID = 4231608548183463223L;

    JButton center, east;
    // leading/trailing spaces in these labels will be ignored.
    JButton[] commandButtons = {
            new JButton("Add Chair"),
            new JButton("Add Table"),
            new JButton("Add Desk "),
            new JButton("Clear All   "),
            new JButton("Total Price"),
            new JButton("Summary "),
            new JButton("Save"),
            new JButton("Load")
    };

    JPanel centerPanel, westPanel, westPanelConstrain, eastPanel;

    PanelFurniture()
    {
        super(new BorderLayout(2,2));
        this.setBorder(new EmptyBorder(4,4,4,4));

        westPanel = new JPanel(new GridLayout(0,1,4,4));

        for(int i=0; i<commandButtons.length; i++)      
        {
            westPanel.add(commandButtons[i]);
        }
        westPanelConstrain = new JPanel(new BorderLayout());
        westPanelConstrain.add(westPanel, BorderLayout.NORTH);
        this.add(westPanelConstrain, 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 static void main(String[] args) 
    {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame application = new JFrame();
                PanelFurniture panel = new PanelFurniture();
                application.add(panel);
                application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                application.pack();
                application.setLocationByPlatform(true);
                application.setVisible(true);
            }
        });
    }
}

【讨论】:

  • 这很好,但我需要按钮从顶部到按钮
  • 现在看到那张图片,我会选择 Gagandeep 或我的主按钮设计,以及 CENTERFlowLayoutJList 调用 @987654321 @.
  • 另请参阅解释我对使用 GridLayout 的意思的更新
  • @trashgod 谢谢。我在等某人这么说。 ;) 我让步并为 OP 的规范提供了第二个示例。 “客户永远是对的。”
猜你喜欢
  • 1970-01-01
  • 2016-04-16
  • 2023-03-19
  • 2021-11-23
  • 2014-06-07
  • 2013-09-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多