0

I need to add a border around the Size label and the small, medium and large buttons so I thought I would add them into their own Panel and then add that panel to the mainPanel but when I do that they do not show up.

What am I doing wrong? :-/ Thanks guys!

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import javax.swing.*;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Dimension;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;


//Sarabeth Jaffe, Pizzaria Gui
public class Pizza extends JFrame implements ActionListener{

	private BufferedImage image;
	private int w,h;
	private JRadioButton small;
	private JRadioButton med;
	private JRadioButton large;
	private JCheckBox pep;
	private JCheckBox anch;
	private Calculations c;
	private JLabel tot;
	
	public Pizza(){

		c=new Calculations();
		setTitle("Pizzaria");
		setSize(495,290);
		setLayout(null);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		JLabel sizes=new JLabel("Sizes");
		sizes.setBounds(30, 30, 90, 40);
		sizes.setFont(new Font("Arial", Font.PLAIN, 20));
		sizes.setForeground(Color.RED);

		small=new JRadioButton("Small", true);
		small.setForeground(Color.YELLOW);
		small.setBounds(30, 70, 90, 40);
		med=new JRadioButton("Medium");
		med.setForeground(Color.YELLOW);
		med.setBounds(30, 110, 90, 40);
		large=new JRadioButton("Large");
		large.setForeground(Color.YELLOW);
		large.setBounds(30, 150, 90, 40);

		pep=new JCheckBox("Pepperoni");
		pep.setForeground(Color.YELLOW);
		pep.setBounds(360, 70, 100, 40);
		anch=new JCheckBox("Anchovies");
		anch.setForeground(Color.YELLOW);
		anch.setBounds(360, 110, 100, 40);

		tot=new JLabel("Total: ");
		tot.setBounds(150, 150, 200, 90);
		tot.setForeground(Color.YELLOW);
		tot.setFont(new Font("Arial", Font.PLAIN, 34));
		Panel mainPanel = new Panel("perfect.jpg");
		mainPanel.setLayout(null);

		pep.addActionListener(this);
		anch.addActionListener(this);

		ButtonGroup bg = new ButtonGroup();
		bg.add(small); bg.add(med); bg.add(large); 

		sizes.setOpaque(true);
		small.setOpaque(true);
		med.setOpaque(true);
		large.setOpaque(true);
		pep.setOpaque(true);
		anch.setOpaque(true);

		sizes.setBackground(new Color(0,0,0,64));
		small.setBackground(new Color(0,0,0,64));
		med.setBackground(new Color(0,0,0,64));
		large.setBackground(new Color(0,0,0,64));
		pep.setBackground(new Color(0,0,0,64));
		anch.setBackground(new Color(0,0,0,64));

		JPanel p=new JPanel();
		p.setLayout(new FlowLayout());
		
		add(sizes);
		add(small);
		add(med);
		add(large);
		
		p.setBorder(BorderFactory.createEtchedBorder());
		
		add(pep);
		add(anch);
		add(tot);
		add(p);
		add(mainPanel);

		setVisible(true);
		p.setVisible(true);
	}

	class Panel extends JPanel{

		public Panel(String fname){
			this.setSize(560,300);

			try {
				image = ImageIO.read(new File(fname));
				w = image.getWidth();
				h = image.getHeight();

			} catch (IOException ioe) {
				System.out.println("Could not read in the pic");
				System.exit(0);
			}

		}

		public Dimension getPreferredSize() {
			return new Dimension(w,h);
		}

		public void paintComponent(Graphics g){
			super.paintComponent(g);
			g.drawImage(image,0,0,this);
		}
	}

	public void actionPerformed(ActionEvent ae){

		if(ae.getActionCommand().equals("Pepperoni")){

			if(small.isSelected()){
				tot.setText(c.pep('s'));
			}

			else if(med.isSelected()){
				tot.setText(c.pep('m'));
			}

			else{
				tot.setText(c.pep('l'));
			}

		}
		
		else if(ae.getActionCommand().equals("Anchovies")){

			if(small.isSelected()){
				tot.setText(c.anch('s'));
			}

			else if(med.isSelected()){
				tot.setText(c.anch('m'));
			}

			else{
				tot.setText(c.anch('l'));
			}

		}
	}

	public static void main(String[] args){

		Pizza c = new Pizza();
	}
}
3
Contributors
2
Replies
4
Views
5 Years
Discussion Span
Last Post by Ezzaral
0

my view, very very bad,

is posible to put Image directly to JPanel, withOut method paintComponent (this Method works for JPanel, for Panel is correct paint )

please search for JPanel instead of Panel

sizes.setBorder(new LineBorder(Color.black, 1));

you have to start with http://download.oracle.com/javase/tutorial/uiswing/

and be sure that you'll read http://download.oracle.com/javase/tutorial/uiswing/layout/index.html

lots of examples for that on http://www.java2s.com/

if you'll a real question feel free to send it here

0

You set your frame layout to null, but never set the location of your panel. The following will make them show up within the panel (though it's not a layout you're going to want to keep once you see it)

JPanel p=new JPanel();
p.setLayout(new FlowLayout());
p.setBounds(20,20,100,130);
p.add(sizes);
p.add(small);
p.add(med);
p.add(large);

I'd recommend going though some of the layout tutorial linked above. Absolute positioning with null layout is usually not the best way to approach a design.

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.