startframe.java

来自「简单的多字符java动画,方便理解Multi-threaded 的概念.」· Java 代码 · 共 186 行

JAVA
186
字号
import javax.swing.*;

import java.awt.event.*;
import java.awt.*;
import java.io.*;
import java.util.*;

public class StartFrame extends JFrame implements ActionListener
{

    private static final long serialVersionUID = 1L;

    String[] fileNameStrings = null;

    JComboBox fileNameList = null;

    JTextField subjtxt = null;

    JButton startButton = null;

    //Vector<Data> vTestSet = null;

    //Vector<Data> vTrials = null;

    LogFile log = null;

    String fileName = null, inputDir = "Input_Files/", outputName = "";

    public void actionPerformed(ActionEvent e)
    {

        if (e.getSource() == fileNameList)
        {
            fileName = (String) fileNameList.getSelectedItem();
            System.out.println(fileName);
        } else if (e.getSource() == subjtxt)
        {
            outputName = subjtxt.getText();
            if (outputName.length() == 0) /* Empty String */
            {
                /* ReInput the Text */
                JOptionPane.showMessageDialog(this,
                        "Please input a subject ID.");
                subjtxt.requestFocus();

            } else
            {
                /* Create A Output FileName */
                outputName = outputName + ".dat";
                System.out.println(outputName);
            }

        } else if (e.getSource() == startButton)
        {
            /* Read Input Files to create Test Trials */
            outputName = subjtxt.getText();
            if (outputName.length() == 0) /* Empty String */
            {
                JOptionPane.showMessageDialog(this,
                        "Please input a subject ID.");
                /* ReInput the Text */
                subjtxt.requestFocus();
            } else
            {
                /* Create A Output FileName */
                //outputName = outputName + ".dat";

                /* Start Reading a File */
                File inputFile = new File(inputDir + fileName);
          
                          
                SetReader
                    setProps = new SetReader(inputFile);
                
                /* Create Bouncing Canvas Frame */
                BouncingTest frame = new BouncingTest("Selective Looking Application",
                                                        outputName,setProps);
                
                
                /* Set Frame Look */
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setVisible(true);

            }
        }
        return;
    }

    public StartFrame(String sTitle)
    {
        super(sTitle);
        setStartGUI();
    }

    /* Setup Graphic User Interface */
    public void setStartGUI()
    {
        Container container = getContentPane();
        /* Set GridBagLayout */
        container.setLayout(new GridBagLayout());
        /* set JLabel for Subject ID */
        JLabel subjlbl = new JLabel("Subject ID"), fileNamelbl = new JLabel(
                "Test File Name");

        subjtxt = new JTextField(20);
        subjtxt.addActionListener(this);

        //Create the combo box, select item at index 0.
        fileNameStrings = readFiles(inputDir, ".txt");
        fileNameList = new JComboBox(fileNameStrings);
        fileNameList.setSelectedIndex(0);
        fileName = fileNameStrings[0];
        fileNameList.addActionListener(this);

        startButton = new JButton("Start Experiment");
        startButton.addActionListener(this);

        /* Add Component to the Frame */
        GridBagConstraints c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 0;
        container.add(subjlbl, c);

        c.gridx = 1;
        c.gridy = 0;
        container.add(subjtxt, c);

        c.gridx = 0;
        c.gridy = 1;
        container.add(fileNamelbl, c);

        c.gridx = 1;
        c.gridy = 1;
        container.add(fileNameList, c);

        c.weightx = 0.0;
        c.gridwidth = 2;
        c.gridx = 0;
        c.gridy = 2;
        container.add(startButton, c);

    }//End setStartGUI Method

    public void setupTest(File inputFile)
    {
       
    }//End setupTest Method

    public static String[] readFiles(String strDir, /* File Directory */
    String strEx) /* File Extension */
    {
        String dir = new String(strDir);
        File fDir = new File(dir);

        MyFileFilter myFileFilter = new MyFileFilter(strEx);

        String[] files = fDir.list(myFileFilter);

        return files;
    }//End readFiles

    public static void main(String[] args)
    {
        Toolkit tk = Toolkit.getDefaultToolkit();

        Dimension d = tk.getScreenSize();
        int 
            x = (int) d.getWidth() / 2 - 700 / 2, 
            y = (int) d.getHeight() / 2 - 500 / 2;
        
        StartFrame
            frame = new StartFrame("Selective Looking Application");
        
        /* Set Frame Look */
        frame.setLocation(x, y);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(500, 500);
        frame.pack();
        frame.setVisible(true);
    }//End main

}//End Class Definition


⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?