Custom Matlab pushbutton appearance with Java

耗尽温柔 提交于 2019-12-11 00:32:35

问题


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.


回答1:


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."




回答2:


That should probably be Color.red or Color.RED.


Note that the BorderFactory method used there was only introduced in Java 1.7 (recently). To make it compatible with versions back to 1.3, use the direct LineBorder(Color,int,boolean) constructor.



来源:https://stackoverflow.com/questions/9242460/custom-matlab-pushbutton-appearance-with-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!