首页 > 解决方案 > 我的代码运行顺序有问题

问题描述

我在我的 GUI 下面发布了两个屏幕截图。屏幕截图 #1 的内容在两个之前打开(换句话说,用户可以在被带到“主页”之前选择一个文档(使用 mean gdp 和 mean pop 之类的按钮)。如何我可以编辑我的代码,以便向用户显示主页,并且他们只有在单击“选择 csv 文件”按钮后才能选择文档?我知道这与嵌套代码有关,但我会想再看看我所做的事情。谢谢

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JFileChooser;

import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.Scanner;
import java.awt.event.ActionEvent;
import java.awt.Color;
import javax.swing.JTextField;
import javax.swing.JLabel;

public class Frame1 {

    private JFrame frame;
    private JTextField txtCountryChosen;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        File file;
        Scanner fileIn;
        int response;
        JFileChooser chooser=new JFileChooser("");
        chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        response= chooser.showOpenDialog(null);
        
        if(response== JFileChooser.APPROVE_OPTION) {
            file = chooser.getSelectedFile();
            try {
                fileIn= new Scanner(file);
                if(file.isFile()) {
                    while(fileIn.hasNextLine()) {
                        String line=fileIn.nextLine();
                        System.out.println(line);
                    }
                }
                else {
                    System.out.println("That wasn't a file!");
                }
                fileIn.close();
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
        }
        
                if(result==JFileChooser.APPROVE_OPTION) {
                    File file=jFileChooser.getSelectedFile();
                    try {
                        FileOutputStream fileoutputStream=
                                new FileOutputStream(file);
                        
                        //fileoutputStream.flush();
                        //fileoutputStream.close();
                    } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                
                
            }
        });
        csv_select.setBounds(130, 62, 150, 25);
        frame.getContentPane().add(csv_select);
        
        JButton btnNewButton_1 = new JButton("Mean GDP");
        btnNewButton_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });
        btnNewButton_1.setBounds(0, 154, 97, 25);
        frame.getContentPane().add(btnNewButton_1);
        
        JButton btnNewButton_2 = new JButton("Mean GDP_per_capita");
        btnNewButton_2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });
        btnNewButton_2.setBounds(250, 154, 170, 25);
        frame.getContentPane().add(btnNewButton_2);
        
        JButton btnNewButton_3 = new JButton("Mean pop");
        btnNewButton_3.setBounds(109, 154, 120, 25);
        frame.getContentPane().add(btnNewButton_3);
        
        txtCountryChosen = new JTextField();
        txtCountryChosen.setBounds(12, 100, 116, 22);
        frame.getContentPane().add(txtCountryChosen);
        txtCountryChosen.setColumns(10);
        
        JButton btnNewButton_4 = new JButton("Select country");
        btnNewButton_4.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });
        btnNewButton_4.setBounds(270, 99, 150, 25);
        frame.getContentPane().add(btnNewButton_4);
        
        JButton btnNewButton_5 = new JButton("Print Results in Txt File");
        btnNewButton_5.setBounds(131, 201, 200, 20);
        frame.getContentPane().add(btnNewButton_5);
        
        JLabel lblNewLabel = new JLabel("Statistics Manager");
        lblNewLabel.setBounds(158, 13, 157, 36);
        frame.getContentPane().add(lblNewLabel);
    }
}```

标签: javacsvuser-interfacenested

解决方案


推荐阅读