import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test
{
public static void panel1()
{
JFrame frame=new JFrame("@b's metric calculator");
//Color b=new Color(0,150,255);
//JPanel Cr=new JPanel();
final JTextField textField = new JTextField(10);
//textField.setBackgound(0,255,220);
JButton button=new JButton("Enter Centimeter");
final JTextField textFiel= new JTextField("Your result will be here",20);
String[] list = {"cm-inch","cm-meter","cm-feet","inch-feet"};
/*The <string> added below is to avoid "unchecked or unsafe operations” warning in Java ,
if passing integer set it <Integer>*/
final JComboBox<String> list1 = new JComboBox<String>(list);
list1.setSelectedIndex(0);
JButton submit=new JButton("Submit");
submit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//textFiel.setText(textField.getText());
//following command converts textfild value to integer
int centi=Integer.parseInt(textField.getText());
//----------------------------------------------------
//double area = 3.14*radius*radius;
int cb = list1.getSelectedIndex();//to get the index of selected item(eg. 0,1,2..)
if(cb==0)
{
Double inch = centi/2.54;
//following command converts double to string
String out = Double.toString(inch);
//-----------------------------------------
textFiel.setText(out);
}
else if(cb==1)
{
Double meter = centi/100.00;
String out = Double.toString(meter);
textFiel.setText(out);
}
else if(cb==2)
{
Double feet = centi/30.48;
String out = Double.toString(feet);
textFiel.setText(out);
}
else
{
Double feet = centi/12.00;
String out = Double.toString(feet);
textFiel.setText(out);
}
}
});
//c.setBackground(b);
frame.add(new Cr());
Cr.add(button);
Cr.add(textField);
Cr.add(list1);
Cr.add(submit);
Cr.add(textFiel);
button.setPreferredSize(new Dimension(150,30));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,300);
frame.setVisible(true);
JOptionPane.showMessageDialog(frame,"Welcome \n enter centimeter in blank box\n Select your choice\n then press submit");
}
class Cr extends JPanel{
ImageIcon image = new ImageIcon("C:/Users/Public/Pictures/Sample Pictures/Desert.jpg");
public void paint( Graphics g ) {
Dimension d = getSize();
for( int x = 0; x < d.width; x += image.getIconWidth() )
for( int y = 0; y < d.height; y += image.getIconHeight() )
g.drawImage( image.getImage(), x, y, null, null );
super.paint(g);
}
}
public static void main(String[] args)
{
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run() {
panel1();
}
});
}
}
problem code is this section,it started when i tried to add image to panel.
class Cr extends JPanel{
ImageIcon image = new ImageIcon("C:/Users/Public/Pictures/Sample Pictures/Desert.jpg");
public void paint( Graphics g ) {
Dimension d = getSize();
for( int x = 0; x < d.width; x += image.getIconWidth() )
for( int y = 0; y < d.height; y += image.getIconHeight() )
g.drawImage( image.getImage(), x, y, null, null );
super.paint(g);
}
}
can you pls help me guys ,pls say which all i have to change. it says error non-static variable can't be referenced from a static context. thks in advance. thks to every one and 'vedant1811' for the layout idea ,'mKorbel' for overriding paint and 'nIcE cOw' for putting my code on center ,i used it now i made my program finally working. thks
My final code
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test extends JPanel{
public Test() {
setOpaque(false);
setLayout(new FlowLayout());
}
public static void main(String[] args) {
JFrame frame = new JFrame("@b's metric calculator");
final JTextField textField = new JTextField(10);
JButton button = new JButton("Enter Centimeter");
final JTextField textFiel = new JTextField("Your result will be here", 20);
String[] list = {"cm-inch", "cm-meter", "cm-feet", "inch-feet"};
/*The <string> added below is to avoid "unchecked or unsafe operations” warning in Java ,
if passing integer set it <Integer>*/
final JComboBox<String> list1 = new JComboBox<String>(list);
list1.setSelectedIndex(0);
JButton submit = new JButton("Submit");
Test Cr = new Test();
submit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//textFiel.setText(textField.getText());
//following command converts textfild value to integer
int centi = Integer.parseInt(textField.getText());
//----------------------------------------------------
//double area = 3.14*radius*radius;
int cb = list1.getSelectedIndex();//to get the index of selected item(eg. 0,1,2..)
if (cb == 0) {
Double inch = centi / 2.54;
//following command converts double to string
String out = Double.toString(inch);
//-----------------------------------------
textFiel.setText(out);
} else if (cb == 1) {
Double meter = centi / 100.00;
String out = Double.toString(meter);
textFiel.setText(out);
} else if (cb == 2) {
Double feet = centi / 30.48;
String out = Double.toString(feet);
textFiel.setText(out);
} else {
Double feet = centi / 12.00;
String out = Double.toString(feet);
textFiel.setText(out);
}
}
});
frame.add(Cr);
Cr.add(button);
Cr.add(textField);
Cr.add(list1);
Cr.add(submit);
Cr.add(textFiel);
button.setPreferredSize(new Dimension(150, 30));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
frame.setVisible(true);
JOptionPane.showMessageDialog(frame, "Welcome \n enter centimeter in blank box\n Select your choice\n then press submit");
}
public void paint(Graphics g)
{
// Loads the background image and stores in img object.
Image img = Toolkit.getDefaultToolkit().getImage("C:\\Users\\Administrator\\Documents\\JAVA\\My java programs\\hd_wall_11493.jpg");
// Draws the img to the BackgroundPanel.
g.drawImage(img,0,0,getSize().width,getSize().height,this);
super.paint(g);
}
}