border&grid layouts

前端 未结 3 1615
北海茫月
北海茫月 2021-01-22 07:04

\"SampleHi everyone I have a problem. If anyone can help it would be great. I am using border and gridlayout and I am

3条回答
  •  一整个雨季
    2021-01-22 07:36

    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

    Output :

    FURNITUREGRIDLAYOUT

    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

    Here is the output :

    FURNITURE

提交回复
热议问题