Need help just inserting an image in a JPanel
I want to know the quickest easiest way to insert an image in a JPanel
Here is my code so far
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.DecimalFormat;
//Panel containing components
public class CalculatePanel extends JPanel{
//1) Declare components to be used
private JLabel inputLabel, resultLabel;
private JTextField xvar;
private int x; private double ans;
private JButton calcButton;
//2) Sets up the GUI
public CalculatePanel(){
//2a) Create two labels
inputLabel = new JLabel ("Enter posotive value of x:");
resultLabel = new JLabel ("Value of expression");
//2b) Create text field
xvar = new JTextField (10);
//2c) Add action listener to the text field
xvar.addActionListener (new TextFieldListener());
//2d) Add components to the panel
add (inputLabel);
add (xvar);
add (resultLabel);
//2e) set buttons and listeners
calcButton = new JButton("Calculate Expression");
calcButton.addActionListener(new CalcButtonListener());
setPreferredSize (new Dimension(300, 450));
setBackground (Color.green);
add(calcButton);
}
//3) Create the action listener for the text field
private class TextFieldListener implements ActionListener {
//--------------------------------------------------------
// Performs the conversion when the enter key is pressed in the
text field
//--------------------------------------------------------
public void actionPerformed (ActionEvent event){
String text = xvar.getText();
x = Integer.parseInt (text);
double ans = Math.sqrt (Math.abs(3 * (Math.pow((x), 5)) -
(12 * (Math.pow((x), 4))) - (9 * (Math.pow((x), 2))) + (2 *
x)));
DecimalFormat df = new DecimalFormat ("0.###");
String result = "Value of expression = " + df.format(ans);
resultLabel.setText (result);
}
}
private class CalcButtonListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
String text = xvar.getText();
x = Integer.parseInt (text);
double ans = Math.sqrt (Math.abs(3 * (Math.pow((x), 5)) -
(12 * (Math.pow((x), 4))) - (9 * (Math.pow((x), 2))) + (2 * x)));
String result = "Value of expression = " + ans;
DecimalFormat df = new DecimalFormat ("0.###");
String result1 = "Value of expression = " +
df.format(ans);
resultLabel.setText (result1);
}
}
}