Ivan
Ivan

Reputation: 39

How do I create bordered color style panel?

I'm trying to create a canvas for my oval shape, and I want it to be different from the main JFrame color. So far, using setSize upon the panel doesn't work, it ended up creating a small box that I couldn't draw in. Here is the panel design that I intended it to be, with the white-colored part as the main frame.

PanelDesign

As I've said, using all three Layout modes (Border, Flow, and Grid) only creates a yellow small box in the upper middle part of the frame. This is the code that I use.

How can I create the panel design similar to the image posted above?

    setTitle("Oval Shape Mover");
    setSize(500, 200);
    setLayout(new BorderLayout());
    JPanel mainpanel, panel1, panel2;

    mainpanel = new JPanel();
    panel1 = new JPanel();
    panel2 = new JPanel();

    panel1.setBackground(Color.YELLOW);
    mainpanel.add(panel1, BorderLayout.CENTER);
    mainpanel.add(panel2);
    add(mainpanel);
    setVisible(true);

Upvotes: 0

Views: 43

Answers (1)

Andrew Thompson
Andrew Thompson

Reputation: 168825

The layouts used to make Java Swing GUIs will more often honor the preferred size than the size. Having said that, a custom rendered component should override (rather than set) getPreferredSize().

This example suggests a preferred size by using a JLabel to display the icon, and empty borders to pad the GUI out.

enter image description here

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.net.*;

public class RedDotLayout {

    private JComponent ui = null;
    String urlToRedCircle = "https://i.sstatic.net/wCF8S.png";

    RedDotLayout() {
        try {
            initUI();
        } catch (MalformedURLException ex) {
            ex.printStackTrace();
        }
    }

    public final void initUI() throws MalformedURLException {
        ui = new JPanel(new BorderLayout());
        ui.setBackground(Color.YELLOW);
        ui.setBorder(new LineBorder(Color.BLACK, 2));

        JLabel label = new JLabel(new ImageIcon(new URL(urlToRedCircle)));
        label.setBorder(new CompoundBorder(
                new LineBorder(Color.GREEN.darker(), 2),
                new EmptyBorder(20, 200, 20, 200)));
        ui.add(label, BorderLayout.CENTER);

        JPanel bottomPanel = new JPanel();
        bottomPanel.setBackground(Color.WHITE);
        bottomPanel.setBorder(new EmptyBorder(30, 50, 30, 50));
        ui.add(bottomPanel, BorderLayout.PAGE_END);
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = () -> {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (Exception useDefault) {
            }
            RedDotLayout o = new RedDotLayout();

            JFrame f = new JFrame(o.getClass().getSimpleName());
            f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            f.setLocationByPlatform(true);

            f.setContentPane(o.getUI());
            f.pack();
            f.setMinimumSize(f.getSize());

            f.setVisible(true);
        };
        SwingUtilities.invokeLater(r);
    }
}

Upvotes: 1

Related Questions