Implementation of java swing constellation selector interface

Keywords: Java Windows

After understanding the use of various basic components and the handling of common events, this case will synthesize the text box. Button and Drop-down list Component to implement a constellation selector program. The program allows the user to be in the ____________ Drop-down list Choose one of your own constellations, if not in the list can also add constellations, you can also delete constellations. The implementation process is as follows.

(1) create a SampeDemo class and call its construction method in the main() method. The code is as follows:

package my;

public class SampeDemo
{

	public static void main(String[] args)
	{
		// TODO Auto-generated method stub
        new SampeDemo();//Call the constructor
	}

}

(2) Create a SampeDemo construction method to complete the creation of windows, add buttons and drop-down lists, and listen for the corresponding events. The code is as follows:

	private JPanel panel=new JPanel();
	private JComboBox cmb=new JComboBox();//Create JComboBox
	private JLabel label1=new JLabel("Add new constellations");
	private JLabel showInfo=new JLabel();//Used for displaying information
	private JTextField jtf=new JTextField(16);//Used for input information
	private JButton buttonADD=new JButton();//Newly added
	private JButton buttonDel=new JButton();//delete
	public SampeDemo()
	{
		JFrame frame=new JFrame("Choose your constellation");
		cmb.addItem("--Please choose--");
		cmb.addItem("Taurus");
		cmb.addItem("Gemini");
		cmb.addItem("Aries");
		panel.add(cmb);
		panel.add(label1);
		panel.add(jtf);
		panel.add(buttonADD);
		panel.add(buttonDel);
		frame.add(panel);
		cmb.addItemListener(new MyItemListener());//Dropdown List Events
		frame.setBounds(300,200,600,200);
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

The drop-down list created by the above program is cmb, and two options are added to it by calling its addItem() method. The program listens for selected events when selecting items in the drop-down list.

(3) Create MyItemListener class and implement the ItemListener interface to process the selected events. At this time, the selected items are displayed in the text box. The implementation code is as follows:

  //Monitor selected events
		class MyItemListener implements ItemListener
		{
		    @Override
		    public void itemStateChanged(ItemEvent e)
		    {
		    	// TODO Auto-generated method stub
		        String str=e.getItem().toString();
		        panel.add(showInfo);
		        showInfo.setText("The constellation you choose is:"+str);
		    }
		}

(4) To monitor the event processing of adding and deleting buttons, the code is as follows:

    // Listen for add and delete button events
	   buttonADD.addActionListener(new ActionListener()
		{
	        @Override
	        public void actionPerformed(ActionEvent e)
	        {
	            String command=e.getActionCommand();
	            //Add button processing
	            if(command.equals("Newly added"))
	            {
	                if(jtf.getText().length()!=0)
	                {
	                    cmb.addItem(jtf.getText());    //Add item
	                    panel.add(showInfo);
	                    showInfo.setText("Successful additions, new additions:"+jtf.getText());
	                }
	                else
	                {
	                    panel.add(showInfo);
	                    showInfo.setText("Please enter the constellation you want to add.");
	                }
	            }
	        }
		});
	     //Delete button processing
	    buttonDel.addActionListener(new ActionListener()
	    {
                
				@Override
				public void actionPerformed(ActionEvent e)
				{
					// TODO Auto-generated method stub
					String command=e.getActionCommand();
		            if(command.equals("delete"))
		            {
		                if(cmb.getSelectedIndex()!=-1)
		                {
		                    //Get the value of the item to be deleted first
		                    String strDel=cmb.getSelectedItem().toString();
		                    cmb.removeItem(strDel);    //Delete item
		                    panel.add(showInfo);
		                    showInfo.setText("Delete successfully, delete:"+strDel);
		                }
		                else
		                {
		                    panel.add(showInfo);
		                    showInfo.setText("Please select the constellation to delete");
		                }
		            }
					
				}
	        });  

The overall code is implemented as follows:

package my;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.sun.java.swing.plaf.motif.MotifButtonListener;

public class SampeDemo
{
	private JPanel panel=new JPanel();
	private JComboBox cmb=new JComboBox();//Create JComboBox
	private JLabel label1=new JLabel("Add new constellations");
	private JLabel showInfo=new JLabel();//Used for displaying information
	private JTextField jtf=new JTextField(16);//Used for input information
	private JButton buttonADD=new JButton("Newly added");//Newly added
	private JButton buttonDel=new JButton("delete");//delete
	public SampeDemo()
	{
		JFrame frame=new JFrame("Choose your constellation");
		cmb.addItem("--Please choose--");
		cmb.addItem("Taurus");
		cmb.addItem("Gemini");
		cmb.addItem("Aries");
		panel.add(cmb);
		panel.add(label1);
		panel.add(jtf);
		panel.add(buttonADD);
		panel.add(buttonDel);
		frame.add(panel);
		cmb.addItemListener(new MyItemListener());//Dropdown List Events
		frame.setBounds(300,200,600,200);
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	    //Monitor selected events
		class MyItemListener implements ItemListener
		{
		    @Override
		    public void itemStateChanged(ItemEvent e)
		    {
		    	// TODO Auto-generated method stub
		        String str=e.getItem().toString();
		        panel.add(showInfo);
		        showInfo.setText("The constellation you choose is:"+str);
		    }
		}
	    // Listen for add and delete button events
	   buttonADD.addActionListener(new ActionListener()
		{
	        @Override
	        public void actionPerformed(ActionEvent e)
	        {
	            String command=e.getActionCommand();
	            //Add button processing
	            if(command.equals("Newly added"))
	            {
	                if(jtf.getText().length()!=0)
	                {
	                    cmb.addItem(jtf.getText());    //Add item
	                    panel.add(showInfo);
	                    showInfo.setText("Successful additions, new additions:"+jtf.getText());
	                }
	                else
	                {
	                    panel.add(showInfo);
	                    showInfo.setText("Please enter the constellation you want to add.");
	                }
	            }
	        }
		});
	     //Delete button processing
	    buttonDel.addActionListener(new ActionListener()
	    {
                
				@Override
				public void actionPerformed(ActionEvent e)
				{
					// TODO Auto-generated method stub
					String command=e.getActionCommand();
		            if(command.equals("delete"))
		            {
		                if(cmb.getSelectedIndex()!=-1)
		                {
		                    //Get the value of the item to be deleted first
		                    String strDel=cmb.getSelectedItem().toString();
		                    cmb.removeItem(strDel);    //Delete item
		                    panel.add(showInfo);
		                    showInfo.setText("Delete successfully, delete:"+strDel);
		                }
		                else
		                {
		                    panel.add(showInfo);
		                    showInfo.setText("Please select the constellation to delete");
		                }
		            }
					
				}
	        });  
	}  
		
	public static void main(String[] args)
	{
		// TODO Auto-generated method stub
        new SampeDemo();//Call the constructor
	}

}

The operation procedure is as follows:

                              

 

Posted by msandersen on Sun, 13 Oct 2019 09:21:18 -0700