如何在带有图像的叠加布局上添加按钮
How to add buttons on an overlay layout with an image
我想将图像设置为背景以便在其上方放置按钮,所以我使用了叠加层。 Ι 只是想把按钮的位置改到图片上面,但是找不到办法
public static void main(String[] args){
Menu program = new Menu();
SwingUtilities.invokeLater(program);
}
public void run() {
JFrame frame = new JFrame () ;
Get_size get_size = new Get_size() ;
get_size.Size_screen() ;
int h = (int)height ;
int w = (int) width;
frame.setSize(w , h );
JPanel panel = new JPanel();
//Create the first button
JButton appointment = new JButton("Appointment menu & Diary");
appointment.setPreferredSize(new Dimension(500,32));
appointment.setFont(new Font("Algerian", Font.PLAIN , 24));
panel.add(appointment) ;
//Create the second button
JButton patient_profile = new JButton("Patient Profile");
patient_profile.setPreferredSize(new Dimension(300, 32));
patient_profile.setFont(new Font("Algerian", Font.PLAIN , 24));
panel.add(patient_profile) ;
JPanel over = new JPanel();
LayoutManager overlay = new OverlayLayout(over);
over.setLayout(overlay);
JPanel imagen1 = new JPanel();
ImageIcon image = new ImageIcon("menu.jpg") ;
imagen1.add(new JLabel(image));
over.add(imagen1);
over.add(panel);
frame.add(over);
//sets the size of the frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//makes it so you can close
frame.setVisible(true);
// Set the frame of the size non-resizable
frame.setResizable(false);
}
您必须定义布局。是否为空或框架布局或边框等
在此处查找 https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
over.add(imagen1);
over.add(panel);
Swing 以与添加组件相反的顺序绘制组件。所以上面的代码将绘制面板,然后在面板顶部绘制图像,因此您只会看到图像。
代码应该反过来:
over.add(panel);
over.add(imagen1);
现在面板将绘制在图像之上。但是,由于面板是不透明的,您将看不到图像,因此您还需要使面板不透明:
JPanel panel = new JPanel();
panel.setOpaque(false);
我想将图像设置为背景以便在其上方放置按钮,所以我使用了叠加层。 Ι 只是想把按钮的位置改到图片上面,但是找不到办法
public static void main(String[] args){
Menu program = new Menu();
SwingUtilities.invokeLater(program);
}
public void run() {
JFrame frame = new JFrame () ;
Get_size get_size = new Get_size() ;
get_size.Size_screen() ;
int h = (int)height ;
int w = (int) width;
frame.setSize(w , h );
JPanel panel = new JPanel();
//Create the first button
JButton appointment = new JButton("Appointment menu & Diary");
appointment.setPreferredSize(new Dimension(500,32));
appointment.setFont(new Font("Algerian", Font.PLAIN , 24));
panel.add(appointment) ;
//Create the second button
JButton patient_profile = new JButton("Patient Profile");
patient_profile.setPreferredSize(new Dimension(300, 32));
patient_profile.setFont(new Font("Algerian", Font.PLAIN , 24));
panel.add(patient_profile) ;
JPanel over = new JPanel();
LayoutManager overlay = new OverlayLayout(over);
over.setLayout(overlay);
JPanel imagen1 = new JPanel();
ImageIcon image = new ImageIcon("menu.jpg") ;
imagen1.add(new JLabel(image));
over.add(imagen1);
over.add(panel);
frame.add(over);
//sets the size of the frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//makes it so you can close
frame.setVisible(true);
// Set the frame of the size non-resizable
frame.setResizable(false);
}
您必须定义布局。是否为空或框架布局或边框等
在此处查找 https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
over.add(imagen1);
over.add(panel);
Swing 以与添加组件相反的顺序绘制组件。所以上面的代码将绘制面板,然后在面板顶部绘制图像,因此您只会看到图像。
代码应该反过来:
over.add(panel);
over.add(imagen1);
现在面板将绘制在图像之上。但是,由于面板是不透明的,您将看不到图像,因此您还需要使面板不透明:
JPanel panel = new JPanel();
panel.setOpaque(false);