1

我有两个观点:test1 和 test2

从 test1 切换到 test 2 工作正常,但是当我从 test2 切换回来时, contentPanel 是空的。这是我的代码。有人可以指出我做错了什么吗?

测试1

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;


public class test1 extends JFrame {

JPanel contentPane;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                test1 frame = new test1();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public test1() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    contentPane.setLayout(new BorderLayout(0, 0));
    setContentPane(contentPane);

    JButton btnView = new JButton("View2");
    contentPane.add(btnView, BorderLayout.CENTER);
    btnView.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            contentPane.removeAll();
            contentPane.invalidate();
            test2 obj=new test2();
            obj.contentPane.setVisible(true);
            contentPane.add(obj.contentPane);
            ((JPanel)contentPane).revalidate();
            contentPane.repaint();          }
    });
}

}

测试2

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;

public class test2 extends JFrame {

JPanel contentPane;

/**
 * Launch the application.
 */
public static void main(String[] args) {

    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                test2 frame = new test2();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public test2() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JLabel lblThisIsView = new JLabel("this is view 2!");
    lblThisIsView.setBounds(178, 107, 110, 14);
    contentPane.add(lblThisIsView);

    JButton btnView = new JButton("View 1!");
    btnView.setBounds(168, 161, 89, 23);
    contentPane.add(btnView);

    btnView.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            contentPane.removeAll();
            contentPane.invalidate();
            test1 obj=new test1();
            obj.contentPane.setVisible(true);
            contentPane.add(obj.contentPane);
            ((JPanel)contentPane).revalidate();
            contentPane.repaint();
        }
    });
}
    }
4

2 回答 2

3

这是布局的基本问题。在test1.java您添加了带有BorderLayout. 但不在test2.java.

test2 的布局应该是BorderLayout. 之后,应使用以下语法添加所有组件 -

contentPane.add(lblThisIsView,BorderLayout.CENTER);

完成的代码test2.java-

public test2() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(new BorderLayout(0, 0));

        JLabel lblThisIsView = new JLabel("this is view 2!");
        lblThisIsView.setBounds(178, 107, 110, 14);
        contentPane.add(lblThisIsView,BorderLayout.CENTER);

        JButton btnView = new JButton("View 1!");
        btnView.setBounds(168, 161, 89, 23);
        contentPane.add(btnView,BorderLayout.SOUTH);

        btnView.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                contentPane.removeAll();
                contentPane.invalidate();
                test1 obj = new test1();
                obj.contentPane.setVisible(true);
                contentPane.add(obj.contentPane);
                ((JPanel) contentPane).revalidate();
                contentPane.repaint();
            }
        });
    }

总代码在这里


从评论中可以看出,最好的方法是使用CardLayout- 添加面板 -

public CardLayoutHelper(JPanel panel, JPanel... panels) {
        this(panel);
        for (int i = 0; i < panels.length; i++) {
            JPanel jPanel = panels[i];
            panel.add(jPanel.getName(), jPanel);
        }
    }

切换面板 -

 public void switchPanel(String name) {
            layout.show(panel, name);
            panel.revalidate();
            panel.repaint();
        }

您可以CardLayoutHelpergithub获取。

于 2012-12-05T08:57:43.533 回答
-2

到我刚刚设置的当前帧:

"classname.this.setVisible(false);"

帧切换现在工作正常。这不是正确的方法,但我的项目提交是今天,解决方案很简单,可以在任何地方实施。

于 2012-12-06T00:54:45.827 回答