使用NetBeans将ImageIcon添加到按钮属性.
print.setFont(new java.awt.Font("Serif", 0, 14));
print.setIcon(new javax.swing.ImageIcon(getClass().getResource("/project/print.gif")));
print.setMnemonic('P');
print.setText("Print");
print.setToolTipText("Print");
Run Code Online (Sandbox Code Playgroud)
编译时显示
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:205)
at project.Editor.initComponents(Editor.java:296)
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
阅读编辑2,了解我实际上缺少的内容
我目前正在尝试使用在photoshop中创建的具有alpha参数的图像创建一些自定义JButton.
到目前为止,覆盖paint()方法来绘制图像已经起到了绘制按钮以显示正确图像的意义.不过,我想通过使其形状(可点击区域)与图像上的可见像素相同来改进它(现在,如果我绘制按钮的边框,它就是一个正方形).
有没有一种简单的方法可以做到这一点,还是我必须解析图像并找到alpha像素来制作自定义边框?
我必须覆盖哪些方法才能使其按照我想要的方式工作?
另外,我将在稍后提出的另一个问题是:使用某种算法来更改图像的颜色会更好,这样看起来当人们点击它时它被点击或者我最好创建第二个按钮处于活动状态时图像和绘图?
编辑:我刚刚读到一些其他问题,我应该重新定义paintComponent()而不是paint(),我想知道为什么重新定义paint()工作正常?
编辑2:我改变了一切,以确保我的JButton是使用带图标的默认构造函数创建的.我要做的是获取点击注册位置的X和Y位置并抓住该位置的图标像素并检查其alpha通道以查看它是否为0(如果是,则不执行任何操作,否则执行此操作应该采取的行动).
问题是,alpha通道始终返回255(蓝色,红色和绿色在透明像素上为238).在其他像素上,一切都返回它应该返回的值.
这是一个示例(如果需要,可以使用另一个图像尝试)重新创建我的问题:
public class TestAlphaPixels extends JFrame
{
private final File FILECLOSEBUTTON = new File("img\\boutonrondX.png"); //My round button with transparent corners
private JButton closeButton = new JButton(); //Creating it empty to be able to place it and resize the image after the button size is known
public TestAlphaPixels() throws IOException
{
setLayout(null);
setSize(150, 150);
closeButton.setSize(100, 100);
closeButton.setContentAreaFilled(false);
closeButton.setBorderPainted(false);
add(closeButton);
closeButton.addMouseListener(new MouseListener()
{
public void mouseClicked(MouseEvent e)
{
}
public void …Run Code Online (Sandbox Code Playgroud) 当它被点击JLabel,我想明白了,如果点击的是"Icon part",和" Text part"的JLabel,所以不同的可以采取行动.有一个聪明的办法做到这一点?或者只是我必须与坐标相对解决问题图标和文字?
我正在学习java swing游戏的教程:http://zetcode.com/tutorials/javagamestutorial/movingsprites/
在此刻:
ImageIcon ii = new ImageIcon(this.getClass().getResource());
image = ii.getImage();
Run Code Online (Sandbox Code Playgroud)
我只是不知道我要写什么样的路径以及我应该在哪里保存我的图像(哪个目录).
你能帮帮我吗?你举个例子吗?
Icon bug = new ImageIcon( getClass().getResource( "bug1.png" ) );
ImageIcon bug = new ImageIcon( getClass().getResource( "bug1.png" ) );
Run Code Online (Sandbox Code Playgroud)
这两者有什么区别?我的书总是使用第一行中的方式声明一个ImageIcon但是不是更好地将它声明为第二种方式,因为更具体地说它是一个ImageIcon?
我已经阅读了java api,但我仍然不明白它们之间的主要区别:
1)ImageIcon 2)BufferedImage 3)VolatileImage 4)图像
有人能告诉我每个人的使用时间吗?
好的,我有一个静态的ImageIcon,而Image却没有显示出来.在同一个程序中我使用其他ImagesIcon,但它们不是静态的,所以当我声明它们时,我这样做:
public ImageIcon blabla = new ImageIcon(getClass().getResource(blabla.png));
Run Code Online (Sandbox Code Playgroud)
但是,如果我声明一个ImageIcon静态我不能使用该行,因为无法从静态值访问getClass().现在这些图像没有显示出来:
public static ImageIcon blabla = new ImageIcon(blabla.png);
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助!
public static ImageIcon networkOfflineIcon = new ImageIcon("Images/networkOfflineIcon.png");
public static ImageIcon networkIcon = new ImageIcon("Images/networkIcon.png");
protected static JMenuItem jmiRemote = new JMenuItem(" Remote", networkOfflineIcon);
//************************************************************************
public static void changeNetWorkStatus(boolean network_status)
//************************************************************************
{
if(network_status){
Application.jmiRemote.setIcon(networkIcon);
Application.jmiRemote.setText("NetWork Online/Remote is On");
Application.lockScreenRemote();
}else if(!network_status){
Application.jmiRemote.setIcon(networkOfflineIcon);
Application.jmiRemote.setText("NetWork Offline/Remote is Off");
Application.unlockScreenRemote();
}
}//DOESNT CHANGE THE IMAGE
//************************************************************************
Run Code Online (Sandbox Code Playgroud) 我试图将相同的jlabel存储图像两次加载到gridlayout面板中,但是不是创建图像的两个实例,而是仅显示一次然后移动图像.
如何将件数组中的相同JLabel位置存储到boardLabels数组中的多个JLabel中.
谢谢 :)
public static JPanel boardPanel = new JPanel(new GridLayout(4, 0));
public static JLabel pieces[] = new JLabel[2];
private static JLabel[] boardLabels = new JLabel[4];
public MainFrame() {
pieces[0] = new JLabel(new ImageIcon(System.getProperty("user.dir") + "/images/piece1.png"));
pieces[1] = new JLabel(new ImageIcon(System.getProperty("user.dir") + "/images/piece2.png"));
this.add(boardPanel);
displayGUIboard();
}
public static void displayGUIboard() {
//ERROR - the label in pieces[0] is not copied into both boardLabels [0] and [1]
boardLabels[0] = pieces[0];
boardLabels[1] = pieces[0];
boardPanel.add(boardLabels[0]);
boardPanel.add(boardLabels[1]);
}
public static void main(String[] …Run Code Online (Sandbox Code Playgroud) 如何将已调整大小的图像保存到特定文件夹?
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
ImgChooser ic = new ImgChooser();
ImageIcon icon = new ImageIcon(me,"id pic");
Image img1 = icon.getImage();
Image img2 = img1.getScaledInstance(105, 105, 0);
icon.setImage(img2);
jLabel1.setIcon(icon);
}
Run Code Online (Sandbox Code Playgroud)
第一个代码是我获取图像并调整大小的地方.然后我希望调整大小的图像保存在另一个文件夹中.提前致谢
每当从JComboBox中选择一个项目时,我都试图从图像文件夹中将图标设置为JLabel.JComboBox中的项目名称和文件夹中图像的名称相同.因此,无论何时从JComboBox中选择一个项目,都应将具有相同名称的相应图像设置为JLabel的图标.我想做这样的事情.
private void cmb_movieselectPopupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt){
updateLabel(cmb_moviename.getSelectedItem().toString());
}
protected void updateLabel(String name) {
ImageIcon icon = createImageIcon("C:\\Users\\xerof_000\\Pictures\\tmspictures\\" + name + ".jpg");
if(icon != null){
Image img = icon.getImage();
Image newimg = img.getScaledInstance(lbl_pic.getWidth(), lbl_pic.getHeight(), java.awt.Image.SCALE_SMOOTH);
icon = new ImageIcon(newimg);
lbl_pic.setIcon(icon);
lbl_pic.setText(null);
}
else{
lbl_pic.setText("Image not found");
lbl_pic.setIcon(null);
}
}
protected static ImageIcon createImageIcon(String path) {
URL imgURL;
imgURL = NowShowing.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL);
} else {
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
我认为问题出现在"C:\ Users\xerof_000\Pictures\tmspictures \"中我尝试使用"C:/ Users/xerof_000/Pictures/tmspictures /",但即使这样也行不通.无论我做什么,它只在JLabel上显示"Image not found".