Advertisement
Guest User

Untitled

a guest
Dec 14th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.87 KB | None | 0 0
  1. package ubhubhubhbhbnybbygbyg;
  2.  
  3. import java.io.*;
  4. import java.awt.*;
  5. import java.awt.event.*;
  6. import javax.swing.*;
  7. import javax.swing.SwingUtilities;
  8. import javax.swing.filechooser.*;
  9.  
  10. /*
  11. * FileChooserDemo.java uses these files:
  12. * images/Open16.gif
  13. * images/Save16.gif
  14. */
  15. public class dfdfdfdf extends JPanel implements ActionListener {
  16. static private final String newline = "\n";
  17. JButton openButton, saveButton;
  18. JTextArea log;
  19. JFileChooser fc;
  20.  
  21. public dfdfdfdf() {
  22. super(new BorderLayout());
  23.  
  24. //Create the log first, because the action listeners
  25. //need to refer to it.
  26. log = new JTextArea(5,20);
  27. log.setMargin(new Insets(5,5,5,5));
  28. log.setEditable(false);
  29. JScrollPane logScrollPane = new JScrollPane(log);
  30.  
  31. //Create a file chooser
  32. fc = new JFileChooser();
  33.  
  34. //Uncomment one of the following lines to try a different
  35. //file selection mode. The first allows just directories
  36. //to be selected (and, at least in the Java look and feel,
  37. //shown). The second allows both files and directories
  38. //to be selected. If you leave these lines commented out,
  39. //then the default mode (FILES_ONLY) will be used.
  40. //
  41. //fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
  42. //fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
  43.  
  44. //Create the open button. We use the image from the JLF
  45. //Graphics Repository (but we extracted it from the jar).
  46. openButton = new JButton("Open a File...", createImageIcon("images/Open16.gif"));
  47. openButton.addActionListener(this);
  48.  
  49. //Create the save button. We use the image from the JLF
  50. //Graphics Repository (but we extracted it from the jar).
  51. saveButton = new JButton("Save a File...", createImageIcon("images/Save16.gif"));
  52. saveButton.addActionListener(this);
  53.  
  54. //For layout purposes, put the buttons in a separate panel
  55. JPanel buttonPanel = new JPanel(); //use FlowLayout
  56. buttonPanel.add(openButton);
  57. buttonPanel.add(saveButton);
  58.  
  59. //Add the buttons and the log to this panel.
  60. add(buttonPanel, BorderLayout.PAGE_START);
  61. add(logScrollPane, BorderLayout.CENTER);
  62. }
  63.  
  64. public void actionPerformed(ActionEvent e) {
  65.  
  66. //Handle open button action.
  67. if (e.getSource() == openButton) {
  68. int returnVal = fc.showOpenDialog(dfdfdfdf.this);
  69.  
  70. if (returnVal == JFileChooser.APPROVE_OPTION) {
  71. File file = fc.getSelectedFile();
  72. //This is where a real application would open the file.
  73. log.append("Opening: " + file.getName() + "." + newline);
  74. } else {
  75. log.append("Open command cancelled by user." + newline);
  76. }
  77. log.setCaretPosition(log.getDocument().getLength());
  78.  
  79. //Handle save button action.
  80. } else if (e.getSource() == saveButton) {
  81. int returnVal = fc.showSaveDialog(dfdfdfdf.this);
  82. if (returnVal == JFileChooser.APPROVE_OPTION) {
  83. File file = fc.getSelectedFile();
  84. //This is where a real application would save the file.
  85. log.append("Saving: " + file.getName() + "." + newline);
  86. } else {
  87. log.append("Save command cancelled by user." + newline);
  88. }
  89. log.setCaretPosition(log.getDocument().getLength());
  90. }
  91. }
  92.  
  93. /** Returns an ImageIcon, or null if the path was invalid. */
  94. protected static ImageIcon createImageIcon(String path) {
  95. java.net.URL imgURL = dfdfdfdf.class.getResource(path);
  96. if (imgURL != null) {
  97. return new ImageIcon(imgURL);
  98. } else {
  99. System.err.println("Couldn't find file: " + path);
  100. return null;
  101. }
  102. }
  103.  
  104. /**
  105. * Create the GUI and show it. For thread safety,
  106. * this method should be invoked from the
  107. * event dispatch thread.
  108. */
  109. private static void createAndShowGUI() {
  110. //Create and set up the window.
  111. JFrame frame = new JFrame("FileChooserDemo");
  112. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  113.  
  114. //Add content to the window.
  115. frame.add(new dfdfdfdf());
  116.  
  117. //Display the window.
  118. frame.pack();
  119. frame.setVisible(true);
  120. }
  121.  
  122. public static void main(String[] args) {
  123. //Schedule a job for the event dispatch thread:
  124. //creating and showing this application's GUI.
  125. SwingUtilities.invokeLater(new Runnable() {
  126. public void run() {
  127. //Turn off metal's use of bold fonts
  128. UIManager.put("swing.boldMetal", Boolean.FALSE);
  129. createAndShowGUI();
  130. }
  131. });
  132. }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement