小编典典

边框和网格布局

java

范例GUI大家好,我有
问题。如果有人可以帮助,那就太好了。我正在使用border和
gridlayout,并且试图拆分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);

    }

}

阅读 249

收藏
2020-11-26

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

    }
}
2020-11-26