簡體   English   中英

將圖像文件設置為背景

[英]Setting an image file as background

我有一個擴展JFrame的類,其中包含3個面板+另一個將所有3個面板保持在一起的布局。

panel = new JPanel();
    panel.setLayout(new BorderLayout());
    panel.add("North", panel2); //row of buttons
    panel.add("Center",grid); // grid of buttons
    panel.add("South",panel3); // a textfield that stretches across the total width of the frame
    add(panel);



    JLabel background = new JLabel (new ImageIcon ("C:\\.png"));
    background.setBounds (0,0,500,550);
    add(background);
    //the background for the frame


    setVisible (true);
    setResizable (false);
    setBounds (398,70, 570, 620);
    setDefaultCloseOperation (EXIT_ON_CLOSE);

我的問題是程序給我的輸出是jlabel中的背景圖像/圖像僅可見或以某種方式使面板模糊/使按鈕不可見。 我需要將背景圖片放在按鈕后面,這就是為什么這就是所謂的背景我想念的是什么?

  • 我沒有使用容器,因為好吧,這不會很快被設置為applet,如果這個原因就可以將其設置為idk,但是

可能最簡單的實現方法是,僅使JLabel成為框架的內容窗格...

JLabel background =  new JLabel (new ImageIcon ("C:\\.png"));
setContentPane(background);

隨之而來的第二個事實是,默認情況下, JLabel沒有布局管理器,這會使您向其中添加其他內容變得有些困難(或者至少不會達到您的期望),因此,接下來,您需要設置布局管理器...

setLayout(new BorderLayout()); // Forwarded to the content pane...

然后,您可以愉快地添加所有內容...

add(panel2, BorderLayout.NORTH); //row of buttons
add(grid); // grid of buttons
add(panel3, BorderLayout.SOUTH); // a textfield that

您可能遇到的下一個問題是大多數組件都是不透明的,因此,如果希望查看背景,則可能要在要添加的組件上調用setOpaque(false)

最后...

setVisible (true);
setResizable (false);

這會給您帶來一些問題,請嘗試使用...

setResizable(false);
pack();
setVisible(true);

ps- JLabel不會自動調整圖像的大小,但是打包應該可以幫助...假設其余內容適合背景圖像的大小...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM