problems with JPanel and JFrame
I seem to be doing something wrong, because my code compiles but all I get is a blank box, I've been going around in circles trying to figure out what I'm doing wrong.
This is a program that accepts a value from the user and uses it as the radius to draw a circle, in addition to displaying varius stats about the circle that is being shown.
Any help greatly appreciated.
Code:
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.SwingConstants;
public class circle extends JPanel
{
public double radius;
public double diameter;
public double circum;
public double area;
public int diam;
public JTextField input;
public JLabel display;
public void input()
{
//input dialog box sits at top of screen
input = new JTextField( 20 );
input.setText( "Please input radius" );
input.selectAll();
add( input );
//JLabel displays dimensions of drawn circle
display = new JLabel( "circumference = " + circum +
"radius = " + radius +
"diameter = " + diameter +
"area = " + area, SwingConstants.BOTTOM );
//a listener for input
input.addActionListener(
new ActionListener()
{
public void actionPerformed( ActionEvent event )
{
if( event.getSource() == input )
{
radius = Double.parseDouble( event.getActionCommand() );
diameter = radius*2;
circum = radius*3.14159*2;
area = 3.14159*(Math.pow( radius, 2 ));
diam = (int)diameter;
}
}
}
);
}
public void paintComponent( Graphics g )
{
super.paintComponent( g );
g.setColor( Color.BLUE );
g.fillOval( 50, 50, diam, diam );
}
}
Code:
import javax.swing.JFrame;
public class circleFrame
{
public static void main( String args[] )
{
JFrame frame = new JFrame( "All you wanted to know about a circle" );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
circle oval = new circle();
frame.add( oval );
frame.setSize( 750, 750 );
frame.setVisible( true );
}
}