Java按钮的自定义

package s003;

import java.awt.*;

import javax.swing.*;

重写按钮类
public class RoundButton extends JButton {

    public RoundButton(String label) {
        super(label);  //调用父类构造函数
        setContentAreaFilled(false);   //不自行绘制按钮背景
    }


    //绘制圆和标签
    protected void paintComponent(Graphics g) {
       if (getModel().isArmed()) {  //鼠标点击时
            g.setColor(Color.lightGray);  //颜色为灰色
        } else {
            g.setColor(getBackground());  //置按钮颜色
        }
        g.fillOval(0, 0, getSize().width, getSize().height);  //绘制圆
        super.paintComponent(g);  //调用父类函数,绘制其余部分
    }


    //绘制边框
   protected void paintBorder(Graphics g) {
       g.setColor(getForeground());  //设置边框颜色
       g.drawOval(0, 0, getSize().width-1, getSize().height-1);  //在边界上绘制一个椭圆
   }

}

package s003;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class RoundButtonDemo extends JFrame{

 private int clickCount=0;  //记录安钮的点击次数
 private JButton button1;
 private JButton button2;

 public RoundButtonDemo()
 {
  button1 = new RoundButton("这是一个圆形按钮");  //初始化按钮一

        Dimension dim=button1.getPreferredSize();  //得到按钮一的最佳尺寸
        double maxsize=Math.max(dim.getHeight(),dim.getWidth());  //得到长宽中的最大值
        dim.setSize(maxsize,maxsize);  //更改长宽为长宽中的最大值
        button1.setPreferredSize(dim);  //设置最佳尺寸

        button2 = new RoundButton("点击了: "+clickCount+" 次");  //初始化按钮二
        button1.setBackground(Color.blue);  //设置按钮的背景颜色
        button2.setBackground(Color.pink);
        getContentPane().add(button1);  //增加组件
        getContentPane().add(button2);
        getContentPane().setLayout(new FlowLayout());  //设置布局管理器

        button2.addActionListener(new ActionListener(){  //铵钮二的事件处理
            public void actionPerformed(ActionEvent e){
                clickCount++;  //增加一次点击数
                button2.setText("点击了: "+clickCount+" 次");  //重新设置按钮二的标签
            }
        });

        setSize(300, 200);  //设置窗口尺寸
        setVisible(true);  //设置窗口可视
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  //关闭窗口时退出程序
 }

    public static void main(String[] args) {
  new RoundButtonDemo();
    }
}

 

调用的示例演示

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要自定义 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 的父级。您可以根据需要使用不同的窗体或面板作为您的父级。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值