gpt4 book ai didi

JButton 中的 Java 图标大小

转载 作者:行者123 更新时间:2023-12-02 11:20:24 25 4
gpt4 key购买 nike

我尝试将 JIcon 放入我的 JButtons 中,但有一个特定的它不适合。我尝试了不同的窗口和图标大小,但没有任何帮助,鲍尔的图标比我聪明。整个计划由 13x16 按钮组成,采用网格布局,边框为空。我不会发送整个代码,但可能只发送其中的相关部分。我尝试在新项目上制作按钮的网格布局,结果相同,球不适合

我尝试了球图像 40x40、50x50、60x60,其 jpg 文件。球应该看起来饱满而漂亮,但它的一侧被切掉了

enter image description here

编辑:我更改了代码以准确显示发生了什么,但仍然找不到错误。

import java.awt.*;
import javax.swing.*;
import javax.swing.border.Border;




public class Line extends JFrame {


private JPanel jPBoard;
private JButton jBFill[] = new JButton[209];
private Border empty;
private int fillCounter;
private int position;
private Icon iconBall;
private Icon iconSand;

public static void main(String[] args)
{
Line frame = new Line();
frame.setSize(520, 640);
frame.createGUI();
frame.setVisible(true);
}

private void createGUI()
{
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container window = getContentPane();


//Icons
try
{
iconBall = new ImageIcon(Toolkit.getDefaultToolkit().createImage(CBallMaze.class.getResource("images/ball.png")));
iconSand = new ImageIcon(Toolkit.getDefaultToolkit().createImage(CBallMaze.class.getResource("images/sand.jpg")));
}

catch (Exception e)
{
System.err.println("Couldn't process"+e);
}


//Main pannel settings

jPBoard = new JPanel();
jPBoard.setPreferredSize(new Dimension(520, 640));
jPBoard.setBackground(Color.GRAY);
window.add(jPBoard);
jPBoard.setLayout(new GridLayout(13, 16));

empty = BorderFactory.createEmptyBorder();


//Icon to Image, resizing

Image img = ((ImageIcon)iconBall).getImage();
Image imageBall = img.getScaledInstance(50, 50, java.awt.Image.SCALE_SMOOTH);
iconBall = new ImageIcon(imageBall);

for (fillCounter = 1; fillCounter < 209; fillCounter++) {


// Filling the field


jBFill[fillCounter] = new JButton(""+fillCounter);
jBFill[fillCounter].setBorder(empty);
position = 24;
jPBoard.add(jBFill[fillCounter]);

jBFill[fillCounter].setIcon(iconSand);


if (fillCounter == 15)
{
jBFill[fillCounter].setBackground(Color.PINK);
}

if (position == fillCounter)
{
jBFill[fillCounter].setIcon(iconBall);
}
}
}
}

最佳答案

一个mcve对于您描述的问题可能是:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;

public class Line extends JFrame {

private static int rows = 3, cols = 3;
private JButton jBFill[] = new JButton[rows*cols];

Line(){
createGUI();
pack();//apply preferred dimensions
setVisible(true);
}

public static void main(String[] args) {

SwingUtilities.invokeLater(() -> new Line());
}

private void createGUI(){
setDefaultCloseOperation(EXIT_ON_CLOSE);
//For posts it is more convenient to use generated images
Image ballImage = createImage(Color.BLUE);
Icon ballIcon = new ImageIcon(ballImage);

Image sandImage = createImage(Color.YELLOW);
Icon sandIcon = new ImageIcon(sandImage);

JPanel jPBoard = new JPanel();
jPBoard.setBackground(Color.GRAY);
add(jPBoard);
jPBoard.setLayout(new GridLayout(rows, cols));
Border empty = BorderFactory.createEmptyBorder();

for (int fillCounter = 0; fillCounter < (rows * cols); fillCounter++) {
JButton button = new JButton(""+fillCounter);
button.setBorder(empty);
button.setIcon(((fillCounter%2) ==0) ? sandIcon : ballIcon);
button.setPreferredSize(new Dimension(125,40)); //set preferred dimension to button
jBFill[fillCounter] = button;
jPBoard.add(button);
}
}

//creates 100x100 image
private BufferedImage createImage(Color color) {

int iconWidth =100;
BufferedImage img = new BufferedImage(iconWidth , iconWidth,
BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = img.createGraphics();
g2.setColor(color);
g2.fillOval(1, 1, iconWidth - 2, iconWidth - 2);
g2.dispose();
return img;
}
}

输出清楚地显示了问题:所使用的 100x100 球形图标被裁剪:

enter image description here

一种补救措施,如 one of the links 中建议的那样在我的评论中,是在将图像应用到按钮之前重新缩放图像。替换这些行:

    Image ballImage = createImage(Color.BLUE);
Icon ballIcon = new ImageIcon(ballImage);
Image sandImage = createImage(Color.YELLOW);
Icon sandIcon = new ImageIcon(sandImage);

与:

    Image ballImage = createImage(Color.BLUE);
Image scaledBall = ballImage.getScaledInstance(15, 15, Image.SCALE_SMOOTH ) ;
Icon ballIcon = new ImageIcon(scaledBall);
Image sandImage = createImage(Color.YELLOW);
Image scaledSand = sandImage.getScaledInstance(15, 15, Image.SCALE_SMOOTH ) ;
Icon sandIcon = new ImageIcon(scaledSand);

并查看带有缩放图像的输出:

enter image description here

关于JButton 中的 Java 图标大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49963096/

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