gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 10:52:16 25 4
gpt4 key购买 nike

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

面板类:

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 block 中,我希望能够将框架设置为不可见,然后继续打开另一个框架。这可能吗?

最佳答案

不要使用多个 JFrame,(这是不好的做法)使用 Cardlayout

这是一个有关卡片布局工作原理的简单示例。

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);
}
}

关于java - 如何关闭一个框架并打开另一个框架?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33794036/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com