2015-11-19 3 views
0

У меня есть программа до тех пор, пока кадр всплывает с просьбой о местоположении файла. Если он не может найти файл, он говорит «Файл не найден», но если он находит файл, он говорит «найденный файл». Если он найдет файл, я также хочу закрыть рамку и открыть новый фрейм. Я не уверен, как это сделать. Вот мой код.Как закрыть рамку и открыть другой фрейм?

класс Группа:

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 

} 

Так в основном в блоке попытка поймать в классе панели, я хочу, чтобы иметь возможность установить рамки не и затем перейти к открытию другого кадра. Это возможно?

ответ

1

Вместо использования нескольких JFrames (Это плохая практика) использовать Cardlayout

Вот простой пример того, как Cardlayouts работы.

package main.frames; 

import javax.swing.*; 
import java.awt.*; 

public class MainFrame extends JFrame 
{ 
static JPanel homeContainer; 
static JPanel homePanel; 
static JPanel otherPanel; 
static CardLayout cl; 

public MainFrame() 
{ 
    JButton showOtherPanelBtn = new JButton("Show Other Panel"); 
    JButton backToHomeBtn = new JButton("Show Home Panel"); 

    cl = new CardLayout(5, 5); 
    homeContainer = new JPanel(cl); 
    homeContainer.setBackground(Color.black); 

    homePanel = new JPanel(); 
    homePanel.setBackground(Color.blue); 
    homePanel.add(showOtherPanelBtn); 

    homeContainer.add(homePanel, "Home"); 

    otherPanel = new JPanel(); 
    otherPanel.setBackground(Color.green); 
    otherPanel.add(backToHomeBtn); 

    homeContainer.add(otherPanel, "Other Panel"); 

    showOtherPanelBtn.addActionListener(e -> cl.show(homeContainer, "Other Panel")); 
    backToHomeBtn.addActionListener(e -> cl.show(homeContainer, "Home")); 

    add(homeContainer); 
    cl.show(homeContainer, "Home"); 
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
    setLocationRelativeTo(null); 
    setExtendedState(JFrame.MAXIMIZED_BOTH); 
    setTitle("CardLayout Example"); 
    pack(); 
    setVisible(true); 
} 

    public static void main(String[] args) 
    { 
     SwingUtilities.invokeLater(MainFrame::new); 
    } 
} 
Смежные вопросы