More than one JPanel in a Frame / having a brackground Image and another Layer with Components on the top

試著忘記壹切 提交于 2019-12-18 09:05:27

问题


I've got a JFrame with a JPanel in which there is a JLabel with an ImageIcon(). Everything's working perfectly, problem is i now want to add another JPanel with all the other stuff like buttons and so on to the JFrame. But it still shows the background Image on top and nothing with the second JPanel.

Can someone help me? Here is an extract of my code:

JFrame window = new JFrame("Http Download");


/*
 * Background Section
 */
JPanel panel1 = new JPanel();

JLabel lbl1 = new JLabel();


/*
 * Component Section
 */
JPanel panel2 = new JPanel();

JLabel lbl2 = new JLabel();


/*
 * Dimension Section
 */
Dimension windowSize = new Dimension(800, 600);
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();

public HTTPDownloadGUI() {

    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    panel1.setLayout(null);
    panel1.setSize(windowSize);
    panel1.setOpaque(false);

    panel2.setLayout(null);
    panel2.setSize(windowSize);
    panel2.setOpaque(false);

    lbl1.setSize(windowSize);
    lbl1.setLocation(0, 0);
    lbl1.setIcon(new ImageIcon(getClass().getResource("bg1.png")));
    panel1.add(lbl1);

    lbl2.setBounds(0, 0, 100, 100);
    //lbl2.setIcon(new ImageIcon(getClass().getResource("bg2.png")));
    lbl2.setBackground(Color.GREEN);
    panel2.add(lbl2);

    panel1.add(panel2);

    window.add(panel1);

    int X = (screen.width / 2) - (windowSize.width / 2);
    int Y = (screen.height / 2) - (windowSize.height / 2);

    window.setBounds(X,Y , windowSize.width, windowSize.height);
    window.setVisible(true);

}

回答1:


  1. Avoid null layouts, here more trouble then they are worth
  2. Set the frame a layout to BorderLayout
  3. Add the label to the frame
  4. Set the labels layout to BorderLayout
  5. Create you panel and set it's opaque property to false
  6. Add the other components to as per normal
  7. Add the panel to the label

Checkout

  • Are there any issues with placing image as background on an extended JFrame?
  • Place JLabel on top of JLabel with image in

For examples

Update with example

  • panel1 is the main background...
  • Set panel1's layout to BorderLayout
  • Add lbl1 to panel1
  • Set lbl1's layout to BorderLayout
  • Set panel2's layout to what ever you want to use...
  • Set panel2's opacity property to false (panel2.setOpacity(false))
  • Add lbl2 to panel2
  • Add panel2 to lbl1
  • Add panel1 to what every you want.

public class TestLayout17 {

    public static void main(String[] args) {
        new TestLayout17();
    }

    public TestLayout17() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        /*
         * Background Section
         */
        JPanel panel1 = new JPanel();
        JLabel lbl1 = new JLabel();
        /*
         * Component Section
         */
        JPanel panel2 = new JPanel();
        JLabel lbl2 = new JLabel();
        /*
         * Dimension Section
         */
        Dimension windowSize = new Dimension(800, 600);
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();

        public TestPane() {

            setLayout(new BorderLayout());

            panel1.setLayout(new BorderLayout());

            lbl1.setLayout(new BorderLayout());
            URL url = getClass().getResource("/bg1.gif");
            System.out.println(url);
            try {
                BufferedImage image = ImageIO.read(url);
                Image smaller = image.getScaledInstance(-1, image.getHeight() / 2, Image.SCALE_SMOOTH);
                lbl1.setIcon(new ImageIcon(smaller));
            } catch (Exception e) {
                e.printStackTrace();
            }
//            lbl1.setIcon(new ImageIcon(url));
            panel1.add(lbl1);

            add(panel1);

            panel2.setLayout(new GridBagLayout());
            panel2.setOpaque(false);

            lbl2.setBorder(new EmptyBorder(8, 8, 8, 8));
            lbl2.setBackground(Color.GREEN);
            lbl2.setText("Say hello");;
            lbl2.setOpaque(true);
            panel2.add(lbl2);

            lbl1.add(panel2);

        }
    }
}



回答2:


The reason that nothing appears to be shown for panel2 is because there is nothing to display. Simply setting text for lbl2 will show that the JPanel itself is visible:

JLabel lbl2 = new JLabel("Some text");

I would, however advise against using null layout as it places the overhead on the developer for setting locations & dimensions which a layout manager can easily manage for you.




回答3:


I would advise you to heed MadProgrammer's advice. If you chose to go with a null layout, make sure you are removing the previous JPanel and adding the new one to the content pane window.setContentPane(panel2); and calling window.repaint().



来源:https://stackoverflow.com/questions/13886476/more-than-one-jpanel-in-a-frame-having-a-brackground-image-and-another-layer-w

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!