尝试创建一个基本上具有背景的程序(使用Paint()绘制),然后将带有Image的Label放在上面。
继续在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);
}
可悲的是无法让它发挥作用,无论如何都要感谢一帮 哦,我没有看到你做的图像!抱歉!我现在来看看
真的很感激这一切!
答案 0 :(得分: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));
}
}
}
我建议您通过阅读来获益