Java 向JButton添加图像

Java 向JButton添加图像,java,eclipse,swing,icons,jbutton,Java,Eclipse,Swing,Icons,Jbutton,我正在尝试向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.Dimensi

我正在尝试向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();*/

                //  }








            }
}           
这是代码:
包,例如edu.guc.dvonn.gui;
导入java.awt.BorderLayout;
导入java.awt.Color;
导入java.awt.Container;
导入java.awt.Dimension;
导入java.awt.FlowLayout;
导入java.awt.GridLayout;
导入java.awt.TextArea;
导入java.awt.TextField;
导入java.awt.Font;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入javax.swing.JButton;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
进口如edu.guc.dvonn.engine.Board;
公共类FirstWindow扩展JFrame实现ActionListener{
JButton开始按钮;
JPanel welcomePanel;
JPanel第二小组;
JPanel标准小组;
JPanel custPanel;
JPanel panelFill;
JButton标准;
钮扣填充;
钮扣放;
纽顿卡斯特;
JLabel标签;
文本字段行;
TextField col;
董事会b;
JButton[]]按钮;
int r1;
int cc;
公共窗口(){
设置大小(800600);
setVisible(真);
setLayout(新的BorderLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
welcomePanel=新的JPanel();
标签=新的JLabel(“欢迎使用Dvonn”);
welcomePanel.添加(标签);
welcomePanel.setVisible(真);
标签。设置前景(颜色。红色);
label.setFont(新字体(“衬线”,Font.PLAIN,40));
Startbutton=新的JButton(“开始”);
开始按钮。后退背景(颜色。黑色);
开始按钮。设置前景(颜色。红色);
Startbutton.setPreferredSize(新尺寸(120,40));
welcomePanel.add(开始按钮);
添加(welcomePanel);
addActionListener(这个);
SecondPanel=newjpanel();
SecondPanel.setVisible(假);
第二个面板。设置尺寸(400400);
行=新文本字段();
col=新文本字段();
添加(行);
第二个面板。添加(col);
填充=新按钮(“随机填充”);
fill.addActionListener(this);
填充.立根背景(颜色:浅灰色);
填充。设置前景(颜色。蓝色);
put=新按钮(“put”);
放置背景(颜色:浅灰色);
设置前景(颜色为蓝色);
标准=新JButton(“标准化板”);
标准设置背景(颜色:浅灰色);
标准。设置前景(颜色。蓝色);
Standard.addActionListener(本);
cust=新的JButton(“定制”);
客户背景(颜色:浅灰色);
客户设置前景(颜色为蓝色);
cust.addActionListener(本);
panelFill=新的JPanel();
面板填充。添加(填充);
面板填充。添加(放置);
第二个面板。添加(标准);
第二个面板。添加(客户);
b=新板(r1,cc);
这是1.r1=5;
这1.cc=11;
按钮=新的JButton[r1][cc];
StandardPanel=新的JPanel();
标准面板。立根背景(颜色。白色);
StandardPanel.setLayout(新网格布局(r1,cc));
对于(int i=0;i参见此处

您可以生成一个
图像图标
,并使用

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
如果不起作用,请尝试创建
例如.edu.guc.dvonn.resources
包,并将第二行更改为:

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

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

我建议您简化所做的工作,并尝试此基础教程。如果您使用gif,则将其更改为jpg。如果这样做有效,则可以使用更简单的示例替换代码


Jbutton教程:

你用
setIcon()试过了吗?
?谢谢,真是太棒了helpful@user1413755:你可以通过点击左边的按钮来接受这个答案。@trashgood:但我注意到了(当我尝试使用netbeans时)。图像嵌入的是图像的大小,而不是按钮的大小。。我们如何将图像嵌入按钮的大小。。@akp:听起来您需要按照建议将图像缩放到一个方便的大小。