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();
}
});
}
}
问题代码是这一部分,它是在我尝试将图像添加到面板时开始的。
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);
}
}
你能帮帮我吗,请说出我必须改变的一切。它说错误非静态变量不能从静态上下文中引用。提前谢谢。 感谢每个人,'vedant1811' 用于布局创意,'mKorbel' 用于覆盖油漆和 'nIcE cOw' 用于将我的代码放在中心,我现在使用它,我的程序终于可以工作了。谢谢
我的最终代码
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);
}
}