gpt4 book ai didi

java - 将图像添加到 JButton

转载 作者:行者123 更新时间:2023-12-01 15:22:18 25 4
gpt4 key购买 nike

我正在尝试将图像添加到 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();*/

// }








}
}

最佳答案

您可以生成一个 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 键,或右键单击 → 刷新)。

关于java - 将图像添加到 JButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10729024/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com