border&grid layouts

大憨熊 提交于 2019-12-02 04:57:59

问题


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

    }

}

回答1:


A few additional suggestions:

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

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

  • Use the for-each construct where possible.

  • Start in the 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:


Latest Edit :

I think, the JPanel on the LINE_START or WEST must have to be with GridLayout, and the CENTER JPanel can go with GridLayout to accomodate these images :-) . I was working on it when you accepted this answer and when @AndrewThompson, added that comment regarding GridLayout thingy, but seems like putting that JPanel on GridLayout will allow equal size JButtons. Here is my interpretation with GridLayout in code :

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




    }
}

Output :

Here is my interpretation with BoxLayout in code :

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




    }
}

Here is the output :




回答3:


This is an example of what I mentioned in the comment (with a few other tweaks).

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

Update

This GUI is based on the image.

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


来源:https://stackoverflow.com/questions/9793194/bordergrid-layouts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!