如何关闭框架并打开另一个框架?

时间:2015-11-19 02:09:01

标签: java swing

到目前为止,我有一个程序弹出一个框架,询问文件位置。如果找不到该文件,则显示“找不到文件”,但如果找到该文件,则显示“找到文件”。如果找到该文件,我也想让框架关闭并打开一个新框架。我不知道怎么做。到目前为止,这是我的代码。

小组类:

import javax.swing.JPanel;

public class FilePanel extends JPanel implements ActionListener {
private Reader reader;
private JTextField textField;
private JButton goButton;
private JLabel label;
private SpringLayout layout;

String fileLocation;
/**
 * Create the panel.
 */
public FilePanel() {

    textField = new JTextField();
    goButton = new JButton("Go!");
    label = new JLabel("Enter path to txt file.");
    layout = new SpringLayout();

    setupPanel();
} // end constructor 

private void setupPanel(){
    setBackground(Color.WHITE);
    setLayout(layout);
    setSize(400,200);

    // Adds label 
    add(label);
    label.setHorizontalAlignment(SwingConstants.CENTER);
    layout.putConstraint(SpringLayout.NORTH, label, 65, SpringLayout.NORTH, this);
    layout.putConstraint(SpringLayout.WEST, label, 125, SpringLayout.WEST, this);
    layout.putConstraint(SpringLayout.SOUTH, label, 81, SpringLayout.NORTH, this);
    layout.putConstraint(SpringLayout.EAST, label, -121, SpringLayout.EAST, this);

    // Adds text field
    add(textField);
    layout.putConstraint(SpringLayout.NORTH, textField, 6, SpringLayout.SOUTH, label);
    layout.putConstraint(SpringLayout.WEST, textField, 0, SpringLayout.WEST, label);
    layout.putConstraint(SpringLayout.EAST, textField, 0, SpringLayout.EAST, label);

    // Adds button
    add(goButton);
    goButton.addActionListener(this);
    layout.putConstraint(SpringLayout.NORTH, goButton, 6, SpringLayout.SOUTH, textField);
    layout.putConstraint(SpringLayout.WEST, goButton, 156, SpringLayout.WEST, this);
    layout.putConstraint(SpringLayout.SOUTH, goButton, 57, SpringLayout.SOUTH, textField);
    layout.putConstraint(SpringLayout.EAST, goButton, -151, SpringLayout.EAST, this);

}
// Action performed if the button 'Go!' button is pressed 
public void actionPerformed(ActionEvent e){
    try{
        fileLocation = textField.getText();

        textField.setText("");
        reader = new Reader(fileLocation);
        label.setText("File found!");

    }catch(FileNotFoundException e1){
        label.setText("File was not found");
    }
} // end actionPerformed



}

这是我的框架类:

import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;
public class FileWindow extends JFrame {

private FilePanel currentPanel;
private boolean close;
private JFrame frame;

public FileWindow(){

    currentPanel = new FilePanel();
    frame = new JFrame();

    setupFrame();
} // end constructor

private void setupFrame(){
    frame.setContentPane(currentPanel);
    frame.setSize(400,200);
    frame.setVisible(true);
    frame.setTitle("File Scanner");
    frame.setDefaultCloseOperation(EXIT_ON_CLOSE);

} // end setupFrame

}

所以基本上在面板类的try catch块中,我希望能够将框架设置为不可见,然后继续打开另一个框架。这可能吗?

1 个答案:

答案 0 :(得分:1)

使用Cardlayout

而不是使用多个JFrame(这是不好的做法)

以下是关于Cardlayouts如何运作的简单示例。

$(function () {
    Dropzone.options.testImage1 = {
        url: "test1.php",
        maxFiles : 10,
        paramName: "file1", 
        maxFilesize: 2,
    }
    Dropzone.options.testImage2 = {
        url: "test2.php",
        maxFiles : 20,
        paramName: "file2", 
        maxFilesize: 10,
    }
    Dropzone.options.testImage3 = {
        url: "test3.php",
        maxFiles : 30,
        paramName: "file3", 
        maxFilesize: 20,
    }
    Dropzone.options.testImage4 = {
        url: "test4.php",
        maxFiles : 40,
        paramName: "file4", 
        maxFilesize: 5,
    }       
});
相关问题