在java中怎样在JLabel上添加JButton呢

在java中怎样在JLabel上添加JButton呢,第1张

java swing中JLabel中添加JButton只需要使用JLabel的add方法就可以添加,实例如下:

package components

 

import java.awt.event.ActionEvent

import java.awt.event.ActionListener

import java.awt.event.KeyEvent

import java.net.URL

 

import javax.swing.ImageIcon

import javax.swing.JButton

import javax.swing.JFrame

import javax.swing.JPanel

import javax.swing.SwingUtilities

 

public class JButtonTest extends JPanel implements ActionListener {

 

   

    private static final long serialVersionUID = 1L

 

    JButton button1,button2,button3

    public JButtonTest() {

       //创建button上的图标

       ImageIcon imageIcon1 = createImage("images/right.gif")

       ImageIcon imageIcon2 = createImage("images/middle.gif")

       ImageIcon imageIcon3 = createImage("images/left.gif")

      

       //创建Button,并设置Button的图标

       button1 = new JButton("中间按钮不可用",imageIcon1)

       //设置Button的文本位置

       button1.setVerticalTextPosition(JButton.CENTER)

       button1.setHorizontalTextPosition(JButton.LEADING)

       //设置Button的快捷键

       button1.setMnemonic(KeyEvent.VK_D)

       //设置Button的反馈消息,消息处理者通过这个标记来辨别是哪个按钮被点击

       button1.setActionCommand("disable")

       //为Button添加监听者

       button1.addActionListener(this)

       //设置Button的提示信息

       button1.setToolTipText("点击此按键,此按键和中间按键变为不可用,右边按键变为可用")

       //将Button添加到panel中

       add(button1)

 }

在学习Swing 图形化界面的时候,会出现标签组件---JLabel,而在容器JFrame当中进行设置的时候,可以添加图片,此图片可以充当标签,那么我们还需要一个接口,那就是Icon接口,专门来接收图片的!

现在通过步骤来解决如何将图片添加到标签组件中!

第一步: 创建JFrame容器,并设置标题

第二步: 定义字符串类型的图片路径,目的为ImageIcon类传参

第三步:实例化Icon接口对象,通过子类ImageIcon来完成。

第四步:创建JLabel对象,传入Icon对象和设置在容器当中的位置。

第五步: 设置容器的大小、背景颜色、并显示出来!

具体代码如下:

public class JLabelDemo {

public static void main(String[] args) {

//实例化JFrame对象,并设置标题

JFrame f = new JFrame("我的窗体")

//定义图片路径

String picPath = "d:" + File.separator + "haha.gif"

//实例化Icon对象,并传入图片路径

Icon icon = new ImageIcon(picPath)

// 实例化标签对象,传入Icon对象,并设定位置。

JLabel lab = new JLabel(icon,JLabel.CENTER)

lab.setBackground(Color.YELLOW) //设置窗体的背景颜色

f.add(lab) //将组件件入到JFrame面板之中

f.setSize(600,360) //设置窗体的大小;

f.setLocation(300,200) //设置窗体出现的位置

f.setVisible(true) //让窗体显示出来。

}

}

默认的就是JLabel了。

如果你要该我给你一个样板。

public class myList extends JPanel {

private static final long serialVersionUID = 1L

JList mList

DefaultListModel mode

public myList() {

setLayout(new BorderLayout())

mode = new DefaultListModel()

for (int i = 0i <10i++) {

mode.addElement("123")

}

mList = new JList(mode)

mList.setCellRenderer(new MyCellRenderer())

add(new JScrollPane(mList), BorderLayout.CENTER)

}

public static void main(String[] args) {

JFrame jf = new JFrame()

jf.add(new myList(), BorderLayout.CENTER)

jf.pack()

jf.setVisible(true)

jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

}

class MyCellRenderer extends JButton implements ListCellRenderer {

public MyCellRenderer() {

setOpaque(true)

}

public Component getListCellRendererComponent(JList list,

Object value,

int index,

boolean isSelected,

boolean cellHasFocus) {

setText(value.toString())

Color background

Color foreground

// check if this cell represents the current DnD drop location

JList.DropLocation dropLocation = list.getDropLocation()

if (dropLocation != null &&!dropLocation.isInsert() &&dropLocation.getIndex() == index) {

background = Color.BLUE

foreground = Color.WHITE

// check if this cell is selected

} else if (isSelected) {

background = Color.RED

foreground = Color.WHITE

// unselected, and not the DnD drop location

} else {

background = Color.WHITE

foreground = Color.BLACK

}

setBackground(background)

setForeground(foreground)

return this

}

}

}

listCellRenderer是控制显示的,我这里是button,你也可以换成别的,不建议使用jPanel,panel无法显示字体,所以你要自己弄个控件显示。

Model只负责保存数据,和界面无关,不要把界面也放到这里了。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/11541085.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-16
下一篇 2023-05-16

发表评论

登录后才能评论

评论列表(0条)

保存