Results 1 to 7 of 7
Like Tree2Likes
  • 1 Post By Norm
  • 1 Post By Fubarable

Thread: Basic Swing-programming Questions: pop up and adding values

  1. #1
    marmelad is offline Member
    Join Date
    May 2012
    Posts
    3
    Rep Power
    0

    Question Basic Swing-programming Questions: pop up and adding values

    Hi!

    First post on the forum, from Sweden also :)

    However, I really need your help guys. I'm supposed to make a program where you can order burgers (very basic) by clicking checkboxes and clicking an "order"-button. It would be perfect if every box had a value, so you could see a total price of the food :)

    I have just two problems. My program worked fairly ok recently but now, no components pops up on the screen which I think is a very basic problem. Also, I don't know how to add values to the components (prices).

    The program is built in a total of 3 panels where each panel is a "page" with either burgers or drinks.
    Code for the "homepage"

    PHP Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class Burgerkingorder extends JFrame {
    	
    		private JLabel label;
    		private JPanel bakgrund, p1, p2;
    		private CardLayout cl;
    		
    	public Burgerkingorder() {
    			setLayout(new FlowLayout());
    		
    		//konstruktor	
    		label = new JLabel("Have it your way!");
    		add(label);
    		
    	}
    		public JPanel createJPanels(){
    			bakgrund = new JPanel();
    			p1 = new JPanel();				//ändrade BPanel1 till JPanel.
    			p1.setBackground(Color.blue);
    			p2 = new JPanel();				//ändrade BPanel2 till JPanel.
    			p2.setBackground(Color.blue);
    			cl = new CardLayout();
    			bakgrund.setLayout(cl);
    			bakgrund.add(p1,"FF1");
    			bakgrund.add(p2, "xxx");
    			return bakgrund;
    	}
    	
    		//nedan följer fönstrets egenskaper
    		public static void main (String args[]){
    			
    		Burgerkingorder gui = new Burgerkingorder();
    		gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		gui.setSize(202,270);
    		gui.setVisible(true);
    		gui.setTitle("Välkommen till Burgerking!");
    		
    		/*MenuBar = själva raden, Menu = kategorierna (ex. arkiv, redigera), 
    		 MenuItem = underkategorierna (ex. importera, avsluta)*/
    		 
    		JMenuBar meny = new JMenuBar();
    		gui.setJMenuBar(meny);
    		
    		JMenu arkiv = new JMenu("Arkiv");
    		meny.add(arkiv);
    		JMenuItem burgare = new JMenuItem("Burgare");
    		arkiv.add(burgare);
    		
    		JMenuItem drycker = new JMenuItem("Drycker");
    		arkiv.add(drycker);
    
    		JMenu help = new JMenu("Hjälp");
    		meny.add(help);
    		JMenuItem about = new JMenuItem("Om...");
    		help.add(about);
    		
    		}
    		class listener implements ActionListener{
    			public void actionPerformed (ActionEvent ae){
    				if(ae.getActionCommand().equals("Burgare")){
    					System.out.println("Burgare");
    					cl.show(bakgrund, "Burgare");
    				}
    				else if(ae.getActionCommand().equals("Drycker")){
    					System.out.println("Drycker");
    					cl.show(bakgrund, "Drycker");
    				}
    			}
    			
    		}
    		
    	}


    Code for the first page/panel
    PHP Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class BPanel1 extends JPanel {
    	
    	private JLabel yourway, klicka, kostnad;
    	private JCheckBox whopper, bigking, angus, cheeseburger;
    	private JComboBox whopperantal, bigkingantal, angusantal, cheeseburgerantal;
    	private JButton b1 = new JButton("Prisfakta");
    	private JTextField pris;
    	private JButton b2 = new JButton("Beställ");
    	private JPanel panel = new JPanel();
    	private toDoListener toDo;
    	
    	public BPanel1(){
    		
    		whopper = new JCheckBox("Whopper");
    		add(whopper);
    		
    		String[] whopperantal = { "1", "2", "3" };
    		JComboBox whoppercombo = new JComboBox(whopperantal);
    		add(whoppercombo);
    		
    		bigking = new JCheckBox("Big King XXL");
    		add(bigking);
    		
    		String[] bigkingantal = { "1", "2", "3" };
    		JComboBox bigkingcombo = new JComboBox(bigkingantal);
    		add(bigkingcombo);
    		
    		angus = new JCheckBox("The Angus");
    		add(angus);
    		
    		String[] angusantal = { "1", "2", "3" };
    		JComboBox anguscombo = new JComboBox(angusantal);
    		add(anguscombo);
    		
    		cheeseburger = new JCheckBox("Cheeseburger");
    		add(cheeseburger);
    		
    		String[] cheeseburgerantal = { "1", "2", "3", "4", "5" };
    		JComboBox cheeseburgercombo = new JComboBox(cheeseburgerantal);
    		add(cheeseburgercombo);
    		
    		klicka = new JLabel("Klicka för pris");
    		add(klicka);
    		
    		b1 = new JButton("Kostnad");
    		add(b1);
    		b1.addActionListener(new toDoListener());
    		
    		kostnad = new JLabel("________");
    		add(kostnad);
    		
    		b2 = new JButton("Beställ!");
    		add(b2);
    		
    		add(panel);
    		panel.setLayout(new GridLayout(2, 5));
    	}
    		
    
    	
    	public class toDoListener implements ActionListener {
    			public void actionPerformed(ActionEvent e) {
    				kostnad.setText("50 SEK");
    		}
    	}
    		
    }


    Code for the 2nd panel, the drinks
    PHP Code:
    import javax.swing.*;
    
    import java.awt.*;
    import java.awt.event.*;
    
    
    public class BPanel2 extends JPanel{
    	
    	private JLabel yourway, klicka, kostnad;
    	private JCheckBox cocacola, fanta, sprite, colalight;
    	private JComboBox colaantal, fantaantal, spriteantal, colalightantal;
    	private JButton b1 = new JButton("Prisfakta");
    	private JTextField pris;
    	private JButton b2 = new JButton("Beställ");
    	private JPanel panel = new JPanel();
    	private toDoListener toDo;
    	
    	public BPanel2(){
    		
    		cocacola = new JCheckBox("Coca-Cola");
    		add(cocacola);
    		
    		String[] colaantal = { "1", "2", "3" };
    		JComboBox colacombo = new JComboBox(colaantal);
    		add(colacombo);
    		
    		fanta = new JCheckBox("Fanta Orange");
    		add(fanta);
    		
    		String[] fantaantal = { "1", "2", "3" };
    		JComboBox fantacombo = new JComboBox(fantaantal);
    		add(fantacombo);
    		
    		sprite = new JCheckBox("Sprite");
    		add(sprite);
    		
    		String[] spriteantal = { "1", "2", "3" };
    		JComboBox spritecombo = new JComboBox(spriteantal);
    		add(spritecombo);
    		
    		colalight = new JCheckBox("Cola Light");
    		add(colalight);
    		
    		String[] colalightantal = { "1", "2", "3", "4", "5" };
    		JComboBox colalightcombo = new JComboBox(colalightantal);
    		add(colalightcombo);
    		
    		klicka = new JLabel("Klicka för pris");
    		add(klicka);
    		
    		b1 = new JButton("Kostnad");
    		add(b1);
    		b1.addActionListener(new toDoListener());
    		
    		kostnad = new JLabel("________");
    		add(kostnad);
    		
    		b2 = new JButton("Beställ!");
    		add(b2);
    		
    		add(panel);
    		panel.setLayout(new GridLayout(2, 5));
    	}
    		
    
    	
    	public class toDoListener implements ActionListener {
    			public void actionPerformed(ActionEvent e) {
    				kostnad.setText("35 SEK");
    		}
    	}
    		
    }
    Thanks in advance! :)
    Last edited by Fubarable; 05-14-2012 at 01:55 AM. Reason: "Hurry" bit removed

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,319
    Rep Power
    25

    Default Re: Need help! Basic Swing-programming, hurry!

    no components pops up on the screen
    Can you explain what happens when you execute the program? When is the component supposed to pop up?
    What kind of a component is it? What does "pop up" mean?
    Fubarable likes this.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Basic Swing-programming Questions: pop up and adding values

    I've edited your question and removed all bits referring to "Hurry" in your post heading. Please, we're volunteers. The hurry is yours not ours, and your question is no more important than any other question here.
    DarrylBurke likes this.

  4. #4
    marmelad is offline Member
    Join Date
    May 2012
    Posts
    3
    Rep Power
    0

    Default Re: Need help! Basic Swing-programming, hurry!

    Quote Originally Posted by Norm View Post
    Can you explain what happens when you execute the program? When is the component supposed to pop up?
    What kind of a component is it? What does "pop up" mean?
    When I execute the program, a window shows up. The window is empty, but it is supposed to be filled with checkboxes, comboboxes and buttons. The components are supposed to show up from the start of the program to the exit. With pop up, I mean that they are supposed to be visible, however, they aren't right now. :)

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,319
    Rep Power
    25

    Default Re: Basic Swing-programming Questions: pop up and adding values

    Where do you add the components you want to see to the GUI? I only see one JLabel being added.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    marmelad is offline Member
    Join Date
    May 2012
    Posts
    3
    Rep Power
    0

    Default Re: Basic Swing-programming Questions: pop up and adding values

    Quote Originally Posted by Norm View Post
    Where do you add the components you want to see to the GUI? I only see one JLabel being added.
    In the so called panels (BPanel1), is that wrong? Should I add the components somewhere else, where the label is located?

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,319
    Rep Power
    25

    Default Re: Basic Swing-programming Questions: pop up and adding values

    You need to add the components to the GUI container like the JFrame that is being shown.

    What do you see when you execute the code? I see one line of text from a JLabel.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Basic Java programming HELP!
    By Javreg in forum New To Java
    Replies: 2
    Last Post: 05-06-2012, 07:12 AM
  2. need help with basic programming assignment (with arrays)
    By magictricks1020 in forum New To Java
    Replies: 11
    Last Post: 05-03-2011, 02:19 AM
  3. help need basic java swing tuitorial
    By azzaiel in forum New To Java
    Replies: 11
    Last Post: 03-26-2009, 11:22 PM
  4. Framework for swing programming
    By makpandian in forum AWT / Swing
    Replies: 2
    Last Post: 02-28-2009, 08:02 AM
  5. basic text editor in Swing
    By Java.child in forum AWT / Swing
    Replies: 6
    Last Post: 12-30-2008, 11:09 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •