Advertisement
kalin729

FirstVisualJava

Jun 14th, 2017
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.10 KB | None | 0 0
  1. import java.awt.BorderLayout;
  2. import java.awt.EventQueue;
  3.  
  4. import javax.swing.JFrame;
  5. import javax.swing.JPanel;
  6. import javax.swing.border.EmptyBorder;
  7. import javax.swing.JTextArea;
  8. import javax.swing.JButton;
  9. import java.awt.event.ActionListener;
  10. import java.awt.event.ActionEvent;
  11. import javax.swing.JLabel;
  12. import java.awt.event.MouseAdapter;
  13. import java.awt.event.MouseEvent;
  14. import java.io.File;
  15. import java.io.FileNotFoundException;
  16. import java.io.FileReader;
  17. import java.io.FileWriter;
  18. import java.io.IOException;
  19. import java.util.Scanner;
  20.  
  21. public class Test1 extends JFrame {
  22.  
  23.     private JPanel contentPane;
  24.     private static File f = new File("ourbasefile.kln");
  25.     private static Test1 frame = null;
  26.     /**
  27.      * Launch the application.
  28.      */
  29.     public static void main(String[] args) {
  30.         EventQueue.invokeLater(new Runnable() {
  31.             public void run() {
  32.                 try {
  33.                     frame = new Test1();
  34.                     frame.setVisible(true);
  35.                 } catch (Exception e) {
  36.                     e.printStackTrace();
  37.                 }
  38.             }
  39.         });
  40.     }
  41.  
  42.     /**
  43.      * Create the frame.
  44.      */
  45.     /**
  46.      *
  47.      */
  48.     public Test1() {
  49.         setTitle("No name, sry");
  50.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  51.         setBounds(100, 100, 600, 450);
  52.         contentPane = new JPanel();
  53.         contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  54.         setContentPane(contentPane);
  55.         contentPane.setLayout(null);
  56.        
  57.         JTextArea textArea = new JTextArea();
  58.         textArea.setBounds(10, 23, 553, 92);
  59.         contentPane.add(textArea);
  60.        
  61.         JButton btnWriteToFile = new JButton("Write to file");
  62.         btnWriteToFile.addMouseListener(new MouseAdapter() {
  63.             @Override
  64.             public void mouseClicked(MouseEvent arg0) {
  65.                
  66.                 try {
  67.                     FileWriter fw = new FileWriter(f);
  68.                     fw.write(textArea.getText());
  69.                     fw.close();
  70.                 } catch (IOException ex) {
  71.                     // TODO Auto-generated catch block
  72.                     ex.printStackTrace();
  73.                 }
  74.             }
  75.         });
  76.         btnWriteToFile.addActionListener(new ActionListener() {
  77.             public void actionPerformed(ActionEvent e) {
  78.             }
  79.         });
  80.         btnWriteToFile.setBounds(274, 287, 89, 23);
  81.         contentPane.add(btnWriteToFile);
  82.        
  83.         JLabel lblNewLabel = new JLabel("");
  84.         lblNewLabel.setBounds(10, 171, 553, 92);
  85.         contentPane.add(lblNewLabel);
  86.  
  87.         JButton btnReadFromFile = new JButton("Read from file");
  88.         btnReadFromFile.addMouseListener(new MouseAdapter() {
  89.             @Override
  90.             public void mouseClicked(MouseEvent arg0) {
  91.                 try {
  92.                     Scanner s = new Scanner(f);
  93.                     String str = s.next();
  94.                     s.close();
  95.                     lblNewLabel.setText(str);
  96.                 } catch (FileNotFoundException e) {
  97.                     // TODO Auto-generated catch block
  98.                     e.printStackTrace();
  99.                 }
  100.             }
  101.         });
  102.         btnReadFromFile.setBounds(373, 287, 89, 23);
  103.         contentPane.add(btnReadFromFile);
  104.        
  105.         JButton btnExitProgram = new JButton("Cancel(r)");
  106.         btnExitProgram.addMouseListener(new MouseAdapter() {
  107.             @Override
  108.             public void mouseClicked(MouseEvent arg0) {
  109.                 Test1.frame.dispose();
  110.             }
  111.         });
  112.         btnExitProgram.addActionListener(new ActionListener() {
  113.             public void actionPerformed(ActionEvent arg0) {
  114.             }
  115.         });
  116.         btnExitProgram.setBounds(472, 287, 89, 23);
  117.         contentPane.add(btnExitProgram);
  118.        
  119.     }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement