0

我正在尝试将图像添加到 JButton,但我不确定我缺少什么。当我运行以下代码时,按钮看起来与我创建它时没有任何图像属性完全相同。Background.jpeg 位于我的项目文件夹的根目录中。

this is the code:

package eg.edu.guc.dvonn.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.TextArea;
import java.awt.TextField;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;



import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import eg.edu.guc.dvonn.engine.Board;


public class FirstWindow extends JFrame implements ActionListener {

    JButton Startbutton;
    JPanel welcomePanel;
    JPanel SecondPanel;
    JPanel StandardPanel;
    JPanel custPanel;
    JPanel  panelFill;
    JButton Standard;
    JButton fill;
    JButton put;
    JButton cust;
    JLabel label;
    TextField rows;
    TextField col;
    Board b;
    JButton[][] button;
    int r1;
    int cc;


         public FirstWindow(){

             setSize(800,600);
             setVisible(true);
             setLayout(new BorderLayout());

             setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

              welcomePanel = new JPanel();

            label  = new JLabel("Welcome to Dvonn");
            welcomePanel.add(label);
            welcomePanel.setVisible(true);

             label.setForeground(Color.RED);
             label.setFont(new Font("Serif", Font.PLAIN, 40));


             Startbutton = new JButton ("start");



             Startbutton.setBackground(Color.BLACK);
             Startbutton.setForeground(Color.RED);
             Startbutton.setPreferredSize(new Dimension(120,40));

             welcomePanel.add(Startbutton);
             add(welcomePanel);

             Startbutton.addActionListener(this);


             SecondPanel = new JPanel();
             SecondPanel.setVisible(false);
             SecondPanel.setSize(400,400);
              rows = new TextField();
              col= new TextField();
              SecondPanel.add(rows);
              SecondPanel.add(col);

              fill = new JButton("fill random");
              fill.addActionListener(this);
              fill.setBackground(Color.LIGHT_GRAY);
              fill.setForeground(Color.BLUE);


              put = new JButton("put");
              put.setBackground(Color.LIGHT_GRAY);
              put.setForeground(Color.BLUE);


              Standard = new JButton("Standardised board");
              Standard.setBackground(Color.LIGHT_GRAY);
              Standard.setForeground(Color.BLUE);
              Standard.addActionListener(this);


              cust = new JButton("customized");
              cust.setBackground(Color.LIGHT_GRAY);
              cust.setForeground(Color.BLUE);
              cust.addActionListener(this);

              panelFill = new JPanel();

              panelFill.add(fill);
              panelFill.add(put);
              SecondPanel.add(Standard);
              SecondPanel.add(cust);


              b = new Board(r1,cc);  
                this.r1 =5;
                this.cc = 11;
                button = new JButton [r1][cc];
                StandardPanel = new JPanel();
                StandardPanel.setBackground(Color.WHITE);
                StandardPanel.setLayout(new GridLayout(r1, cc));

                for (int i = 0; i <= r1 - 1; i++) {
                    for (int j = 0; j <= cc - 1; j++) {
                        button[i][j] = new JButton(); // i want to     add an image to it

                        button[i][j].addActionListener(this);
                    }
                }
                for (int i = 0; i <= r1 - 1; i++) {
                    for (int j = 0; j <= cc - 1; j++) {

                        button[i][j].setLayout(new FlowLayout());
                        button[i][j].setBackground(Color.WHITE);

                        StandardPanel.add(button[i][j], i, j);

                    }
                }



         }  

         public FirstWindow(int row,int cols){

             setSize(800,600);
             setVisible(true);
             setLayout(new BorderLayout());

             setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


              fill = new JButton("fill random");
              fill.addActionListener(this);
              fill.setBackground(Color.LIGHT_GRAY);
              fill.setForeground(Color.BLUE);


              put = new JButton("put");
              put.setBackground(Color.LIGHT_GRAY);
              put.setForeground(Color.BLUE);


              Standard = new JButton("Standardised board");
              Standard.setBackground(Color.LIGHT_GRAY);
              Standard.setForeground(Color.BLUE);
              Standard.addActionListener(this);

              cust = new JButton("customized");
              cust.setBackground(Color.LIGHT_GRAY);
              cust.setForeground(Color.BLUE);
              cust.addActionListener(this);

              panelFill = new JPanel();
              panelFill.add(fill);
              panelFill.add(put);





            this.r1 = row;
            this.cc = cols;



                 b = new Board(r1,cc);  


                button = new JButton [r1][cc];

                custPanel = new JPanel();
                custPanel.setBackground(Color.WHITE);
                custPanel.setLayout(new GridLayout(r1, cc));

                for (int i = 0; i <= r1 - 1; i++) {
                    for (int j = 0; j <= cc - 1; j++) {
                        button[i][j] = new JButton(); // i want to     add an image to it
                        button[i][j].addActionListener(this);
                    }
                }
                for (int i = 0; i <= r1 - 1; i++) {
                    for (int j = 0; j <= cc - 1; j++) {

                        button[i][j].setLayout(new FlowLayout());
                        button[i][j].setBackground(Color.WHITE);
                        custPanel.add(button[i][j], i, j);

                    }
                }
                add(custPanel);
                add(panelFill,BorderLayout.SOUTH);


         }
         public static void main  (String [] args){
             FirstWindow window = new FirstWindow();



         }
        @Override
        public void actionPerformed(ActionEvent e) {

            if (e.getSource()==Startbutton){

                this.remove(welcomePanel);
                this.add(SecondPanel);
                SecondPanel.setVisible(true);
                //this.add(SecondPanel);

                JLabel label1 = new JLabel("dvonn");
                label1.setForeground(Color.RED);
                label1.setFont(new Font("Serif", Font.PLAIN, 25));
                SecondPanel.add(label1);

                }

                if(e.getSource()== cust){
                   String rowText = rows.getText();
                   String colText = col.getText();

                   r1 = Integer.parseInt(rowText);
                   cc = Integer.parseInt(colText);
                   this.setVisible(false);
                FirstWindow custom =new FirstWindow(r1, cc);




                 } 
                      if(e.getSource()==Standard){
                         SecondPanel.setVisible(false); // set the second panel invisible
                         this.add(StandardPanel); //to add the panel with the buttons
                         StandardPanel.setVisible(true);

                         add(panelFill,BorderLayout.SOUTH);
                         add(StandardPanel,BorderLayout.CENTER);

                     }



              /* Board b  = new Board(r1,c1);
                    b.fillRandom();*/

                //  }








            }
}           
4

3 回答 3

1

您可以使用生成一个ImageIcon并将其设置为按钮的图标

JButton button; // precondition: not null
ImageIcon icon = new ImageIcon(FirstWindow.class.getResource("/Background.jpeg"));
button.setIcon(icon);
button.setDisabledIcon(icon); // or a grayed-out version

如果这不起作用,请尝试创建一个eg.edu.guc.dvonn.resources包并将第二行更改为:

ImageIcon icon = new ImageIcon(
    FirstWindow.class.getResource(
    "/eg/edu/guc/dvonn/resources/Background.jpeg"));

确保在运行代码之前刷新 Eclipse 项目(单击项目管理器、空白处或项目名称,然后按 F5 键,或右键单击 → 刷新)。

于 2012-05-24T00:02:28.800 回答
0

见这里 http://docs.oracle.com/javase/tutorial/uiswing/components/button.html

于 2012-05-23T23:18:03.230 回答
0

我建议简化您正在做的事情并尝试这个基本教程。如果你让它与 gif 一起使用,那么将其更改为你的 jpg。如果可行,那么您可以用更简单的示例替换代码。

Jbutton教程: http: //www.apl.jhu.edu/~hall/java/Swing-Tutorial/Swing-Tutorial-JButton.html

于 2012-05-24T00:05:39.873 回答