1

一直在尝试创建一个基本上具有背景的程序(使用 Paint() 绘制),然后在其上方放置一个带有图像的标签。

继续将 JLabel 放在 Paint 下方...

有任何想法吗?

提前非常感谢。


public class GUI extends JFrame {

    JPanel menuBar = new JPanel();
    JButton button1 = new JButton("Press Me");
    JLayeredPane layeredPane = new JLayeredPane();
    private ImageIcon image1;
    private static JLabel label1;

    public GUI() {
        super("Add a profile");

        setLayout(null);

        try {
            image1 = new ImageIcon(getClass().getResource(
                    "Images/location.PNG"));
        } catch (Exception e) {
            System.out.println("Image not found!");
        }
        label1 = new JLabel(image1);
        label1.setBounds(new Rectangle(new Point(262, 94), label1.getPreferredSize()));
        label1.setLocation(1, 1);
        label1.setSize(114, 105);
        add(label1);
    }

    public void paint(Graphics g) {
        paintComponents(g);
        Graphics2D g2d = (Graphics2D) g;

        // Menu Bar
        g2d.setColor(Color.BLACK);
        g2d.drawRect(60, 93, 190, 373);
        g2d.setColor(Color.GRAY);
        g2d.fillRect(61, 94, 189, 372);

        // Background box
        g2d.setColor(Color.BLACK);
        g2d.drawRect(281, 106, 560, 360);
        g2d.setColor(Color.GRAY);
        g2d.fillRect(282, 107, 559, 359);
    }

    public static void main(String[] args) {
        GUI gui = new GUI();
        gui.setVisible(true);
        gui.setSize(900, 550);
        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gui.setResizable(false);
        gui.setLocationRelativeTo(null);

    }

}

可悲的是无法让它工作,无论如何谢谢一堆哦,我没有看到你所做的图像!对不起!我现在看看

真的很感激这一切!

4

1 回答 1

2

从您的代码的外观来看,您正在尝试使用绘画来完成其他容器的工作。我建议,不要。

不要覆盖顶级paint容器(例如JFrame

相反,为您自己创建一个自定义组件(从类似的东西扩展JPanel)并使用它的paintComponent方法,然后将其添加到框架中。

在你的情况下,你可以改变组件的背景和边框,它会得到相同的结果。

更新

在此处输入图像描述

只需一些基本布局和几个组件,我就能够制作出这个......

public class BadPaint {

    public static void main(String[] args) {
        new BadPaint();
    }

    public BadPaint() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new MenuPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class MenuPane extends JPanel {

        public MenuPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.weighty = 1;
            gbc.fill = GridBagConstraints.VERTICAL;
            gbc.insets = new Insets(10, 10, 10, 10);

            BackgroundPane left = new BackgroundPane();
            left.setLayout(new BorderLayout());
            JLabel label = new JLabel(" Menu ");
            label.setHorizontalAlignment(JLabel.CENTER);
            label.setVerticalAlignment(JLabel.CENTER);
            left.add(label);
            add(left, gbc);

            gbc.gridx++;
            gbc.weighty = 0;
            gbc.weightx = 1;
            gbc.fill = GridBagConstraints.BOTH;
            gbc.insets = new Insets(40, 10, 40, 10);
            BackgroundPane right = new BackgroundPane();
            right.setLayout(new BorderLayout());
            label = new JLabel(" Content ");
            label.setHorizontalAlignment(JLabel.CENTER);
            label.setVerticalAlignment(JLabel.CENTER);
            right.add(label);
            add(right, gbc);

        }

    }

    public class BackgroundPane extends JPanel {

        public BackgroundPane() {
            setBackground(Color.GRAY);
            setBorder(new LineBorder(Color.BLACK));
        }

    }

}

我建议您从通读中受益

于 2012-11-15T02:35:53.747 回答