java+swing中,运行窗体时,按键不会立刻跟随背景图片出现,只有鼠标滑过按键位置时,按键才会出现

package hospital;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.enterprise.inject.New;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class HomePage extends JFrame{
	
    private JLabel la1, la2;
    
    private Font laFont = new Font("隶书", Font.BOLD, 60);
    private Font btFont=new Font("宋体",Font.BOLD,25);
    
    
    ImageIcon background = new ImageIcon("WebRoot\\image\\2051854.jpg");
    JLabel bgJLabel = new JLabel(background);
    
    JButton doctorJButton=new JButton("医生信息管理");
    JButton patientJButton=new JButton("病人信息管理");
    JButton wardJButton=new JButton("科室信息管理");
    
    
    
 
    public HomePage() {}
    
    
    public HomePage(String a){
      	super(a);
    	this.setLayout(null);
    	
    	this.add(doctorJButton);
        this.add(patientJButton);
        this.add(wardJButton);
        doctorJButton.setFont(btFont);
        patientJButton.setFont(btFont);
        wardJButton.setFont(btFont);

        doctorJButton.setBounds(0, 0, 200, 200);
        patientJButton.setBounds(0, 200, 200, 200);
        wardJButton.setBounds(0, 400, 200, 200);
    
    
        la1 = new JLabel("欢迎使用");
        la2 = new JLabel("医院信息管理系统");
 
        
        this.add(la1);
        this.add(la2);
        la1.setBounds(500, 100, 500, 100);
        la1.setFont(laFont);
        la2.setBounds(400,200, 600, 100);
        la2.setFont(laFont);
       
 
        this.add(bgJLabel);
        bgJLabel.setBounds(0, 0, 1000, 600);
        
        wardJButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				
				new department("科室管理界面");
				HomePage.this.setVisible(false);
				
			}
		});
       
    
    	this.setTitle("主页面");
    	this.setResizable(true);
    	this.setVisible(true);
    	this.setSize(1000, 600);
    	this.setLocationRelativeTo(null);
    	this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	
    	
    }
    public static void main(String[] args) {
		HomePage homePage=new HomePage("主页面1");
		
		
	}

}

这是因为窗体添加组件也区分优先级,只需要把按键组件在有参构造方法中优先添加,背景图片组件最后添加便可以解决这个问题。

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

做一道光

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值