java JFrame setContentPanel然后JPanel消失

时间:2014-08-17 07:31:02

标签: java swing jframe jpanel

我有一个代码:

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);
    }
}

我想使用 setContentPane 方法在两个JPanel( contentPane panel_main )之间切换。

有两个重要方法: goFullReadMode ()和 exitFullReadMode ()。

当我运行此程序时, goFullReadMode ()正确执行,但当切换回默认面板(单击文本外部的粉红色区域)时,中间面板( panel_main )消失了。为什么它不再显示? enter image description here

我认为它已被移动到某处,所以在 exitFullReadMode 方法中,我尝试:

contentPane.add(panel_main);

但它没有显示在正确的位置,因为容器有一个GroupLayout,并且重新调整所有组件并不是一个好主意。

后续解决方案是将 panel_main setContentPane 的副本复制到此副本,但我们需要复制所有内容(数据,子组件,...)到这个副本对象(就像goFullReadMode方法中的注释)。所以这不是一件容易的事。

是否有任何解决方案不需要复制面板并在切换回默认视图时仍然保留它? 感谢。

1 个答案:

答案 0 :(得分:2)

由于您在致电panel_main时仅使用this.setContentPane(panel_main2);的一个引用,因此它会从contentPane面板中自行删除,并将其自身添加到Frame }作为主要Panel,因此在退出全屏时,您无法在contentPane面板中看到它。

<强>溶液

由于您无法将其重新调整为其状态,因此您可以为ReadPanel创建一个contentPane实例,为全屏创建一个实例

    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();
        }

用于 fullScreen

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

现在,当您更新panel_main的文字时,您也必须更新panel_main2