我试着将JIcon放入我的JButtons中,但有一个特别的想要它不适合。我尝试了不同的窗口和图标大小,但没有任何帮助,Ball的图标超出我的要求。 整个计划由网格布局中的13x16按钮和空边框构成。 我不是发送整个代码,但可能只是它的相关部分。 我尝试在新项目上制作按钮的网格布局并且结果相同,球不适合
我尝试了球像40x40,50x50,60x60,它的jpg文件。 球应该看起来很漂亮,但是它的一边切割
编辑:我更改代码以显示正在发生的事情,仍然无法找到错误。
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);
}
}
}
}
答案 0 :(得分:1)
您描述的问题的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球形图标被裁剪:
我的评论中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);
使用缩放图像查看输出: