Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.BorderLayout;
- import java.awt.EventQueue;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.border.EmptyBorder;
- import javax.swing.filechooser.FileNameExtensionFilter;
- import javax.swing.JLabel;
- import javax.swing.SwingConstants;
- import javax.swing.JTextArea;
- import javax.swing.JButton;
- import javax.swing.JFileChooser;
- import java.awt.event.ActionListener;
- import java.io.BufferedReader;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.IOException;
- import java.awt.event.ActionEvent;
- public class Okno extends JFrame {
- private JPanel contentPane;
- /**
- * Launch the application.
- */
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- Okno frame = new Okno();
- frame.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- /**
- * Create the frame.
- */
- public Okno() {
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setBounds(100, 100, 450, 300);
- contentPane = new JPanel();
- contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
- contentPane.setLayout(new BorderLayout(0, 0));
- setContentPane(contentPane);
- JLabel lblNazwaPliku = new JLabel("Nazwa pliku");
- lblNazwaPliku.setHorizontalAlignment(SwingConstants.CENTER);
- contentPane.add(lblNazwaPliku, BorderLayout.NORTH);
- JTextArea textArea = new JTextArea();
- textArea.setEditable(false);
- contentPane.add(textArea, BorderLayout.CENTER);
- JPanel panel = new JPanel();
- contentPane.add(panel, BorderLayout.SOUTH);
- JButton btnOtworz = new JButton("Otworz");
- btnOtworz.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- JFileChooser chooser = new JFileChooser();
- FileNameExtensionFilter filter = new FileNameExtensionFilter("txt");
- chooser.setFileFilter(filter);
- int returnVal = chooser.showOpenDialog(Okno.this);
- if(returnVal == JFileChooser.APPROVE_OPTION) {
- lblNazwaPliku.setText(chooser.getSelectedFile().getName());
- try(BufferedReader br = new BufferedReader(new FileReader(chooser.getSelectedFile().getAbsolutePath()))) {
- StringBuilder sb = new StringBuilder();
- String line = br.readLine();
- while (line != null) {
- sb.append(line);
- sb.append(System.lineSeparator());
- line = br.readLine();
- }
- String everything = sb.toString();
- textArea.append(everything);
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- });
- panel.add(btnOtworz);
- JButton btnWyjdz = new JButton("Wyjdz");
- btnWyjdz.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- System.exit(0);
- }
- });
- panel.add(btnWyjdz);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement