Make background image show through panels on top

前端 未结 1 1751
我寻月下人不归
我寻月下人不归 2021-01-27 12:12

When I add panels and objects in panels, I want the image to be set as the background and show through the panels put on top of it. I read you can do this bet setting the

相关标签:
1条回答
  • 2021-01-27 12:33

    You're replacing the backgroundFrame with centerFrame

    add(backgroundFrame);
    add(topFrame, BorderLayout.NORTH);
    add(centerFrame, BorderLayout.CENTER);
    

    This is feature of BorderLayout, where it can only support a single component at each of it's 5 available positions

    Instead, what you want to do is either add the other panels to the backgroundFrame or set backgroundFrame as the frame's contentPane, for example

    setContentPane(backgroundFrame);
    add(topFrame, BorderLayout.NORTH);
    add(centerFrame, BorderLayout.CENTER);
    

    Also, as a general suggestion, avoid using setPreferredSize, there are simply too many things that can make that go wrong

    Sorry to bother you again, it looks like it is not showing through the JScrollPane. Should I open this up as a new question?

    The JScrollPane is a more complex component, which is made up of two components which are used to provide the functionality of scrolling

    You need to make the JScrollPane and it's JViewport transparent as well (as well as the view you wrap into it)

    So, this is a modified version of your createCenterFrame method which creates a JTextArea, wraps into a JScrollPane and adds it to the pnl

    private JPanel createCenterFrame() {
        JPanel pnl = new JPanel();
        Border raisedBevel = BorderFactory.createRaisedBevelBorder();
        Color lineColor = new Color(224, 224, 224);
        Border lineBorder = BorderFactory.createMatteBorder(5, 5, 5, 5, lineColor);
        Border loweredBevel = BorderFactory.createLoweredBevelBorder();
        Border compoundSetup = BorderFactory.createCompoundBorder(raisedBevel, lineBorder);
        Border compoundFinal = BorderFactory.createCompoundBorder(compoundSetup, loweredBevel);
        TitledBorder topFrameTitle = BorderFactory.createTitledBorder(compoundFinal, "Stuff");
        topFrameTitle.setTitleJustification(TitledBorder.CENTER);
    
        pnl.setBorder(topFrameTitle);
        pnl.setLayout(new BorderLayout());
    
        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setOpaque(false);
        scrollPane.getViewport().setOpaque(false);
    
        JTextArea ta = new JTextArea();
        ta.setOpaque(false);
        ta.setText("This is an example of a transparent text area inside a transparent scroll pane, but you could just as easily wrap a transparent panel into it and get the same result");
        ta.setWrapStyleWord(true);
        ta.setLineWrap(true);
    
        scrollPane.setViewportView(ta);
    
        pnl.add(scrollPane);
    
        pnl.setOpaque(false);
        return pnl;
    }
    

    0 讨论(0)
提交回复
热议问题