2

したがって、私の最後の投稿Java Button Widthに従って、いくつかの画像を追加して背景色を設定しようとしています。私はそれをするたびに、いくつかのことを試しました。それはいつも私にエラーを与えます。

私はもう試した

setBackground(args);

img = addImage("image.png");

彼らは私のために働きません。誰か手を貸してくれませんか?

わかりました、ディシャの投稿を試しました。そして、アプレットは黒ではなく同じ色のままです

http://pastebin.com/iijj7fSr

4

3 に答える 3

5

最初に、Javaの命名規則を学び、それらに固執してください。

JFrameに背景色を追加したので、に背景色を提供するためJPanelCENTER。したがって、次のように記述して1つの背景色を取得することはできません。

interfaceFrame.setBackground(Color.black);

次に、の不透明なプロパティJPanel をtrueに設定し、同じように1つの背景色を設定する必要があります。

setOpaque(true);
setBackground(Color.BLUE);

MenuPane クラスのコンストラクター内。

これがあなたの修正されたコードです:

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

public class Gmine {
        JFrame interfaceFrame;
        JButton singleplayerButton, multiplayerButton, optionsButton, quitButton;


        public Gmine() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException ex) {
                    } catch (InstantiationException ex) {
                    } catch (IllegalAccessException ex) {
                    } catch (UnsupportedLookAndFeelException ex) {
                    }

                    interfaceFrame = new JFrame("G-Mine");
                    interfaceFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    interfaceFrame.setLayout(new BorderLayout());
                    interfaceFrame.setSize(800,500);
                    //interfaceFrame.setBackground(Color.black);
                    interfaceFrame.add(new MenuPane());
                    interfaceFrame.setLocationRelativeTo(null);
                    interfaceFrame.setVisible(true);
                }
            });
        }

        public class MenuPane extends JPanel {

            public MenuPane() {
                setLayout(new GridBagLayout());

                setOpaque(true);
                setBackground(Color.BLUE);

                singleplayerButton = new JButton("SinglePLayer");
                multiplayerButton = new JButton("MultiPlayer");
                optionsButton = new JButton("Options");
                quitButton = new JButton("Quit");

                GridBagConstraints gbc = new GridBagConstraints();
                gbc.gridx = 0;
                gbc.gridy = 0;
                gbc.fill = GridBagConstraints.HORIZONTAL;
                gbc.ipadx = 20;
                gbc.ipady = 20;

                add(singleplayerButton, gbc);
                gbc.gridy++;
                add(multiplayerButton, gbc);
                gbc.gridy++;
                add(optionsButton, gbc);
                gbc.gridy++;
                add(quitButton, gbc);
            }
        }
        public static void main(String[] args) {
            new Gmine();
        }
}

これで、プロジェクトに画像を追加するために、Javaでプロジェクトに画像を追加する方法についてこの回答を見ることができます。また、次のようなこの小さなサンプルコードからもヘルプを得ることができます。

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;

public class PaintingExample
{
    private CustomPanel contentPane;
    private JTextField userField;
    private JPasswordField passField;
    private JButton loginButton;

    private void displayGUI()
    {
        JFrame frame = new JFrame("Painting Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        contentPane = new CustomPanel();        

        frame.setContentPane(contentPane);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new PaintingExample().displayGUI();
            }
        });
    }
}

class CustomPanel extends JPanel
{
    private BufferedImage image;

    public CustomPanel()
    {
        setOpaque(true);
        setBorder(BorderFactory.createLineBorder(Color.BLACK, 5));
        try
        {
            /*
             * Since Images are Application Resources,
             * it's always best to access them in the
             * form of a URL, instead of File, as you are doing.
             * Uncomment this below line and watch this answer
             * of mine, as to HOW TO ADD IMAGES TO THE PROJECT
             * https://stackoverflow.com/a/9866659/1057230
             * In order to access images with getClass().getResource(path)
             * here your Directory structure has to be like this
             *                 Project
             *                    |
             *         ------------------------
             *         |                      |
             *        bin                    src
             *         |                      |
             *     ---------             .java files             
             *     |       |                   
             *  package   image(folder)
             *  ( or              |
             *   .class        404error.jpg
             *   files, if
             *   no package
             *   exists.)
             */
            //image = ImageIO.read(
            //      getClass().getResource(
            //              "/image/404error.jpg"));
            image = ImageIO.read(new URL(
                        "http://gagandeepbali.uk.to/" + 
                                "gaganisonline/images/404error.jpg"));
        }
        catch(IOException ioe)
        {
            System.out.println("Unable to fetch image.");
            ioe.printStackTrace();
        }
    }

    /*
     * Make this one customary habbit,
     * of overriding this method, when
     * you extends a JPanel/JComponent,
     * to define it's Preferred Size.
     * Now in this case we want it to be 
     * as big as the Image itself.
     */
    @Override
    public Dimension getPreferredSize()
    {
        return (new Dimension(image.getWidth(), image.getHeight()));
    }

    /*
     * This is where the actual Painting
     * Code for the JPanel/JComponent
     * goes. Here we will draw the image.
     * Here the first line super.paintComponent(...),
     * means we want the JPanel to be drawn the usual 
     * Java way first, then later on we will
     * add our image to it, by writing the other line,
     * g.drawImage(...).
     */
    @Override
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g.drawImage(image, 0, 0, this);
    }
}

以下の行のコメントを解除し、指定された場所に画像を追加してください。

image = ImageIO.read(
      getClass().getResource(
              "/image/404error.jpg")); 

それでも疑問がある場合は、質問をしてください。私の範囲内であれば、情報を提供しようとします:-)

于 2012-11-02T13:57:03.983 に答える
2

使用する背景色を設定するにはこれを試し、setBackground(Color.color_name);画像を設定するには以下のコードを試してください

Image bgImage= Toolkit.getDefaultToolkit().getImage("wallpaper_adrift.jpg");
contentPane.setBackgroundImage(bgImage);

http://www.daniweb.com/software-development/java/threads/346524/how-to-set-background-image-in-java-swing および フレームの背景として画像を設定する方法も参照してくださいJavaのSwing GUI?

于 2012-11-02T11:27:56.617 に答える
1

これはあなたが探しているソリューションです:

  1. like というパッケージを作成します。com.icon

  2. そのパッケージにアイコンを追加します (コピー/貼り付け)

  3. 次のようにボタンにアイコンを追加します。

    button.setIcon(new ImageIcon(NameOfClass.class.getResource("/com/icon/nameOfIcon.png")));
    

PS .png 形式であることを確認してください。

于 2012-11-02T11:08:16.990 に答える