gpt4 book ai didi

java - 尝试将 JLabel 置于 Paint 上方

转载 作者:行者123 更新时间:2023-11-30 04:36:32 29 4
gpt4 key购买 nike

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

继续获取 Paint 下面的 JLabel...

有什么想法吗?

提前非常感谢。

<小时/>
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);

}

}

遗憾的是无法让它工作,无论如何,非常感谢哦,我没有看到你做的图片!对不起!我现在就看一下

非常感谢这一切!

最佳答案

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

不要覆盖 paint顶级容器的方法,如 JFrame ,这些方法做了很多重要的工作,让您弄乱它,并且框架已经在它们之上放置了许多组件,通常会使您的任何工作都过时。

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

在您的情况下,您可以通过更改组件的背景和边框来实现相同的结果。

已更新

enter image description here

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

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

}

}

我建议您仔细阅读会受益

关于java - 尝试将 JLabel 置于 Paint 上方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13390613/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com