tarun
tarun

Reputation: 1

Unrequired border around first card

I am using GroupLayout for the main panel. All cards are in their place except the problem in the first one. It's leaving a border around it which reduces when the output window is minimized. What am I missing here?

Output:

https://i.sstatic.net/upgRy.png

Output when window is minimized:

https://i.sstatic.net/RVor7.png

public class UI2 extends JFrame {

UI2() {
    setTitle("Dashboard");
    Container c = getContentPane();
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel jpmain = new JPanel(){
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g;
            g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
            int w = getWidth();
            int h = getHeight();
            Color color1 = new Color(90,63,55);
            Color color2 = new Color(44,119,68);
            GradientPaint gp = new GradientPaint(0, 0, color1, 0, h, color2);
            g2d.setPaint(gp);
            g2d.fillRect(0, 0, w, h);
        }
    };
    GroupLayout layout = new GroupLayout(jpmain);
    jpmain.setLayout(layout);
    try{
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    }catch (Exception e){
        System.out.println(e);
    }

    ImageIcon image = new ImageIcon(new File("path1").getPath());
    JButton btn1 = new JButton(new ImageIcon(image.getImage().getScaledInstance(150,150, java.awt.Image.SCALE_SMOOTH)));
    btn1.setText("First Menu");
    btn1.setHorizontalTextPosition(SwingConstants.CENTER);
    btn1.setVerticalTextPosition(SwingConstants.BOTTOM);
    btn1.setFont(new Font("Arial",Font.PLAIN,15));

    image = new ImageIcon(new File("path2").getPath());
    JButton btn2 = new JButton(new ImageIcon(image.getImage().getScaledInstance(150,150, java.awt.Image.SCALE_SMOOTH)));
    btn2.setText("Second Menu");
    btn2.setHorizontalTextPosition(SwingConstants.CENTER);
    btn2.setVerticalTextPosition(SwingConstants.BOTTOM);
    btn2.setFont(new Font("Arial",Font.PLAIN,15));

    image = new ImageIcon(new File("path3").getPath());
    JButton btn3 = new JButton(new ImageIcon(image.getImage().getScaledInstance(150,150, java.awt.Image.SCALE_SMOOTH)));
    btn3.setText("Third Menu");
    btn3.setHorizontalTextPosition(SwingConstants.CENTER);
    btn3.setVerticalTextPosition(SwingConstants.BOTTOM);
    btn3.setFont(new Font("Arial",Font.PLAIN,15));

    image = new ImageIcon(new File("path4").getPath());
    JButton btn4 = new JButton(new ImageIcon(image.getImage().getScaledInstance(150,150, java.awt.Image.SCALE_SMOOTH)));
    btn4.setText("Fourth Menu");
    btn4.setHorizontalTextPosition(SwingConstants.CENTER);
    btn4.setVerticalTextPosition(SwingConstants.BOTTOM);
    btn4.setFont(new Font("Arial",Font.PLAIN,15));

    image = new ImageIcon(new File("path5").getPath());
    JButton btn5 = new JButton(new ImageIcon(image.getImage().getScaledInstance(150,150, java.awt.Image.SCALE_SMOOTH)));
    btn5.setText("Fifth Menu");
    btn5.setHorizontalTextPosition(SwingConstants.CENTER);
    btn5.setVerticalTextPosition(SwingConstants.BOTTOM);
    btn5.setFont(new Font("Arial",Font.PLAIN,15));

    JPanel jp = new JPanel();

    layout.setHorizontalGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addComponent(btn1)
                    .addComponent(jp))
            .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addComponent(jp)
                    .addComponent(btn4))
            .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addComponent(btn2)
                    .addComponent(jp))
            .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addComponent(jp)
                    .addComponent(btn5))
            .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addComponent(btn3)
                    .addComponent(jp)));

    layout.setVerticalGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER)
                    .addComponent(btn1)
                    .addComponent(jp)
                    .addComponent(btn2)
                    .addComponent(jp)
                    .addComponent(btn3))
            .addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER)
                    .addComponent(jp)
                    .addComponent(btn4)
                    .addComponent(jp)
                    .addComponent(btn5)
                    .addComponent(jp)));

    layout.setAutoCreateGaps(true);
    layout.setAutoCreateContainerGaps(true);

    setExtendedState(JFrame.MAXIMIZED_BOTH);
    c.add(jpmain);
    pack();
}

public static void main(String[] args) {
    UI2 frame = new UI2();
    frame.setVisible(true);
 }
}

Upvotes: 0

Views: 37

Answers (1)

Andrew Thompson
Andrew Thompson

Reputation: 168845

enter image description here

This type of layout is easily done using a GridLayout. We just need to add alternate icons and blank labels. Here is who it looks when stretched wider.

enter image description here

Upvotes: 1

Related Questions