Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

JAVA GUI assignment

Options
  • 11-11-2007 1:34pm
    #1
    Registered Users Posts: 7,155 ✭✭✭


    Hi,

    I've an assignment in java where i have to imitate the options of a washing machine. I have most of it up and runing but if you could help me out with the rest it would be great. Heres the problems i'm having:

    the layout of my GUI is truely awful!

    I have to display the current status of the wash, but i cant get the chosen variables for heat and speed to print out.

    lastly its mentioned in the question that i need to "Show the top-level design of the GUI, including any Panels and related Layout Manager objects that you propose to use" TBH i dont understand what this means!

    I'll print the code in the next comment so its easier to look at.

    Thanks in advance!


Comments

  • Registered Users Posts: 7,155 ✭✭✭witnessmenow


    import java.awt.FlowLayout;
    import java.awt.event.ItemListener;
    import java.awt.event.ItemEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import javax.swing.JRadioButton;
    import javax.swing.ButtonGroup;
    import javax.swing.JFrame;
    import javax.swing.JList;
    import javax.swing.JScrollPane;
    import javax.swing.JButton;
    import javax.swing.JOptionPane;
    
    
    public class washing extends JFrame
    {
    	private JButton onButton;
    	private JList tempJList;
    	private final String temp[] = { "90 oC", "60 oC", "40 oC", "30 oC"};
    	private JRadioButton speed600JRadioButton;
    	private JRadioButton speed900JRadioButton;
    	private JRadioButton speed122JRadioButton;
    	private ButtonGroup speed;
    	
    	
    	public washing()
    	{
    		super( "Washing Machine" );
    		
    		
    		
    		setLayout( new FlowLayout() );
    		
    		onButton = new JButton ( "ON" );
    		add( onButton );
    		
    		//Button Handler
    		ButtonHandler handler = new ButtonHandler();
    		onButton.addActionListener( handler );
    		
    	//Temperature section:
    		setLayout( new FlowLayout() );
    		
    		tempJList = new JList ( temp );
    		tempJList.setVisibleRowCount( 4 );
    		
    		add( tempJList );
    		
    	//Speed section:
    		speed600JRadioButton = new JRadioButton( "600 rpm");
    		speed900JRadioButton = new JRadioButton( "900 rpm");
    		speed122JRadioButton = new JRadioButton( "122 rpm");
    		add( speed600JRadioButton );
    		add( speed900JRadioButton );
    		add( speed122JRadioButton );
    		
    		speed = new ButtonGroup ();
    		speed.add(speed600JRadioButton);
    		speed.add(speed900JRadioButton);
    		speed.add(speed122JRadioButton);
    		
    		
    
    		
    	}
    	private class ButtonHandler implements ActionListener
    	{
    		public void actionPerformed( ActionEvent event )
    		{
    			JOptionPane.showMessageDialog( washing.this, String.format( "You pressed the on button "));
    			
    		}
    	}
    
    }
    
    


  • Moderators, Science, Health & Environment Moderators Posts: 10,079 Mod ✭✭✭✭marco_polo


    import java.awt.BorderLayout;
    import java.awt.Container;
    import java.awt.GridLayout;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    
    import javax.swing.ButtonGroup;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JList;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JRadioButton;
    import javax.swing.JScrollPane;
    
    public class Washing extends JFrame {
    	private JButton onButton;
    
    	private JList tempJList;
    
    	private final String temp[] = { "90 oC", "60 oC", "40 oC", "30 oC" };
    
    	private JRadioButton speed600JRadioButton;
    
    	private JRadioButton speed900JRadioButton;
    
    	private JRadioButton speed122JRadioButton;
    	private JRadioButton speed140JRadioButton;
    
    	private ButtonGroup speed;
    
    	public Washing() {
    		super("Washing Machine");
    
    		Container c = this.getContentPane();
    
    		addWindowListener(new WindowAdapter() {
    
    			public void windowClosing(WindowEvent e) {
    				setVisible(false);
    				dispose();
    			}
    
    		});
    
    		JPanel mainPanel = new JPanel();
    		mainPanel.setLayout(new BorderLayout());
    		c.add(mainPanel);
    
    		JPanel onoffPanel = new JPanel();
    		onButton = new JButton("ON");
    		
    		JButton offButton = new JButton("OFF");
    		onoffPanel.add(onButton);
    		onoffPanel.add(offButton);
    		mainPanel.add(onoffPanel, BorderLayout.NORTH);
    		
    		// Button Handler
    		ButtonHandler handler = new ButtonHandler();
    		onButton.addActionListener(handler);
    
    		// Temperature section:
    
    		tempJList = new JList(temp);
    		tempJList.setVisibleRowCount(4);
    		JScrollPane listScrollPane = new JScrollPane(tempJList);
    		mainPanel.add(listScrollPane, BorderLayout.CENTER);
    
    		// Speed section:
    		JPanel radioButtonPanel = new JPanel(new GridLayout(3,1));
    
    		speed600JRadioButton = new JRadioButton("600 rpm");
    		speed900JRadioButton = new JRadioButton("900 rpm");
    		speed122JRadioButton = new JRadioButton("122 rpm");
    		speed140JRadioButton = new JRadioButton("140 rpm");
    		
    		radioButtonPanel.add(speed600JRadioButton);
    		radioButtonPanel.add(speed900JRadioButton);
    		radioButtonPanel.add(speed122JRadioButton);
    		radioButtonPanel.add(speed140JRadioButton);
    		
    		mainPanel.add(radioButtonPanel, BorderLayout.SOUTH);
    
    		speed = new ButtonGroup();
    		speed.add(speed600JRadioButton);
    		speed.add(speed900JRadioButton);
    		speed.add(speed122JRadioButton);
    		speed.add(speed140JRadioButton);
    
    		setSize(250, 300);
    		this.setVisible(true);
    	}
    
    	private class ButtonHandler implements ActionListener {
    		public void actionPerformed(ActionEvent event) {
    			JOptionPane.showMessageDialog(Washing.this,
    					"You pressed the on button ");
    
    		}
    	}
    
    	public static void main(String args[]) {
    		Washing w = new Washing();
    
    	}
    
    }
    

    I made a few very quick changes to the code to illustrate a few points. Some java Layout Managers can be pretty complex though some of more basic ones are fairly straight forward.

    Firstly I changed washing to Washing (Classes should start with a capital letter :) )and added a main method to launch the GUI.

    First point is that a JFrame itself does not hold it components components, but has a Container object to do this job. You add components to the container object not to the JFrame directly.
    Container c = this.getContentPane();
    c.add(myComponent);
    

    Also some code to terminate the program when the window is closed.
    addWindowListener(new WindowAdapter() {
    			public void windowClosing(WindowEvent e) {
    				setVisible(false);
    				dispose();
    			}
    
    		});
    
    

    Third thing I have done is added a JList to a ScrollPane, this ensures that if the list gets too big for the screen, a scroll bar will appear in the window enabling all the elements of the list to be viewed. This is common practice when displaying a table of a list in a swing GUI.

    Finally I will quickly explain how the layout works.

    I created a JPanel which I have added to the JFrame container. This JPanel holds all the other components in the GUI. A LayoutManager tells a Container (such as a JPanel) how to layout its components on screen. A BorderLayout manager divides the screen into NORTH,SOUTH,EAST WEST and CENTER areas, which I used to layout the mainPanel.

    I added an extra JButton and a extra radio button to demonstrate the Flow layout and the Grid Layout. I created two panels one to display the buttons at the top (Added to the BorderLayout.NORTH), and one to display the radio buttons at the bottom(Added to the BorderLayout.SOUTH). The top panel uses a FlowLayout (the default LayouManager) which adds components from left to right and centers them by default.) The bottom panel uses a 2 X 2 Grid panel, this adds the components left to right also filling one row at a time.

    The ScrollPane I added to the BorderLayout.CENTER area of the main panel.

    Using these three layout managers should be enough to do any basic gui. Remember panels can contain other panels too, so you can put together pretty complicated GUIs with even simple layout managers.


  • Registered Users Posts: 5,379 ✭✭✭DublinDilbert


    You best not to place the components onto a JFrame directly...

    You should place them onto a JPanel first, each JPanel can have its own layout type (border / flow / grid )... then place the individual JPanels into the JFrame... Using this you can get quite complex layouts.


  • Registered Users Posts: 7,155 ✭✭✭witnessmenow


    All i can say is wow! Thank you for the detail you went into there! Just going through the code now. Can i just ask a quick question ? In your comment you mention the radio buttons using a 2x2 layout(and thats how it displays) but in your code you have
    JPanel radioButtonPanel = new JPanel(new GridLayout(3,1));
    

    sorry i just cant get my head around where 2x2 is defined?

    I was using a seperate class to display it but your way is much better

    If anyone is interested how bad the GUI looked initially here you go!


    EasySnap1.bmp

    thanks again for the help!


  • Moderators, Science, Health & Environment Moderators Posts: 10,079 Mod ✭✭✭✭marco_polo


    All i can say is wow! Thank you for the detail you went into there! Just going through the code now. Can i just ask a quick question ? In your comment you mention the radio buttons using a 2x2 layout(and thats how it displays) but in your code you have
    JPanel radioButtonPanel = new JPanel(new GridLayout(3,1));
    

    sorry i just cant get my head around where 2x2 is defined?

    I was using a seperate class to display it but your way is much better

    If anyone is interested how bad the GUI looked initially here you go!


    EasySnap1.bmp

    thanks again for the help!

    The 2X2 was just defined in my head it would seem, indeed that shoud be new JPanel radioButtonPanel=JPanel(new GridLayout(2,2));. :)


  • Advertisement
Advertisement