簡體   English   中英

如何在 MATLAB GUI 中制作一個簡單的無邊框按鈕

[英]How to make a simple borderless button in MATLAB GUI

很簡單,我正在嘗試在 MATLAB GUI 中創建一個無邊框按鈕。 原因主要是美學,所以不需要爭論為什么它應該是無邊界的。

我已經知道這不能單獨使用內置的 MATLAB uicontrol 來完成,因為按鈕的邊框在 MATLAB 中不是可訪問的屬性。 因此,必須訪問底層 JAVA 代碼(在其上編寫 MATLAB)才能操作邊界。 這是我迷路的地方,因為我只在 MATLAB 中編程過。

我從這里遵循了一個例子: http : //undocumentedmatlab.com/blog/borderless-button-used-for-plot-properties

但我仍然沒有得到一個無邊界按鈕。

這是一個簡單的代碼示例(注意使用 Yair Altman 的 findjobj,它可在 MATLAB 文件交換中找到):

f=figure('Menubar','none', 'Position',[200 200 300 200]);
p=uipanel(f, 'BackgroundColor', [0 0 1]);
h = uicontrol('parent', p, ...
'Style','pushbutton', ...
'String','click', ...
'TooltipString', 'you should click this' ...
'Units','normalized', ...
'Position',[0.3 0.3 0.5 0.5], ...
'BackgroundColor', [0 0 1]);
jh = findjobj(h);
jh.setBorder(javax.swing.BorderFactory.createEmptyBorder()); 
%attempt 1 does not remove border
jh.border=[];                                                
%attempt 2 does not remove border
jh.setBorder([]);                                            
%attempt 3 does not remove border
jh.border=javax.swing.BorderFactory.createEmptyBorder();     
%attempt 4 does not remove border

關於我哪里出錯的任何想法? 謝謝!

您應該添加兩行:

jh.setBorderPainted(false);    
jh.setContentAreaFilled(false);

我不清楚你所說的“無邊界”是什么意思。

查看您發布的網頁上的示例,我假設您正在尋找類似“隱形”按鈕的東西。

如果是這樣,您可以考慮這種替代方式:

  • 安裝了一個按鈕,你可能有一個static text uicontrol
  • 使其backgroundcolor與 GUI 背景顏色相同(它將變得“不可見”並且沒有任何邊框)
  • 不要在static text uicontrol設置任何string
  • static text uicontrolenable屬性設置為off
  • static text uicontrol定義ButtonDownFcn
  • 通過按下ButtonDownFcn的按鈕編寫要執行的代碼

當您在“不可見” static text uicontrol上按下鼠標按鈕時,其ButtonDownFcn將被執行。

你只需要記住...... static text uicontrol是“不可見”的static text uicontrol

希望這可以幫助。

邊框受飛越外觀特征的影響。 http://undocumentedmatlab.com/blog/undocumented-button-highlighting你需要添加

jh.setFlyOverAppearance(true);

為我工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM