У меня есть программа до тех пор, пока кадр всплывает с просьбой о местоположении файла. Если он не может найти файл, он говорит «Файл не найден», но если он находит файл, он говорит «найденный файл». Если он найдет файл, я также хочу закрыть рамку и открыть новый фрейм. Я не уверен, как это сделать. Вот мой код.Как закрыть рамку и открыть другой фрейм?
класс Группа:
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
}
Так в основном в блоке попытка поймать в классе панели, я хочу, чтобы иметь возможность установить рамки не и затем перейти к открытию другого кадра. Это возможно?