Newer
Older
TraceMaker / TraceMarker / src / Main.java
daiki1020 on 28 Mar 2018 3 KB CVSから持ってきた。
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

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


public class Main {

	public static void main(String[] args) {
		final ArrayList<Long> marks = new ArrayList<Long>();
		final JFrame frame = new JFrame();
		frame.setResizable(false);
		frame.setTitle("Trace Marker");
		frame.setSize(300, 215);
		frame.setLayout(null);
		
		JButton markButton = new JButton("Mark");
		markButton.setBounds(25, 20, 100, 25);
		
		final JLabel time = new JLabel();
		time.setBounds(150, 20, 125, 25);
		
		JButton clearButton = new JButton("Clear");
		clearButton.setBounds(25, 60, 100, 25);
		
		JButton saveButton = new JButton("Save As...");
		saveButton.setBounds(25, 120, 100, 25);
		
		JButton okButton = new JButton("OK");
		okButton.setBounds(150, 120, 100, 25);
	
		frame.add(time);
		frame.add(markButton);
		frame.add(clearButton);
		frame.add(saveButton);
		frame.add(okButton);
		
		markButton.addMouseListener(new MouseListener() {
			@Override
			public void mouseClicked(MouseEvent e) {
				Long curTime = System.nanoTime();
				time.setText(Long.toString(curTime));
				marks.add(curTime);
			}
			@Override
			public void mouseEntered(MouseEvent e) {
			}
			@Override
			public void mouseExited(MouseEvent e) {
			}
			@Override
			public void mousePressed(MouseEvent e) {
			}
			@Override
			public void mouseReleased(MouseEvent e) {
			}			
		});
		
		clearButton.addMouseListener(new MouseListener() {
			@Override
			public void mouseClicked(MouseEvent e) {
				time.setText("");
				marks.clear();;
			}
			@Override
			public void mouseEntered(MouseEvent e) {
			}
			@Override
			public void mouseExited(MouseEvent e) {
			}
			@Override
			public void mousePressed(MouseEvent e) {
			}
			@Override
			public void mouseReleased(MouseEvent e) {
			}			
		});
		
		saveButton.addMouseListener(new MouseListener() {
			@Override
			public void mouseClicked(MouseEvent e) {
			    JFileChooser filechooser = new JFileChooser();

			    int selected = filechooser.showSaveDialog(frame);
			    if (selected == JFileChooser.APPROVE_OPTION){
			    	File file = filechooser.getSelectedFile();
					try {
						BufferedWriter br = new BufferedWriter(new FileWriter(file));
						for (int n = 0; n < marks.size(); n++) {
							br.write(Long.toString(marks.get(n)));
							br.newLine();
						}
						br.close();
					} catch (IOException ex) {
					}
			    }			
			}
			@Override
			public void mouseEntered(MouseEvent e) {
			}
			@Override
			public void mouseExited(MouseEvent e) {
			}
			@Override
			public void mousePressed(MouseEvent e) {
			}
			@Override
			public void mouseReleased(MouseEvent e) {
			}			
		});
		
		okButton.addMouseListener(new MouseListener() {
			@Override
			public void mouseClicked(MouseEvent e) {
				frame.dispose();
			}
			@Override
			public void mouseEntered(MouseEvent e) {
			}
			@Override
			public void mouseExited(MouseEvent e) {
			}
			@Override
			public void mousePressed(MouseEvent e) {
			}
			@Override
			public void mouseReleased(MouseEvent e) {
			}
		});
		
		frame.setVisible(true);
	}

}