我想创建一个窗口,其中有一个背景图像和组件。我设法让组件和背景“堆叠”,但我的问题是定位组件。
我尝试过使用AbsoluteLayout,但它似乎不起作用。
到目前为止,这是我的(初学者)代码:
public class RegionPrompt extends JPanel {
public RegionPrompt () throws IOException {
JFrame frame = new JFrame("Map");
GridBagConstraints gbc = new GridBagConstraints();
JPanel pane = new JPanel() {
URL image2 = getClass().getResource("map.jpg");
BufferedImage image = ImageIO.read(image2);
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), this);
}
};
frame.setContentPane(pane);
JPanel pane2 = new JPanel();
pane2.setOpaque(false);
pane2.setSize(DefaultGUI.defaultSize);
pane2.setLayout(new GridBagLayout());
JButton c = new JButton("Map Location");
//gbc.gridx = 0;
//gbc.gridy = 0;
c.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "Location information", JOptionPane.INFORMATION_MESSAGE);
}
});
pane2.add(c,gbc);
frame.add(pane2);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(653, 448);
frame.setVisible(true);
}
}
非常感谢任何帮助(以及对此代码块的宽恕)!