Function: you can click the button to add three guitar photos at a time and display them in the form according to the proportion.
The Code is as follows:
/*****/Import java. awt. borderLayout; import java. awt. dimension; import java. awt. image; import java. awt. event. actionEvent; import java. awt. event. actionListener; import java. io. file; import javax. swing. *;/*** @ author wangyubin * There is a button in the center of the Frame to open the file. * To be implemented: click the button to add multiple guitar photos at a time and display them in the form according to the proportion. Select three files (implemented) * to be improved: if not three files are selected, */public class GuitarChordsViewer {/*** @ param args * // defines the main form JFrame jf = new JFrame ("Guitar spectrum Browser "); // define the open file button JButton inputButton = new JButton ("Open File"); // define the label used to browse the image // JLabel label = new JLabel (); // define the File selector JFileChooser chooser = new JFileChooser (new File ("C: \ Users \ wangyubin \ Pictures \ GuitarChords \\")); // note that \ In the path must be expressed as \; otherwise \ will be considered as an escape symbol // The initialization method public void init () {inputButton. setPreferredSize (new Dimension (300,100); inputButton. addActionListener (new ActionListener () {@ Overridepublic void actionreceivmed (ActionEvent event) {// method stub chooser automatically generated by TODO. setMultiSelectionEnabled (true); // you can set int result = chooser for multiple objects. showDialog (jf, "Open guitar image file"); // click the button to display an int value in the open file dialog box, which is used to determine the user's choice // if the user selects the agree button, that is, open. The saved equivalent button detects that the event has changed again. label // when the user selects to open the event, if (result = JFileChooser. APPROVE_OPTION) {File [] files = chooser. getSelectedFiles (); // obtain the selected File and use the File [] array object to save String first = files [0]. getPath (); // obtain the path of the first file String second = files [1]. getPath (); // obtain the second file path String third = files [2]. getPath (); // obtain the path of the third object // display the specified image. Use the label icon to display the image to generate two labels ImageIcon icon1 = new ImageIcon (first ); icon1.setImage (icon1.getImage (). getScaledInstance (640,1080, Image. SCALE_DEFAULT); // The width of each image is JLabel label1 = new JLabel (); label1.setIcon (icon1); jf. add (label1, BorderLayout. WEST); // Add label1 to the WEST side of the Frame ImageIcon icon2 = new ImageIcon (second); icon2.setImage (icon2.getImage (). getScaledInstance (640,1080, Image. SCALE_DEFAULT); JLabel label2 = new JLabel (); label2.setIcon (icon2); jf. add (label2, BorderLayout. CENTER); // Add label2 to the middle of the Frame ImageIcon icon3 = new ImageIcon (third); icon3.setImage (icon3.getImage (). getScaledInstance (640,1080, Image. SCALE_DEFAULT); JLabel label3 = new JLabel (); label3.setIcon (icon3); jf. add (label3, BorderLayout. EAST); // Add label1 to the west side of the Frame. setVisible (false); // make the open file button invisible to jf. setExtendedState (JFrame. MAXIMIZED_BOTH); // make the Frame full screen }}); jf. add (inputButton, "South"); // place the button at the bottom of jf. pack (); // set the Frame to fit the size of the internal component jf. setVisible (true); jf. setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE);} public static void main (String [] args) {// method stub automatically generated by TODO new GuitarChordsViewer (). init ();}}
Key code:
1. Set the multiple-choice File chooser. setMultiSelectionEnabled (true), and use File [] files = chooser. getSelectedFiles () to obtain the array of File objects.
2. Use jf. add (label1, BorderLayout. WEST); add the label to the Frame and place it according to the layout parameter BorderLayout. WEST.
Defects (next improvement): 1. Only three guitar photos can be added; otherwise, an error is reported; 2. duplicate code is encapsulated; 3. The program is packaged into an EXE Executable File Using exe4j.
Program: