java怎么改变按钮外观,Java自定义Matlab按钮外观

I want to change the appearance of a Matlab pushbutton with Java. For it, I use the application Findjobj. However, because of I do not know Java, I have troubles to use Java classes properly that I need to get my button has rounded corners. Based on the information found here), I have tried to set up the button with the class BorderFactory:

hButton = uicontrol('string','click me!');

jButton = findjobj(hButton);

jButton.setCursor(java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));

jButton.setBorder(BorderFactory.createLineBorder(red,1,true));

But this does not work. I got the following error message:

??? Undefined function or variable 'red'.

Error in ==> de at 4

jButton.setBorder(BorderFactory.createLineBorder(red,1,true));

EDIT: I realize I forgot to include the Java classes for color and LineBorder. After fixing it, the code looks like this:

import java.awt.Color;

import javax.swing.border.LineBorder;

hButton = uicontrol('Style','pushbutton','String','click me!',...

'Units','normalized','Position',[0.156 0.64 0.688 0.1],'FontSize',9,...

'ForegroundColor','w','BackgroundColor','k');

jButton = findjobj(hButton);

jButton.setCursor(java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));

jButton.setBorder(LineBorder(Color.white,5,true));

However, the result still be unpleased for me. Here is the final appearance. I can get the corners to be rounded.

解决方案

If you want rounded buttons, you'll have to use a Look & Feel whose button UI delegate draws rounded buttons. Examples include com.apple.laf.AquaButtonUI, illustrated here, and javax.swing.plaf.synth.SynthLookAndFeel, shown here. You can also implement your own ButtonUI and Border, as shown here, but you'll have decide if it's worth the effort.

Addendum: Looking at this related Matlab question and the relevant Swing tutorial, note that "If you are going to set the L&F, you should do it as the very first step in your application."

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值