简体   繁体   中英

java JFrame setContentPanel and then JPanel disappear

I have a code:

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.Color;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

class ReadPanel extends JPanel{
    private static final long serialVersionUID = 1L;
    private JTextArea textArea;

    public ReadPanel(){
        super();
        setBackground(new Color(255, 182, 193));
        JScrollPane scrollPane = new JScrollPane();
        GroupLayout gl_panel_main = new GroupLayout(this);
        gl_panel_main.setHorizontalGroup(
            gl_panel_main.createParallelGroup(Alignment.LEADING)
                .addGroup(gl_panel_main.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 402, Short.MAX_VALUE)
                    .addContainerGap())
        );
        gl_panel_main.setVerticalGroup(
            gl_panel_main.createParallelGroup(Alignment.LEADING)
                .addGroup(gl_panel_main.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE)
                    .addContainerGap())
        );

        textArea = new JTextArea();
        scrollPane.setViewportView(textArea);
        setLayout(gl_panel_main);
    }
    public void setNews(String news){
        textArea.setText(news);
    }
    public String getNews(){
        return textArea.getText();
    }
    //another complex method here...
}

public class SetContentPanelFrame extends JFrame {

    private static final long serialVersionUID = 1L;
    private JPanel contentPane;
    private ReadPanel panel_main;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    SetContentPanelFrame frame = new SetContentPanelFrame();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    public void goFullReadMode(){
        /*ReadPanel copy = new ReadPanel();
        copy.setNews(panel_main.getNews());
        copy.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {
                exitFullReadMode();
            }
        });
        //Set another property here...
        this.setContentPane(copy);*/

        this.setContentPane(panel_main);
        this.repaint();
        this.revalidate();
    }
    public void exitFullReadMode(){
        this.setContentPane(contentPane);
        //contentPane.add(panel_main);
        this.repaint();
        this.revalidate();
    }

    /**
     * Create the frame.
     */
    public SetContentPanelFrame() {
        setTitle("Set Content Frame");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 741, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);

        JPanel panel_menu = new JPanel();
        panel_menu.setBackground(new Color(240, 248, 255));

        JPanel panel_ads = new JPanel();
        panel_ads.setBackground(new Color(255, 239, 213));

        panel_main = new ReadPanel();
        panel_main.setNews("This is a news\r\n1\r\n\r\n\r\n\r\n\r\n\r\n\r\n"
                + "This is a news\r\n2\r\n\r\n\r\n\r\n"
                + "This is a news\r\n3\r\n\r\n\r\n"
                + "This is a news\r\n4");
        panel_main.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {
                exitFullReadMode();
            }
        });

        JButton btnViewFull = new JButton("View full");
        btnViewFull.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                goFullReadMode();
            }
        });
        GroupLayout gl_contentPane = new GroupLayout(contentPane);
        gl_contentPane.setHorizontalGroup(
            gl_contentPane.createParallelGroup(Alignment.LEADING)
                .addGroup(gl_contentPane.createSequentialGroup()
                    .addComponent(panel_menu, GroupLayout.PREFERRED_SIZE, 140, GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(ComponentPlacement.RELATED)
                    .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                        .addGroup(gl_contentPane.createSequentialGroup()
                            .addComponent(btnViewFull)
                            .addContainerGap())
                        .addGroup(gl_contentPane.createSequentialGroup()
                            .addComponent(panel_main, GroupLayout.DEFAULT_SIZE, 422, Short.MAX_VALUE)
                            .addPreferredGap(ComponentPlacement.RELATED)
                            .addComponent(panel_ads, GroupLayout.PREFERRED_SIZE, 141, GroupLayout.PREFERRED_SIZE))))
        );
        gl_contentPane.setVerticalGroup(
            gl_contentPane.createParallelGroup(Alignment.LEADING)
                .addGroup(gl_contentPane.createSequentialGroup()
                    .addComponent(btnViewFull)
                    .addPreferredGap(ComponentPlacement.RELATED)
                    .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                        .addComponent(panel_main, GroupLayout.DEFAULT_SIZE, 222, Short.MAX_VALUE)
                        .addComponent(panel_ads, GroupLayout.DEFAULT_SIZE, 222, Short.MAX_VALUE)
                        .addComponent(panel_menu, GroupLayout.DEFAULT_SIZE, 222, Short.MAX_VALUE)))
        );

        panel_ads.setLayout(new BorderLayout(0, 0));

        JLabel lblAds = new JLabel("Ads...");
        lblAds.setHorizontalAlignment(SwingConstants.CENTER);
        panel_ads.add(lblAds, BorderLayout.CENTER);
        panel_menu.setLayout(new BorderLayout(0, 0));

        JLabel lblMenu = new JLabel("Menu");
        lblMenu.setHorizontalAlignment(SwingConstants.CENTER);
        panel_menu.add(lblMenu, BorderLayout.CENTER);
        contentPane.setLayout(gl_contentPane);
    }
}

I want to switch between two JPanel ( contentPane and panel_main ) using setContentPane method.

There are 2 important method: goFullReadMode () and exitFullReadMode ().

When I run this program, the goFullReadMode () do correctly, but when switch back to default panel (click the pink area outside the text), the middle panel ( panel_main ) is disappear. Why it doesn't display again? 在此处输入图片说明

I think that it has been moved to somewhere, so in exitFullReadMode method, I try:

contentPane.add(panel_main);

But it doesn't display in correct position because the container has a GroupLayout, and regoup all component is not a good idea.

An afternate solution is make a copy of panel_main and setContentPane to this copy, but we need to copy all content(data, sub-component, ...) to this copy object (like the comment in goFullReadMode method). So it is not easy way to do.

Is there any solution that does not need make a copy of the panel and still keep it when we switch back to default view? Thanks.

Since you are using only one reference to the panel_main by the time you call this.setContentPane(panel_main2); it will then remove it self from the contentPane panel and add itself to the Frame as its main Panel thus you can not see it in your contentPane panel upon exiting the fullscreen.

solution:

since you cant resize it back to its state, you can create 2 instance of ReadPanel one for the contentPane and one for the fullscreen

    panel_main = new ReadPanel();
    panel_main2 = new ReadPanel();
    panel_main.setNews("This is a news\r\n1\r\n\r\n\r\n\r\n\r\n\r\n\r\n"
            + "This is a news\r\n2\r\n\r\n\r\n\r\n"
            + "This is a news\r\n3\r\n\r\n\r\n"
            + "This is a news\r\n4");

    panel_main2.setNews("This is a news\r\n1\r\n\r\n\r\n\r\n\r\n\r\n\r\n"
            + "This is a news\r\n2\r\n\r\n\r\n\r\n"
            + "This is a news\r\n3\r\n\r\n\r\n"
            + "This is a news\r\n4");
    panel_main2.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent arg0) {
            exitFullReadMode();
        }

For going fullScreen

 public void goFullReadMode(){
    this.setContentPane(panel_main2);
    this.repaint();
    this.revalidate();
}

Now when you update the text of the panel_main you must update the panel_main2 as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM