1

私は JFrame の背景画像を設定しようとしましたが、今は私のコンポーネントが (私が推測するに) その背後にスタックしています。トップレベルのコンテナについて読んでみましたが、理解できません T_T. アイデアはありますか?たぶん、背景画像を設定する別の方法を見つける必要がありますか? 助けてくれてありがとう。:

@SuppressWarnings("serial")
public class CopyOfMainMenu extends JFrame {

//Running the GUI
    public static void main(String[] args) throws IOException {
        CopyOfMainMenu gui2 = new CopyOfMainMenu();
        gui2.mainPanel();
    }

    public void thepanel () throws IOException {
        // GridBagLayout/Constraint
        GridBagConstraints gridbagc = new GridBagConstraints();
        gbc.insets = new Insets(15, 15, 15, 15);
        JFrame frame = new JFrame();
        JPanel panel = new JPanel(new GridBagLayout());
        // Creating JButtons/Icons
        panel.add(buttonTwo, gbc); //SCOREBOARD
        panel.add(buttonThree, gbc); //INSTRUCTIONS
        // JButton size's
        button.setPreferredSize(new Dimension(400, 35))
        buttonTwo.setContentAreaFilled(false);
        buttonThree.setBorder(BorderFactory.createEmptyBorder());
        buttonThree.setContentAreaFilled(false);
    }
}

繰り返しますが、助けてくれてありがとう。

4

2 に答える 2

3

パネルをどこにも追加することはありません (追加すると、画像が塗りつぶされます)。ただし、JLabelコンテナとして a を使用することは可能です:

JComponent panel = new JLabel(new ImageIcon(ImageIO.read(new File("res/FinalBG.png"))));
panel.setLayout(new GridBagLayout());
...
// continue adding the components as before
...
frame.add(panel);

(または、ラベルをコンテンツ ペインとして設定しても、同様に機能します)。

于 2013-09-14T20:03:33.433 に答える
1

JFrame 内で、paint メソッドをオーバーライドし、Graphics オブジェクトを使用して画像を描画します。

public void paint(Graphics g) {
    super.paint(g);
    Graphics2D g2d = (Graphics2D) g;
    g2d.drawImage(image, width, height, null);
}
于 2013-09-14T20:11:39.407 に答える