使用Javaswing自定义图片作为按钮(原创)

本人初学习Javaswing没多久,刚开始找不到怎么把图片放在按钮上,上网查找资料都是涵盖了在项目中的,自己摸索后才知道原来没有那么难。下面是我写的例子:

package com.iconButtonDemo;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
/**
 * 自定义图片按钮
 * @author lenovo
 *
 */
public class IconOnButton {
    //声明窗体
    private JFrame frame = null;
    //获取按钮方法
    public void getButtonss(){
        //创建窗体并设置标题
        frame = new JFrame("图片在按钮上");
        //创建图片容器并赋予图片路径
        ImageIcon icon = new ImageIcon("C:/Users/lenovo/Desktop/pt/stop.jpg");
        //创建按钮
        JButton button = new JButton(icon);
        //设置图片大小
        button.setSize(37,36);
        //使窗体居中
        frame.setLocationRelativeTo(null);

        //窗体大小自定义
        frame.setSize(100,100);
        //点击窗体关闭时同时关闭后台服务
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //使窗体可视化
        frame.setVisible(true);
        //把按钮放进窗体中
        frame.add(button);
    }
    
    public static void main(String[] args){
        IconOnButton iob = new IconOnButton();
        iob.getButtonss();
    }
    
}


  • 5
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
要自定义 Java Swing 的 ConfirmDialog,您需要创建一个新的 JDialog,并在其中添加您需要的组件。您可以添加标签、按和其他控件来创建您希望的外观和行为。 以下是一个简单的示例,展示如何创建一个自定义 ConfirmDialog: ```java import javax.swing.*; import java.awt.*; import java.awt.event.*; public class CustomConfirmDialog extends JDialog { private boolean answer; public CustomConfirmDialog(JFrame parent, String title, String message) { super(parent, title, true); JLabel label = new JLabel(message); JButton yesButton = new JButton("Yes"); JButton noButton = new JButton("No"); yesButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { answer = true; dispose(); } }); noButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { answer = false; dispose(); } }); JPanel panel = new JPanel(new GridLayout(2, 1)); panel.add(label); JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER)); buttonPanel.add(yesButton); buttonPanel.add(noButton); panel.add(buttonPanel); getContentPane().add(panel, BorderLayout.CENTER); setDefaultCloseOperation(DISPOSE_ON_CLOSE); pack(); setLocationRelativeTo(parent); setVisible(true); } public boolean getAnswer() { return answer; } } ``` 这个例子创建了一个新的 JDialog,其中包含了一个文本标签和两个按(“Yes”和“No”)。当用户单击其中一个按时,JDialog 会关闭,并且您可以使用 getAnswer() 方法来获取用户的响应(true 表示用户单击了“Yes”按,false 表示用户单击了“No”按)。 要使用这个自定义 ConfirmDialog,您可以像下面这样调用它: ```java CustomConfirmDialog dialog = new CustomConfirmDialog(frame, "Confirm", "Are you sure you want to continue?"); if (dialog.getAnswer()) { // User clicked "Yes" } else { // User clicked "No" } ``` 这里的 frame 是您的 JFrame,它是新 JDialog 的父级。您可以根据需要使用不同的窗体或面板作为您的父级。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值