单击按钮打开框架

时间:2016-08-11 12:10:05

标签: java

在我的项目中,我尝试点击LoginFrame来自WelcomeFrame点击a button and I want my WelcomeFrame to be closed as well. I have successfully opened the LoginFrame by using setVisible(true). To close the WelcomeFrame I have written frame.SetVisible(false)where frame is the object of WelcomeFrame`但此行显示错误:无法解析帧....

这是我的代码..请帮忙

public class WelcomeFrame extends JFrame{

    private JPanel contentPane;

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



JButton btnNewButton = new JButton("Librarian Portal\r\n");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
LoginFrame l=new LoginFrame();
l.setVisible(true);
frame.setVisible(false);  //error:frame cannot be resolved
    }
        });

3 个答案:

答案 0 :(得分:0)

因为无法访问WelcomeFrame的对象。

在尝试调用setVisible方法之前,您需要创建另一个对象。

margin:0px auto;

答案 1 :(得分:0)

公共类WelcomeFrame扩展了JFrame {

private JPanel contentPane;
private WelcomeFrame frame;

 EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                frame = new WelcomeFrame();

我认为它会帮助你

答案 2 :(得分:0)

CardLayout更适合您的需求;一般的想法是创建一个jPanel而不是JFrame,CardLayout允许您通过一次只显示一个来切换它们。如果这是你需要的have alook here