0
對不起,如果這已被回答在別的地方,但我找不到解決方案。未顯示背景圖片。被JPanel/JScrollPane攔截?
我有一個JFrame,爲其設置背景和JScrollPane中的JPanel。
但是,當我將scrollpane添加到我的jframe時,它只顯示灰色,即使我將它設置爲不透明。
我想弄明白,所以jpanel背部是透明的,並且按鈕添加到jpanel 在背景上滾動。
這裏是我的代碼:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.*;
public class ZeldaBundle extends JFrame {
JPanel panel = new JPanel();
JScrollPane scroller = new JScrollPane(panel);
JLabel background = new JLabel(new ImageIcon("images/back.jpg"));
JButton theLegendOfZelda = new JButton() {
public void paint(Graphics g) {
try {
g.drawImage(ImageIO.read(new File("images/The Legend Of Zelda.png")), 0, 0, null);
} catch (Exception e) {
setText("The Legend OF Zelda");
}
}
};
public ZeldaBundle() {
setSize(900, 563);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setIconImage(new ImageIcon("images/zelda.png").getImage());
setContentPane(background);
background.setLayout(new BorderLayout());
panel.setPreferredSize(new Dimension(1000, 563));
panel.setLayout(null);
panel.setOpaque(false);
scroller.setOpaque(false);
theLegendOfZelda.setSize(270, 110);
theLegendOfZelda.setLocation(700, 113);
panel.add(theLegendOfZelda);
scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
add(scroller);
setVisible(true);
}
public static void main(String[] args) {
new ZeldaBundle();
}
}
有誰知道如何解決這個問題或知道它已經有了答案?
謝謝。
(是的,我知道它的空佈局,生病改變它時,香港專業教育學院得到這個排序(!))