Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何创建带边框的颜色样式面板?_Java_Swing_Jpanel_Layout Manager - Fatal编程技术网

Java 如何创建带边框的颜色样式面板?

Java 如何创建带边框的颜色样式面板?,java,swing,jpanel,layout-manager,Java,Swing,Jpanel,Layout Manager,我正在尝试为我的椭圆形创建画布,我希望它与主JFrame颜色不同。到目前为止,在面板上使用setSize不起作用,它最终创建了一个我无法绘制的小框。这是我想要的面板设计,白色部分作为主框架 面板设计 正如我所说,使用所有三种布局模式(边框、流、和网格)只会在框架的中上部创建一个黄色小框。这是我使用的代码 如何创建与上面发布的图像相似的面板设计 setTitle("Oval Shape Mover"); setSize(500, 200); setLayout(new B

我正在尝试为我的椭圆形创建画布,我希望它与主
JFrame
颜色不同。到目前为止,在面板上使用
setSize
不起作用,它最终创建了一个我无法绘制的小框。这是我想要的面板设计,白色部分作为主框架

面板设计

正如我所说,使用所有三种布局模式(
边框
、和
网格
)只会在框架的中上部创建一个黄色小框。这是我使用的代码

如何创建与上面发布的图像相似的面板设计

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

用于制作JavaSwingGUI的布局通常会遵循首选的大小而不是大小。话虽如此,自定义渲染组件应该覆盖(而不是设置)
getPreferredSize()

此示例建议使用
JLabel
显示图标,并使用空边框填充GUI,以显示首选大小

请看:如果这有助于解决问题,请。
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.net.*;

public class RedDotLayout {

    private JComponent ui = null;
    String urlToRedCircle = "https://i.stack.imgur.com/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);
    }
}