在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)

 }

用frame.add(button)添加按钮对象到窗枯厅体对没碰隐象,吵首

再用frame.setVisable(true)显示窗口即可

或用

panel.add(button)

frame.add(panel)

frame.setVisable(true)

/** 给个例子你看下吧 同学, 开发swing 最好的利剑 莫过于 NetBeans 6 了 * 用手写也 不用 怎样 很简单 new 一个汪段控件 add 进 去 就 可以了* 希望对你有用 同学拿陵陵*/

import java.awt.FlowLayoutimport javax.swing.JButton

import javax.swing.JFrame

import javax.swing.JList

import javax.swing.JPanel

import javax.swing.SwingUtilities

import javax.swing.WindowConstants

public class download extends JFrame { /**

* @param args

*/

JFrame j

public download(){

j = new JFrame("java tcp/ip 下载")

j.setBounds(100, 100, 800, 600) // 实例 一 窗体

j.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE)//JJFrame.EXIT_ON_CLOSE

j.getContentPane().setLayout(new FlowLayout(ABORT)) //布局设置

JPanel p = new JPanel()

p.setBounds(100, 100, 100, 100)

p.setLayout(new FlowLayout())

JButton jbtn = new JButton()

jbtn.setText("下载")

p.setSize(100, 100)

String[] data = {"one", "two", "three", "four"}

JList myList = new JList(data)

add(myList)

p.add(jbtn)p.add(myList) // 将 button list 控件 添加 panel 中

j.getContentPane().add(p) // 将 panel 添加 进窗体

j.pack() //自动适应窗体大小

j.setVisible(true)}

public static void main(String[] args) {

// TODO Auto-generated method stub

SwingUtilities.invokeLater(new Runnable(){

public void run() {

/消戚/ TODO Auto-generated method stub

new download()

}

})

}}


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

原文地址:https://54852.com/bake/8036850.html

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

发表评论

登录后才能评论

评论列表(0条)

    保存