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

pls は私を助けてくれませんか、pls は私が変更しなければならないすべてを教えてください。非静的変数は静的コンテキストから参照できないというエラーが表示されます。よろしくお願いします。 すべての人に感謝し、レイアウトのアイデアについては「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 に答える