1
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);
    }
    }      
4

2 回答 2

2

不想评论某事

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Test {

    public Test() {
        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);
        Cr cr = new Cr();
        cr.add(button);
        cr.add(textField);
        cr.add(list1);
        cr.add(submit);
        cr.add(textFiel);
        button.setPreferredSize(new Dimension(150, 30));
        frame.add(cr);
        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 {

        private static final long serialVersionUID = 1L;
        private ImageIcon image = new ImageIcon("C:/Users/Public/Pictures/Sample Pictures/Desert.jpg");

        @Override
        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() {
                Test test = new Test();
            }
        });
    }
}
于 2012-06-25T06:36:42.253 回答
2

我会解释你的错误:你的 test() 方法是一个静态方法。它不能直接访问非静态方法/变量(这具有完美的编程意义,如果您希望我详细说明,请发表评论)。静态方法和变量只能访问静态成员。您需要创建一个静态成员的对象,并且该对象可以访问其他非静态成员。

至于代码中所需的更正,mKorbel 的解决方案是完美的。但是,要将图像添加到 JPanel,最好将其“添加”为 JLabel,而不是绘制它。:

BufferedImage myPicture = ImageIO.read(new File("path-to-file"));
JLabel picLabel = new JLabel(new ImageIcon( myPicture ));
frame.add( picLabel );//see javadocs for JLabel for more info

编辑

但是你需要一张背景图片。以下是更正后的代码:

class Cr extends JPanel{
    //change here ('\\' is the escape sequence for '\') :
    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);
    }           
} 
于 2012-06-25T07:56:27.070 回答