标签: imageicon

JLabel图像阵列

我试图将相同的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)

java swing jlabel imageicon

6
推荐指数
2
解决办法
4938
查看次数

Java - 在图像的中心绘制文本

我需要在图像的中心写文字.要写的文字并不总是一样的.

我正在使用的代码在这里:

// Here I first draw the image
g.drawImage(img, 22, 15, 280, 225, null);
// I get the text 
String text = photoText.getText();
// Set the text color to black
g.setColor(Color.black);
// I draw the string 
g.drawString(text, 79.5F, 220.0F);
Run Code Online (Sandbox Code Playgroud)

问题是文本不在图像的中心,我该怎么办?

我只需要在水平中心绘制文字.

java graphics swing graphics2d imageicon

5
推荐指数
3
解决办法
1万
查看次数

在JButton上动画GIF,当鼠标悬停时播放它

Icon icon = new ImageIcon(getClass().getResource( "/img/icon.gif" ) );
aButton = new JButton("Its a button", icon);
Run Code Online (Sandbox Code Playgroud)

是否有某种方法可以阻止动画播放?我正在考虑分配gif的静态jpg,然后当我悬停时,分配动画gif,但我不认为有一个事件可以取消鼠标,MouseMotionListener所以我可以加载静态jpg.

按钮中的gif循环,但是,如果我将鼠标悬停在按钮上,它就会消失.

如果我的鼠标光标不在按钮上,如何使gif静态?

如果我使用MouseMotionListener,如果我取下鼠标,它是否会触发事件?

@Override
public void mouseMoved(MouseEvent e) {
//play the gif
//if I take mouse off, call some method to stop playing animated gif
}

@Override
public void mouseDragged(MouseEvent e) {
}
Run Code Online (Sandbox Code Playgroud)

java swing animated-gif jbutton imageicon

5
推荐指数
1
解决办法
5369
查看次数

如何将ImageIcon添加到JFrame?

我正在尝试将图像添加到一个帧但看起来它不起作用.ImageIcon从指定文件创建的图像.图像文件位于java文件存在的seam目录中.

import java.awt.BorderLayout;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

    public class image {

        public static void main(String args[])
        {
            TimeFrame frame = new TimeFrame();
        }
    }

    class TimeFrame extends JFrame
    {
        //Image icon = Toolkit.getDefaultToolkit().getImage("me.jpg");
        ImageIcon icon = new ImageIcon("me.jpg");
        JLabel label = new JLabel(icon);
        public TimeFrame(){
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setTitle("My Frame");
            setSize(500,400);
            //this.setIconImage(icon);
            add(label,BorderLayout.CENTER);
            setVisible(true);
        }


    }
Run Code Online (Sandbox Code Playgroud)

java swing jlabel jframe imageicon

5
推荐指数
2
解决办法
6万
查看次数

关于 ImageIcons 的方法不起作用

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;


public class Cards extends JFrame {

private GridLayout grid1;

    JButton []bt=new JButton[52];

    ImageIcon tail=new ImageIcon(getClass().getResource("b1fv.png"));

    ImageIcon ori;

    public Cards(){
        grid1=new GridLayout(7,9,2,2);
        setLayout(grid1);
        for(int i=0;i<bt.length;i++){
            ImageIcon c=new ImageIcon(getClass().getResource(i+1+".png"));
            bt[i]=new JButton(c);
            bt[i].addActionListener(new RatingMouseListener(i));
            add( bt[i]);
        }
    }

    public static void main(String[] args){
        Cards frame=new Cards();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(1400,700);
        frame.setVisible(true);
    }

    private class RatingMouseListener implements ActionListener {
        private  int index=0;


        public RatingMouseListener(int index) {
            this.index = index;

        }




        public void actionPerformed(ActionEvent e) {
              System.out.println("Mouse entered for rating " …
Run Code Online (Sandbox Code Playgroud)

java swing actionlistener imageicon

5
推荐指数
1
解决办法
723
查看次数

如何将图像图标设置为JButton

我不是很擅长创建Swing应用程序.所以我有问题如何设置图标JButton.

我的项目结构如下所示:

在此输入图像描述

JButtonMainWindow课堂上很简单:它看起来像这样:

tactButton = new JButton("next tact");
Run Code Online (Sandbox Code Playgroud)

我想使用方法将图像设置为此按钮setIcon.我的代码看起来像这样:

tactButton.setIcon(new ImageIcon(getClass().getResource("/images/button_next.jpg")));
Run Code Online (Sandbox Code Playgroud)

但是当我启动应用程序时,我有例外:

java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(ImageIcon.java:205)
    at by.bulgak.conveyor.gui.MainWindow.<init>(MainWindow.java:117)
    at by.bulgak.conveyor.gui.MainWindow$1.run(MainWindow.java:46)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:721)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:682)
    at java.awt.EventQueue$3.run(EventQueue.java:680)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:691)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)
Run Code Online (Sandbox Code Playgroud)

所以我尝试了不同的东西:

  • 把所有的照片在同一文件夹中MainWindow的类
  • 将图片放在项目根文件夹中
  • 尝试类似的东西 tactButton.setIcon(new ImageIcon("/images/button_next.jpg"));

但我有这个例外,或者如果我使用tactButton.setIcon(new ImageIcon("/images/button_next.jpg"));我有简单的按钮没有图像.

最后,我写了我的图像的绝对路径,这很好(但绝对路径不是好主意).你能帮帮我吗?

我看了一个问题如何将图像添加到JButton并试图像那样做.

UPDATE

创建按钮和设置图标的完整代码:

tactButton = new JButton("next tact");
tactButton.setSize(100, …
Run Code Online (Sandbox Code Playgroud)

java swing packaging image imageicon

5
推荐指数
0
解决办法
3万
查看次数

我希望在滚动时将图片保留在 jscrollpane 的左上角

我在 JScrollpane 中有一个 JPanel。我在 BufferedImage 上绘制,并将其显示在 JPanel 上。在 JScrollpane 的左上角,我想要一张图片,当我向下滚动查看 JPanel 的其余部分时,该图片始终保留在该角落。这里是Jpanel的paintComponent方法:

@Override
public void paintComponent(Graphics g){
    super.paintComponent(g);
    if (bufferedImage != null){
        g.drawImage(bufferedImage, 0, 0, this);
        Point p = parent.getViewPosition();
        System.out.println("paintComponent(): "+ p.x + "," + p.y);
        g.setColor(Color.RED);
        g.fillRect(p.x + 20, p.y + 20, 200, 200);
    }
}
Run Code Online (Sandbox Code Playgroud)

其中parent.getViewPosition()给我scrollPane.getViewport().getViewPosition()。当我启动时,我可以看到左上角有红色矩形的缓冲图像。当我向下滚动时,我可以看到缓冲图像的其余部分,但红色矩形向上移动,然后消失,并且当我向上滚动时不再出现。在控制台中,我可以看到滚动时点 p 发生变化:

paintComponent(): 0,0
paintComponent(): 0,10
paintComponent(): 0,20
paintComponent(): 0,30
paintComponent(): 0,40
paintComponent(): 0,50
Run Code Online (Sandbox Code Playgroud)

谁能帮我解决这个问题吗?

java swing jscrollpane imageicon jviewport

5
推荐指数
1
解决办法
692
查看次数

JLabel得到了图像

有没有更好的方法可以将容器中的JLabel的Icon作为BufferedImage获得多个转换?

Component[] components = container.getComponents();
BufferedImage image = ((BufferedImage) ((ImageIcon) ((JLabel) components[i]).getIcon()).getImage());
Run Code Online (Sandbox Code Playgroud)

java swing image jlabel imageicon

5
推荐指数
1
解决办法
6477
查看次数

我们可以使用Font Awesome创建自己的自定义图标吗?

我有web应用程序,我想在我的项目中使用不同的图标,因为我使用了以下css和代码.

<p><i class="fa fa-camera-retro fa-lg "></i> fa-camera-retro</p>
<p><i class="fa fa-camera-retro fa-2x "></i> fa-camera-retro</p>
<p><i class="fa fa-camera-retro fa-3x "></i> fa-camera-retro</p>
<p><i class="fa fa-camera-retro fa-4x "></i> fa-camera-retro</p>
<p><i class="fa fa-camera-retro fa-5x "></i> fa-camera-retro</p>
Run Code Online (Sandbox Code Playgroud)

字体awesome.min.css

    /*!
 *  Font Awesome 4.0.3 by @davegandy - http://fontawesome.io - @fontawesome
 *  License - http://fontawesome.io/license (Font: SIL OFL 1.1, CSS: MIT License)
 */
/* FONT PATH
 * -------------------------- */
@font-face {
  font-family: 'FontAwesome';
  src: url('../fonts/fontawesome-webfont.eot?v=4.0.3');
  src: url('../fonts/fontawesome-webfont.eot?#iefix&v=4.0.3') format('embedded-opentype'), url('../fonts/fontawesome-webfont.woff?v=4.0.3') format('woff'), url('../fonts/fontawesome-webfont.ttf?v=4.0.3') format('truetype'), url('../fonts/fontawesome-webfont.svg?v=4.0.3#fontawesomeregular') format('svg');
  font-weight: normal;
  font-style: normal; …
Run Code Online (Sandbox Code Playgroud)

css fonts icons imageicon font-awesome

5
推荐指数
1
解决办法
2万
查看次数

如何在jlist中添加图像

嗨我已经创建了aj列表,我想在该文本中的任何文本之前添加图像我怎么能这样做我试过但我无法实现我的目标我想在列表元素"Barmer"之前添加图像.

    public class ListDemo extends JPanel
                          implements ListSelectionListener {
        private JList list;
        private DefaultListModel listModel;


        public ListDemo() {
            super(new BorderLayout());

            listModel = new DefaultListModel();
            listModel.addElement("Barmer");
           //Create the list and put it in a scroll pane.
            list = new JList(listModel);
            list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            list.setSelectedIndex(0);
            list.addListSelectionListener(this);
            list.setVisibleRowCount(5);
            list.setBackground(new java.awt.Color(0,191,255));;
           list.setFont(new Font("Arial",Font.BOLD,35));
            list.setForeground( Color.white );
            list.setFixedCellHeight(60);
    list.setFixedCellWidth(50);
    list.setBorder(new EmptyBorder(10,20, 20, 20));


            JScrollPane listScrollPane = new JScrollPane(list);
            add(listScrollPane, BorderLayout.CENTER);

        }
         public void valueChanged(ListSelectionEvent e) {

        }

        private static void createAndShowGUI() {
            //Create and set up the …
Run Code Online (Sandbox Code Playgroud)

java swing jlist imageicon

5
推荐指数
1
解决办法
2万
查看次数