Referred to herein the following: Using Layout Managers Compound Borders Combo Boxes Multiple Images of the main frame of the program is as follows: // Create the phase selection and display panels.selectPanel = new JPanel (); displayPanel = new JPanel (); // Add various widgets to the sub panels.addWidgets (); // Create the main panel to contain the two sub panels.mainPanel = new JPanel (); mainPanel.setLayout (new BoxLayout (mainPanel, BoxLayout.PAGE_AXIS)); mainPanel.setBorder (BorderFactory .createemptyborder (5, 5, 5, 5)); // add the select and display panels to the main panel.mainpanel.add (selectpanel); mainpanel.add (displaypanel); it is also better to understand. Common layout managers include: BorderLayout, Boxlayout, FlowLayout, GridLayout, GridbagLayout and SpringLayout. Borders: selectPanel.setBorder (BorderFactory.createCompoundBorder (BorderFactory.createTitledBorder ( "Select Phase"), BorderFactory.createEmptyBorder (5,5,5,5))); drop-down box: JComboBox phaseChoices = null; ... // Create Combo Box with lunar phase choices.string [] Phases = {"New", "Waxing Crescent", "First Quarter", "Waying Gibbous", "Full", "WANING GIBBOUS", "Third Quarter", "WANING CRESCENT" }; phaseChoices = new JComboBox (phases); phaseChoices.setSelectedIndex (START_INDEX); drop down box event processing: phaseChoices.addActionListener (this); ... public void actionPerformed (ActionEvent event) {if ( "comboBoxChanged" .equals ( event.getActionCommand ())) {// Update the icon to display the new phase phaseIconLabel.setIcon (images [phaseChoices.getSelectedIndex ()]);}} multi-picture operation: final static int NUM_IMAGES = 8; final static int START_INDEX = 3;
Imageicon [] images = new imageicon [num_images]; ... // get the images and put the insto an array of imageicon.for (int i = 0; i Import javax.swing. *; Public class lunarphases implements an actionListener { Final Static Int Num_images = 8; Final static int start_index = 3; Imageicon [] images = new imageicon [num_images]; JPanel Mainpanel, SelectPanel, DisplayPanel JCOMBOBOX PHASECHOICES = NULL; Jlabel PhaseiconLabel = NULL; Public lunarphases () { // Create a status selection panel and display panel. SelectPanel = New jPanel (); DisplayPanel = New jPanel (); // Add the component to the sub-panel. AddWidgets (); // Creating the main panel to include the two panels above. Mainpanel = new jpanel (); Mainpanel.setLayout (New BoxLayout (Mainpanel, BoxLayout.page_AXIS); Mainpanel.SetBorder (BorderFactory.createemptyBorder (5, 5, 5, 5)); // Add two sub-panels to the main panel. Mainpanel.add (selectpanel); Mainpanel.Add (DisplayPanel); } / * * Get the picture and install the component. * / Private void addwidgets () { // Get the picture and put them in an imageicons array. For (int I = 0; i Images [i] = CreateImageicon ("/ images / image" ".jpg"); } / * * Create a Label used to display the picture and give him a border. * / Phaseiconlabel = new jlabel (); PhaseiconLabel.Sethorizontalalignment (jlabel.center); Phaseiconlabel.setVerticalAlignment (jlabel.center); Phaseiconlabel.setVerticalTextPosition (jlabel.center); Phaseiconlabel.SethorizontalTextPosition (jlabel.center); Phaseiconlabel.setBorder Borderfactory.createCompoundBorder BorderFactory.createloweredBevelBorder (), Borderfactory.createemptyBorder (5, 5, 5, 5)))); Phaseiconlabel.setBorder Borderfactory.createCompoundBorder Borderfactory.createemptyBorder (0, 0, 10, 0), PhaseiconLabel.getBorder ())); // Create a Combo Box that can select the moon. String [] Phases = { "New", "Waxing Crescent", "First quarter", "Waxing Gibbous", "Fulll", "WANING GIBBOUS", "Third quarter", "WANING CRESCENT"} Phasechoices = New JCOMBOBOX (PHASES); Phasechoices.setSelectedIndex (start_index); // Display the first pair of pictures. Phaseiconlabel.seticon (images [start_index]); Phaseiconlabel.setText (""); // Add the border to the selection panel. SelectPanel.setBorder Borderfactory.createCompoundBorder BorderFactory.createTitledborder ("SELECT PHASE"), Borderfactory.createemptyBorder (5, 5, 5, 5)))); // Add the border to the display panel. DisplayPanel.SetBorder Borderfactory.createCompoundBorder BorderFactory.createTitledborder ("Display Phase"), Borderfactory.createemptyBorder (5, 5, 5, 5)))); // Increase the COMBO BOX to the Select Panel and the image label to the display panel. DisplayPanel.Add (PhaseiconLabel); SelectPanel.Add (Phasechoices); // Add events to Combo Box. Phasechoices.AddActionListener (this); } Public void actionPerformed (ACTIONEvent Event) { IF ("ComboBoxChanged" .equals (event.getActioncommand ())) { // Update the icon to display a new state. Phaseiconlabel.seticon (images [phasechoices.getSelected "); } } / ** RETURNS An Imageicon, or Null IF The Path Was INVALID. * / protected static imageicon createImageicon (String path) { Java.net.url imageurl = lunarphases.class.getResource (path); IF (ImageURL == NULL) { System.err.Println ("Resource Not Found: PATH); Return NULL; } else {return new imageicon (imageURL); } } / ** * Create a graphical user interface to display, in order to be safe, use Event-Dispatching Thread call * / Private static void createandshowgui () { // make Sure WE Have Nice Window Decorations. Jframe.SetDefault (TRUE); // CREATE A NEW Instance of Lunarphases. Lunarphases Phases = New Lunarphases (); // Create and set up the window. JFrame LunarphaseSframe = New Jframe ("Lunar Phases"); LunarphaseSframe.SetDefaultCloseOperation (jframe.exit_on_close); LunarphaseSframe.SetContentpane (Phases.Mainpanel); // display the window. LunarphaseSframe.PACK (); LunarphaseSframe.setVisible (TRUE); } Public static void main (String [] args) { // Schedule a Job for the event-dispatch thread: // CREATING AND Showing this Application's GUI. Javax.swing.swingutilities.Invokelater (new runnable () { Public void run () { CreateandShowgui (); } }); } }