【问题标题】:Background image on JFrame [duplicate]JFrame上的背景图像[重复]
【发布时间】:2015-07-04 04:17:10
【问题描述】:

我从 Java 编程语言开始。我想制作一个 JFrame 并在其上和 JFrame 上方放置一个背景图像,我想插入小部件: JTextArea , JButton ...但它们不能重叠。这是我的代码:

public void go() {
    cadre = new JFrame("Premiere Feneêtre");
    cadre.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    cadre.setSize(600, 600);
    cadre.setVisible(true);`

    BorderLayout agencement = new BorderLayout();
    JPanel arrierePlan = new JPanel(agencement);
    arrierePlan.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

    Box boiteABoutons = new Box(BoxLayout.PAGE_AXIS);
    boiteABoutons.setSize(150,150);
    boiteABoutons.setOpaque(false);
    //boiteABoutons.setBackground(Color.yellow);
    b1 = new JButton("Bouton 1");
    b1.addActionListener(this);
    b2 = new JButton("Bouton 2");
    b2.addActionListener(this);
    b3 = new JButton("Bouton 3");
    boiteABoutons.add(b1);
    boiteABoutons.add(b2);
    boiteABoutons.add(b3);
    zoneTexte = new JTextArea();

    Panneau p1 = new Panneau();
    arrierePlan.add(BorderLayout.SOUTH, boiteABoutons);
    arrierePlan.add(BorderLayout.NORTH,zoneTexte);

    cadre.getContentPane().add(p1);
    cadre.getContentPane().add(arrierePlan);

}

public class Panneau extends JPanel {
    public void paintComponent(Graphics g){
        try {
            Image img = ImageIO.read(new File("C:/Users/........png"));
            //g.drawImage(img, 10, 10, this);
            g.drawImage(img, 10, 10, this.getWidth(), this.getHeight(), this);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

关于我遇到的这个问题,我已经阅读了几篇文章,但没有任何帮助。

谢谢 抱歉英语不好

【问题讨论】:

    标签: java image swing background


    【解决方案1】:
    cadre.getContentPane().add(p1);
    cadre.getContentPane().add(arrierePlan);
    

    您不能将多个组件添加到 BorderLayout 的 CENTER。

    您想要将组件添加到图像面板:

    Panneau p1 = new Panneau();
    p1.setLayout( new BorderLayout() );
    p1.add(boiteABoutons, BorderLayout.SOUTH);
    p1.add(zoneTexte, BorderLayout.NORTH);
    cadre.add(p1);
    

    以后在提问前先搜索论坛(主题标题通常包含好的关键字开始)。正如您在本页右侧 Related 标题下的帖子中看到的那样,这个问题经常被问到。

    【讨论】:

      猜你喜欢
      • 2017-04-29
      • 1970-01-01
      • 2014-01-13
      • 2011-02-08
      • 1970-01-01
      • 1970-01-01
      • 2015-06-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多