java中添加背景_java中如何增加背景图片
我的程序代碼如下,試了加背景的方法雖然程序沒有錯誤,但是背景還是顯示不出來,按鈕的setsize好像也不起作用,希望高人能助一臂之力,小女子先寫過各位了:、、、、importjava.awt....
我的程序代碼如下,試了加背景的方法雖然程序沒有錯誤,但是背景還是顯示不出來,按鈕的setsize好像也不起作用,希望高人能助一臂之力,小女子先寫過各位了:、、、、 import java.awt.*; import javax.swing.*; public class MainFrame extends JFrame { public static void main(String[] args) { JFrame frame=new JFrame("補考管理系統"); frame.setSize(300,200); frame.setBackground(Color.gray); /*((JPanel) frame.getContentPane()).setOpaque(false); // Winter.jpg這個圖片的位置要跟當前這個類是同一個包下 ImageIcon img = new ImageIcon("c://aaa.jpeg"); JLabel background = new JLabel(img); frame.getLayeredPane().add(background, new Integer(Integer.MIN_VALUE)); background.setBounds(0, 0, img.getIconWidth(), img.getIconHeight()); */ ImageIcon img = new ImageIcon("//practice//src//img//login_mainbg.jpg");//這是背景圖片 JLabel imgLabel = new JLabel(img);//將背景圖放在標簽里。 frame.getLayeredPane().add(imgLabel, new Integer(Integer.MIN_VALUE));//注意這里是關鍵,將背景標簽添加到jfram的LayeredPane面板里。 imgLabel.setBounds(0,0,img.getIconWidth(), img.getIconHeight());//設置背景標簽的位置 //JFrame frame=new JFrame("補考管理系統"); // frame.addComponent(new FrameListener());//添加監聽器 //設置用戶關閉框架時的響應動作 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JLabel title=new JLabel("歡迎進入補考管理系統"); Font fnt=new Font("Serief",Font.BOLD+Font.ITALIC,40); title.setFont(fnt); Panel userpan=new Panel();//用戶信息面板 JLabel userlab=new JLabel("用戶名:"); JLabel mmlab=new JLabel("密碼:"); JTextField username=new JTextField(); TextField password=new TextField(6); JButton login=new JButton("登錄"); JButton Reset=new JButton("重置"); password.setEchoChar('*'); //login.resize(5,5); // Reset.resize(5,5); userpan.setSize(100,100); userpan.setLocation(300,300); userpan.setLayout(new GridLayout(3,2,0,5)); //3行2列的網格布局管理器 userpan.add(userlab); userpan.add(username); userpan.add(mmlab); userpan.add(password); userpan.add(login); userpan.add(Reset); frame.add(title,BorderLayout.NORTH); frame.add(userpan,BorderLayout.CENTER); frame.setResizable(false); frame.setVisible(true); } }
展開
總結
以上是生活随笔為你收集整理的java中添加背景_java中如何增加背景图片的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 常见php面试题,常见的 PHP 面试题
- 下一篇: MySQL8怎么设置时区为东八区_mys