Home  >  Article  >  类库下载  >  Graphical buttons in java graphical interface

Graphical buttons in java graphical interface

高洛峰
高洛峰Original
2016-10-17 09:13:562129browse

To make the button graphic, just create an ImageIcon object, give the graphic path to the ImageIcon object, and then pass the object to the button.

 This involves the path settings of the graphics in eclipse, including (under the project path, under the non-project path, relative path, absolute path). The relative path is not preceded by /. The relative path here is relative to the project folder in eclipse. In other words, the absolute path is the unknown specific path where the graphic is located. Take pictureGraphical buttons in java graphical interface (under the path H:/java/workspace/study/src/picture) as an example:

1. If you place the picture folder under the study/src path (not the project path):

1.1 Absolutely Path: H:/java/workspace/study/src/picture/pictureGraphical buttons in java graphical interface 1.2 Relative path: src/picture/pictureGraphical buttons in java graphical interface 2. If you place the picture folder under the study path (project path):

2.1 The absolute path remains unchanged: H:/java/workspace/study/picture/pictureGraphical buttons in java graphical interface

2.2 Relative path: picture/pictureGraphical buttons in java graphical interface

package test;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import static net.mindview.util.SwingConsole.*;

public class PictureLabel extends JFrame{
    private static Icon[] pictures;
    private JButton jb,jb1 = new JButton("Disable");
    private boolean mad = false;
    
    public PictureLabel()
    {
        pictures = new Icon[]{
                new ImageIcon("src/picture/pictureGraphical buttons in java graphical interface"),   //相对路径
                new ImageIcon("H:/java/workspace/study/src/picture/picture2.jpg"),  //绝对路径
                new ImageIcon("src/picture/picture3.jpg"),
                new ImageIcon("src/picture/picture4.jpg"),
                new ImageIcon("src/picture/picture5.jpg")
        };
        //路径前不加/为相对路径
        
        jb = new JButton("JButton",pictures[3]);
        setLayout(new FlowLayout());
        jb.addActionListener(new ActionListener(){
            @Override//保证被标注的方法确实覆盖了基类的方法,否则编译会出错
            public void actionPerformed(ActionEvent e)
            {
                if(mad)
                {
                    jb.setIcon(pictures[3]);
                    mad = false;
                }else
                {
                    jb.setIcon(pictures[0]);
                    mad = true;
                }
                jb.setVerticalAlignment(JButton.TOP);
                jb.setHorizontalAlignment(JButton.LEFT);
            }
        });
        
        jb.setRolloverEnabled(true);  //允许翻转图标
        jb.setRolloverIcon(pictures[1]);
        jb.setPressedIcon(pictures[2]);
        jb.setDisabledIcon(pictures[4]);
        jb.setToolTipText("Yow");
        add(jb);
        //如果 setRolloverEnabled 为 true,则当鼠标移动到按钮上时,setRolloverIcon的内容就被用到该按钮的图形上,即picture[1];
        //当按下按钮时,setPressedIcon的内容被用到该按钮的图形上,即picture[2];当按钮被禁止时,setDisabledIcon的内容被应用到按钮,
        //即picture[4]。
       
        jb1.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e)
            {
                if(jb.isEnabled())
                {
                    jb.setEnabled(false);
                    jb1.setText("Enable");
                }else
                {
                    jb.setEnabled(true);
                    jb1.setText("Disable");
                }
            }
        });
        add(jb1);
    }
    
    public static void main(String[] args)
    {
        run(new PictureLabel(),500,200);
    }
}

The button available after compilation has an animation effect.

Note: 1. Do not add / before the relative path;

Graphical buttons in java graphical interface 2. In eclipse, the path of the file introduced in the program is relative to the project folder;

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn