为什么" dispose()"和" setVisible()"是不是关闭了表格?

时间:2018-02-23 16:25:19

标签: java swing jframe dispose

我从JFrame类获得了两个表单。通过按钮单击,其中一个必须关闭,另一个打开。我已尝试使用" setVisible()"并且" dispose()",结果是一样的 - 没有任何改变。在我的代码类下面,带有" dispose()" -try:

的版本
 public static void btnEnterHandler() throws Exception{
    JFrame mainFrame = Forms.mainFrameDraw(new Chat());
    JFrame enterFrame = Forms.enterFrameDraw(new Chat());
    try{
        enterFrame.dispose();
        mainFrame.setVisible(true);
    } catch(Exception ex2) {
        Logger.getLogger(enterFrame.getName());
    }
    //todo stopped here!
}

带表格的课程:

public static JFrame mainFrameDraw(JFrame frame) {
    textAreaMain = new JTextArea(FRM_HEIGHT / 19, 50);
    textFieldMessage = new JTextField();
    textAreaMain.setLineWrap(true);
    textAreaMain.setEditable(false);
    JScrollPane spMain = new JScrollPane(textAreaMain);
    spMain.setLocation(0, 0);

    JButton btnSend = new JButton();
    btnSend.setText("Send");
    btnSend.setToolTipText("Broadcast a message");
    btnSend.addActionListener(e -> {
        try {
            Chat.btnSendHandler();
        } catch (Exception e1) {
            e1.printStackTrace();
        }
    });

    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.setTitle(FRM_TITLE);
    frame.setLocation(FRM_LOC_X, FRM_LOC_Y);
    frame.setSize(FRM_WIDTH, FRM_HEIGHT);
    frame.setResizable(false);
    frame.getContentPane().add(BorderLayout.NORTH, spMain);
    frame.getContentPane().add(BorderLayout.CENTER, textFieldMessage);
    frame.getContentPane().add(BorderLayout.EAST, btnSend);
    frame.setVisible(false);

    return frame;
}

public static JFrame enterFrameDraw(JFrame frame){
    JButton btnEnter = new JButton("Sign in!");
    textFieldLogin = new JTextField("Login!");
    btnEnter.addActionListener(e -> {
        try{
            Chat.btnEnterHandler();
        } catch (Exception e2){
            e2.printStackTrace();
        }
    });


    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.setTitle(FRM_TITLE);
    frame.setLocation(FRM_LOC_X, FRM_LOC_Y);
    frame.setSize(FRM_WIDTH, FRM_HEIGHT);
    frame.getContentPane().add(BorderLayout.PAGE_START, textFieldLogin);
    frame.setResizable(false);
    frame.getContentPane().add(BorderLayout.PAGE_END, btnEnter);
    frame.setVisible(true);

    return frame;
}

}

不是所有代码。但我认为,这已经足够了。如果您需要更多 - 请告诉我。

1 个答案:

答案 0 :(得分:0)

首先,尽量避免只使用静态类,因为它实际上不是java的内容。获得有关 SOLID原则的一些知识,它会对您有所帮助。

如果是关于你的情况:

enterFrame.dispose();

单击按钮,似乎您正在初始化全新帧,然后立即关闭其中一个:

JFrame

您要做的是在btnEnterHandler()内设置public static JFrame enterFrameDraw(JFrame frame){ //... btnEnter.addActionListener(e -> { try{ Chat.btnEnterHandler(frame); } catch (Exception e2){ e2.printStackTrace(); } }); 的实例。

工作示例(但请考虑不要使用这么多静态!):

public static void btnEnterHandler(JFrame frame) throws Exception{
    JFrame mainFrame = Forms.mainFrameDraw(new Chat());
    try{
        frame.dispose();
        mainFrame.setVisible(true); //i'm pretty sure its not necessary because its visible by default
    } catch(Exception ex2) {
        Logger.getLogger(enterFrame.getName());
    }
}

然后您的处理程序如下:

var selectedVideoIndex: Int!


override func viewDidLoad()
{
    super.viewDidLoad()

    episodes = Episode.downloadAllEpisodes()
    self.tableView.reloadData()

    tableView.estimatedRowHeight = tableView.rowHeight
    tableView.rowHeight = UITableViewAutomaticDimension
    tableView.separatorStyle = .none

}

override var preferredStatusBarStyle : UIStatusBarStyle {
    return .lightContent
}

// MARK: - Table view data source

override func numberOfSections(in tableView: UITableView) -> Int
{
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    return episodes.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCell(withIdentifier: "Episode Cell", for: indexPath) as! EpisodeTableViewCell
    let episode = episodes[indexPath.row]

    cell.episode = episode

    return cell
}

// MARK: - UITableViewDelegate

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath:
    IndexPath)
{
    tableView.deselectRow(at: indexPath, animated: true)

    performSegue(withIdentifier: "secondView", sender: indexPath.row)

}



override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    var next = segue.PViewController  as! PlayTableViewController

    next.index = sender as? Int
}