JFrame does not display correctly on small screens

enter image description hereI am trying to create an application that could work on different screen sizes. I am trying to use it in NetBeans IDE. But what happens is that my code is not working as intended.

I found several solutions around it. And one of them had to use the layout manager, so I did the same. Because of this, still nothing has changed. However, the problem remains the same. Below is a sample code.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package testproject;

public class HomePage extends javax.swing.JFrame {

    /**
     * Creates new form HomePage
     */
    public HomePage() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jTextField3 = new javax.swing.JTextField();
        jButton7 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setMaximumSize(new java.awt.Dimension(1366, 720));
        setPreferredSize(new java.awt.Dimension(1366, 720));

        jButton1.setText("jButton1");

        jButton2.setText("jButton2");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(1167, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addGap(5, 5, 5)
                .addComponent(jButton2)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(36, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap())
        );

        jTextField1.setText("jTextField1");

        jTextField2.setText("jTextField1");

        jTextField3.setText("jTextField1");

        jButton7.setText("jButton3");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTextField1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 173, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 173, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 173, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(41, 41, 41)
                .addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 281, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(43, 43, 43)
                .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(47, 47, 47)
                .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(47, 47, 47)
                .addComponent(jButton7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(242, 242, 242))
        );

        jButton3.setText("jButton3");

        jButton4.setText("jButton3");

        jButton5.setText("jButton3");

        jButton6.setText("jButton3");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(41, 41, 41)
                        .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 288, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(55, 55, 55)
                        .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 281, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(45, 45, 45)
                        .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 283, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(28, 28, 28)
                        .addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 301, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 39, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jButton5, javax.swing.GroupLayout.DEFAULT_SIZE, 36, Short.MAX_VALUE)
                    .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(21, 21, 21))
        );

        pack();
    }// </editor-fold>

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(HomePage.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(HomePage.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(HomePage.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(HomePage.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new HomePage().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    // End of variables declaration
}

      

Please tell me the errors in the above code, if any. And suggest any solution around it. Thank you in advance.

+3


source to share


1 answer


enter image description hereAs I said, you can achieve almost any kind of layout using just BorderLayout

and GridLayout

. Manage these two layout managers and you don't need the others. Here is my final version. Please refer to the tutorials for a more complete understanding of layout managers: https://docs.oracle.com/javase/tutorial/uiswing/layout/using.html

            import java.awt.BorderLayout;
            import java.awt.Color;
            import java.awt.Component;
            import java.awt.Dimension;
            import java.awt.GridLayout;

            import javax.swing.BorderFactory;
            import javax.swing.JButton;
            import javax.swing.JFrame;
            import javax.swing.JLabel;
            import javax.swing.JPanel;
            import javax.swing.JScrollPane;
            import javax.swing.JTextField;
            import javax.swing.JTree;
            import javax.swing.SwingConstants;
            import javax.swing.SwingUtilities;
            import javax.swing.WindowConstants;

            public class FrameDemo extends JFrame {
                private static final long serialVersionUID = 1L;

                public FrameDemo() {
                    super("Frame Demo");
                    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

                    getContentPane().add(new NorthPanel(), BorderLayout.NORTH);
                    getContentPane().add(new CenterPanel(), BorderLayout.CENTER);
                    getContentPane().add(new SouthPanel(), BorderLayout.SOUTH);

                //          Toolkit tk = Toolkit.getDefaultToolkit();
                //          int xSize = ((int) tk.getScreenSize().getWidth());
                //          int ySize = ((int) tk.getScreenSize().getHeight());
                //          setSize(xSize,ySize);

                    pack();
                    setLocationRelativeTo(null);
                }

                public static void main(String[] args) {
                    SwingUtilities.invokeLater(new Runnable() {
                        public void run() {
                            JFrame frame = new FrameDemo();
                            frame.setVisible(true);
                        }
                    });
                } 
            }

            class NorthPanel extends JPanel {
                private static final long serialVersionUID = 1L;

                public NorthPanel() {
                    super(new BorderLayout());
                    setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

                    add(getHeaderPanel(), BorderLayout.NORTH);
                    add(getButtonPanel(), BorderLayout.SOUTH);
                }

                private Component getHeaderPanel() {
                    JPanel panel = new JPanel(new GridLayout(2, 1));
                    panel.setBackground(Color.WHITE);

                    panel.add(new JLabel("XXXXXX Enterprises", SwingConstants.CENTER));
                    panel.add(new JLabel("XXX, Industria; Area - II, XXXXXXXXX", SwingConstants.CENTER));

                    return panel;
                }

                private Component getButtonPanel() {
                    JPanel panel = new JPanel(new BorderLayout());
                    panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 0, 0));

                    JPanel buttons = new JPanel(new GridLayout(1, 2));
                    buttons.add(new JButton("Admin"));
                    buttons.add(new JButton("Exit"));

                    panel.add(buttons, BorderLayout.EAST);
                    return panel;
                }
            }

            class CenterPanel extends JPanel {
                private static final long serialVersionUID = 1L;

                private static final Dimension PANEL_SIZE = new Dimension(640, 480);

                public CenterPanel() {
                    super(new BorderLayout());
                    setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

                    add(getSearchFieldsPanel(), BorderLayout.NORTH);
                    add(getResultsPanel(), BorderLayout.CENTER);
                }

                private Component getSearchFieldsPanel() {
                    JPanel panel = new JPanel(new BorderLayout());
                    panel.setBackground(Color.WHITE);

                    panel.add(new JLabel("Sales Inventory", SwingConstants.CENTER), BorderLayout.NORTH);
                    panel.add(getFieldsPanel(), BorderLayout.CENTER);
                    panel.add(getButtonPanel(), BorderLayout.SOUTH);

                    return panel;
                }

                private Component getResultsPanel() {
                    JPanel panel = new JPanel(new BorderLayout());

                    panel.add(getHeaderPanel(), BorderLayout.NORTH);
                    panel.add(new JScrollPane(new JTree()), BorderLayout.CENTER);
                    panel.add(getAnotherButtonPanel(), BorderLayout.SOUTH);

                    return panel;
                }

                private Component getHeaderPanel() {
                    JPanel panel = new JPanel(new BorderLayout());
                    panel.setBorder(BorderFactory.createEmptyBorder(10, 0, 10, 0));

                    JLabel label = new JLabel("Customers", SwingConstants.LEFT);
                    label.setOpaque(true);
                    label.setBackground(Color.WHITE);


                    panel.add(label, BorderLayout.NORTH);
                    return panel;
                }

                private Component getFieldsPanel() {
                    JPanel panel = new JPanel(new GridLayout(1, 4));

                    panel.add(new LabeledField("Name :  "));
                    panel.add(new LabeledField("Firm Name :  "));
                    panel.add(new LabeledField("City :  "));
                    panel.add(new LabeledField("Mobile :  "));

                    return panel;
                }

                private Component getButtonPanel() {
                    JPanel panel = new JPanel(new BorderLayout());
                    panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 0, 0));
                    panel.setBackground(Color.WHITE);

                    JPanel buttons = new JPanel(new GridLayout(1, 2));
                    buttons.add(new JButton("Reset"));
                    buttons.add(new JButton("Search"));

                    panel.add(buttons, BorderLayout.EAST);
                    return panel;
                }

                private Component getAnotherButtonPanel() {
                    JPanel panel = new JPanel(new BorderLayout());
                    panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 0, 0));
                    panel.setBackground(Color.WHITE);

                    JPanel buttons = new JPanel(new GridLayout(1, 2));
                    buttons.add(new JButton("Next"));

                    panel.add(buttons, BorderLayout.EAST);
                    return panel;
                }

                public Dimension getPreferredSize() {
                    return PANEL_SIZE;
                }

                public Dimension getMinimumSize() {
                    return PANEL_SIZE;
                }

                public Dimension getMaximumSize() {
                    return PANEL_SIZE;
                }
            }

            class SouthPanel extends JPanel {
                private static final long serialVersionUID = 1L;

                public SouthPanel() {
                    super(new GridLayout());

                    setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

                    add(new JButton("Sales Inventory"));
                    add(new JButton("Customer"));
                    add(new JButton("Stock Inventory"));
                    add(new JButton("Reports"));
                }
            }

            class LabeledField extends JPanel {
                private static final long serialVersionUID = 1L;

                public LabeledField(String label) {
                    super(new BorderLayout());

                    setBackground(Color.WHITE);
                    add(getLabel(label), BorderLayout.WEST);
                    add(getEntry(), BorderLayout.CENTER);
                }

                private Component getLabel(String caption) {
                    JLabel label = new JLabel(caption, SwingConstants.RIGHT);
                    label.setPreferredSize(new Dimension(85, 25));
                    return label;
                }

                private Component getEntry() {
                    return new JTextField();
                }
            }

      



This is output enter image description here

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import javax.swing.border.Border;
import javax.swing.border.CompoundBorder;

public class FrameDemo extends JFrame {
    private static final long serialVersionUID = 1L;

    public FrameDemo() {
        super("Frame Demo");
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        getContentPane().add(new NorthPanel(), BorderLayout.NORTH);
        getContentPane().add(new CenterPanel(), BorderLayout.CENTER);
        getContentPane().add(new SouthPanel(), BorderLayout.SOUTH);

        //          Toolkit tk = Toolkit.getDefaultToolkit();
        //          int xSize = ((int) tk.getScreenSize().getWidth());
        //          int ySize = ((int) tk.getScreenSize().getHeight());
        //          setSize(xSize,ySize);

        pack();
        setLocationRelativeTo(null);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new FrameDemo();
                frame.setVisible(true);
            }
        });
    } 
}

class NorthPanel extends JPanel {
    private static final long serialVersionUID = 1L;

    public NorthPanel() {
        super(new BorderLayout());
        setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        add(getHeaderPanel(), BorderLayout.NORTH);
        add(getButtonPanel(), BorderLayout.SOUTH);
    }

    private Component getHeaderPanel() {
        JPanel panel = new JPanel(new GridLayout(2, 1));
        panel.setBackground(Color.WHITE);

        panel.add(new JLabel("XXXXXX Enterprises", SwingConstants.CENTER));
        panel.add(new JLabel("XXX, Industria; Area - II, XXXXXXXXX", SwingConstants.CENTER));

        return panel;
    }

    private Component getButtonPanel() {
        JPanel panel = new JPanel(new BorderLayout());
        panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 0, 0));

        JPanel buttons = new JPanel(new GridLayout(1, 2));
        buttons.add(new JButton("Admin"));
        buttons.add(new JButton("Exit"));

        panel.add(buttons, BorderLayout.EAST);
        return panel;
    }
}

class CenterPanel extends JPanel {
    private static final long serialVersionUID = 1L;

    private static final Dimension PANEL_SIZE = new Dimension(640, 480);

    public CenterPanel() {
        super(new BorderLayout());
        setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        add(getSearchFieldsPanel(), BorderLayout.NORTH);
        add(new NewPanel(), BorderLayout.WEST);
        add(getResultsPanel(), BorderLayout.CENTER);
    }

    private Component getSearchFieldsPanel() {
        JPanel panel = new JPanel(new BorderLayout());
        panel.setBackground(Color.WHITE);

        panel.add(new JLabel("Sales Inventory", SwingConstants.CENTER), BorderLayout.NORTH);
        panel.add(getFieldsPanel(), BorderLayout.CENTER);
        panel.add(getButtonPanel(), BorderLayout.SOUTH);

        return panel;
    }

    private Component getResultsPanel() {
        JPanel panel = new JPanel(new BorderLayout());

        panel.add(getHeaderPanel(), BorderLayout.NORTH);
        panel.add(new JScrollPane(new JTree()), BorderLayout.CENTER);
        panel.add(getAnotherButtonPanel(), BorderLayout.SOUTH);

        return panel;
    }

    private Component getHeaderPanel() {
        JPanel panel = new JPanel(new BorderLayout());
        panel.setBorder(BorderFactory.createEmptyBorder(10, 0, 10, 0));

        JLabel label = new JLabel("Customers", SwingConstants.LEFT);
        label.setOpaque(true);
        label.setBackground(Color.WHITE);


        panel.add(label, BorderLayout.NORTH);
        return panel;
    }

    private Component getFieldsPanel() {
        JPanel panel = new JPanel(new GridLayout(1, 4));

        panel.add(new LabeledField("Name :  "));
        panel.add(new LabeledField("Firm Name :  "));
        panel.add(new LabeledField("City :  "));
        panel.add(new LabeledField("Mobile :  "));

        return panel;
    }

    private Component getButtonPanel() {
        JPanel panel = new JPanel(new BorderLayout());
        panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 0, 0));
        panel.setBackground(Color.WHITE);

        JPanel buttons = new JPanel(new GridLayout(1, 2));
        buttons.add(new JButton("Reset"));
        buttons.add(new JButton("Search"));

        panel.add(buttons, BorderLayout.EAST);
        return panel;
    }

    private Component getAnotherButtonPanel() {
        JPanel panel = new JPanel(new BorderLayout());
        panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 0, 0));
        panel.setBackground(Color.WHITE);

        JPanel buttons = new JPanel(new GridLayout(1, 2));
        buttons.add(new JButton("Next"));

        panel.add(buttons, BorderLayout.EAST);
        return panel;
    }

    public Dimension getPreferredSize() {
        return PANEL_SIZE;
    }

    public Dimension getMinimumSize() {
        return PANEL_SIZE;
    }

    public Dimension getMaximumSize() {
        return PANEL_SIZE;
    }
}

class SouthPanel extends JPanel {
    private static final long serialVersionUID = 1L;

    public SouthPanel() {
        super(new GridLayout());

        setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        add(new JButton("Sales Inventory"));
        add(new JButton("Customer"));
        add(new JButton("Stock Inventory"));
        add(new JButton("Reports"));
    }
}

class LabeledField extends JPanel {
    private static final long serialVersionUID = 1L;

    public LabeledField(String label) {
        super(new BorderLayout());

        setBackground(Color.WHITE);
        add(getLabel(label), BorderLayout.WEST);
        add(getEntry(), BorderLayout.CENTER);
    }

    private Component getLabel(String caption) {
        JLabel label = new JLabel(caption, SwingConstants.RIGHT);
        label.setPreferredSize(new Dimension(85, 25));
        return label;
    }

    private Component getEntry() {
        return new JTextField();
    }
}

class NewPanel extends JPanel {
    private static final long serialVersionUID = 1L;

    public NewPanel() {
        super(new BorderLayout());
        setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 10));       
        add(getTopPanel(), BorderLayout.NORTH);
        add(new NewBottomPanel(), BorderLayout.CENTER);     
    }

    public Dimension getPreferredSize() {
        return new Dimension(350, 300);
    }

    private Component getTopPanel() {
        JPanel container = new JPanel(new BorderLayout());

        JPanel panel = new JPanel(new GridLayout(3,2));
        panel.add(new LabeledField("Design ID :  "));
        panel.add(new LabeledField("Thickness :  "));
        panel.add(new LabeledField("   Finish :  "));
        panel.add(new LabeledField(" Quantity :  "));
        panel.add(new LabeledField("     Rate :  "));
        panel.add(new LabeledField("     Size :  "));

        container.add(panel, BorderLayout.NORTH);
        container.add(getTopButtonPanel(), BorderLayout.SOUTH);

        return container;
    }

    private Component getTopButtonPanel() {
        JPanel container = new JPanel(new BorderLayout());
        container.setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 0));

        container.setBackground(Color.WHITE);
        container.add(new JButton("Add Item"), BorderLayout.EAST);
        return container;
    }
}

class NewBottomPanel extends JPanel {
    private static final long serialVersionUID = 1L;

    public NewBottomPanel() {
        super(new BorderLayout());

        Border emptyBorder = BorderFactory.createEmptyBorder(10, 0, 0, 0);
        Border redLineBorder = BorderFactory.createLineBorder(Color.RED, 1);
        Border compounded = new CompoundBorder(emptyBorder, redLineBorder);
        setBorder(compounded);      
        setBackground(Color.WHITE);

        add(getTopPanel(), BorderLayout.NORTH);

    }

    private Component getTopPanel() {
        JPanel container = new JPanel(new BorderLayout());
        container.setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 0));
        container.setBackground(Color.WHITE);

        JPanel panel = new JPanel(new GridLayout(3,2));
        panel.add(new LabeledField("Design ID :  "));
        panel.add(new LabeledField("Thickness :  "));
        panel.add(new LabeledField("   Finish :  "));
        panel.add(new LabeledField(" Quantity :  "));
        panel.add(new LabeledField("     Rate :  "));
        panel.add(new LabeledField("     Size :  "));

        container.add(panel, BorderLayout.NORTH);       
        return container;
    }   
}

      

+3


source







All Articles