亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

JTextFieldUI 在 JTabbedPane 的另一個選項卡中繪制,具有我個人的外觀和感覺

JTextFieldUI 在 JTabbedPane 的另一個選項卡中繪制,具有我個人的外觀和感覺

www說 2023-08-09 16:57:33
我正在開發新的外觀和感覺,現在在 JTabbledPane 組件中使用組件 JtextField 時遇到了一個錯誤。我的 JTextFieldUI 有這段代碼,它擴展了 BasicLookAndFell另外,這是我的最小可重現示例public class DemoLookAndFeel extends JFrame {? ? static {? ? ? ? try {? ? ? ? ? ? ?UIManager.setLookAndFeel(new MyLookAndFeel());? ? ? ? } catch (UnsupportedLookAndFeelException ex) {? ? ? ? }? ? }? ? public void init() {? ? ? ? JPanel panelOne = new JPanel();? ? ? ? panelOne.add(new JTextField("write inside me and change the tab"));? ? ? ? JPanel panelTwo = new JPanel();? ? ? ? //panelTwo.add(new Label("Now seee the JTextField?"));? ? ? ? JTabbedPane tabbedPane = new JTabbedPane();? ? ? ? tabbedPane.add("One", panelOne);? ? ? ? tabbedPane.add("Two", panelTwo);? ? ? ? this.setContentPane(tabbedPane);? ? ? ? this.setSize(800,800);? ? ? ? this.pack();? ? ? ? this.setLocationRelativeTo(null);? ? ? ? this.setVisible(true);? ? }? ? private static class MyLookAndFeel extends BasicLookAndFeel {? ? ? ? @Override? ? ? ? public String getName() {? ? ? ? ? ? return "my look and feel";? ? ? ? }? ? ? ? @Override? ? ? ? public String getID() {? ? ? ? ? ? return "qwerty";? ? ? ? }? ? ? ? @Override? ? ? ? public String getDescription() {? ? ? ? ? ? return "";? ? ? ? }? ? ? ? @Override? ? ? ? public boolean isNativeLookAndFeel() {? ? ? ? ? ? return false;? ? ? ? }? ? ? ? @Override? ? ? ? public boolean isSupportedLookAndFeel() {? ? ? ? ? ? return true;? ? ? ? }? ? ? ? @Override? ? ? ? protected void initClassDefaults(UIDefaults table) {? ? ? ? ? ? super.initClassDefaults(table);?? ? ? ? ? ? ? table.put("TextFieldUI", MyPersonalFieldUI.class.getCanonicalName());? ? ? ? }
查看完整描述

1 回答

?
梵蒂岡之花

TA貢獻1900條經驗 獲得超5個贊

問題在于你如何操縱繪畫。在changeColorOnFocus(由 調用FocusListener)方法內部有以下幾行:


if (c.getGraphics() != null) {

? ? c.paint(c.getGraphics());

}

在這里,您使用圖形并在負責組件繪畫層次結構中繪畫的方法之外進行一種自定義繪畫。我希望我能更好地解釋它,但長話短說,Graphics只使用將它們作為參數提供給您的方法(并調用其super方法)。在UI類中方法是paintSafely,在組件中是paintComponent。它們都給你Graphics對象,因此它們適合定制繪畫。


因此,解決方案是在您的內部FocusListener添加一個標志,以便了解組件是否獲得焦點,并調用changeColorOnFocus內部paintSafely(Graphics g)方法。您的 UI 類應該在以下部分中進行更改(我不會全部粘貼,因為它有點大)。


private boolean focused; //a field

protected class FocusListenerColorLine implements FocusListener {


? ? @Override

? ? public void focusGained(FocusEvent e) {

? ? ? ? firePropertyChange(PROPERTY_LINE_COLOR, colorLineInactive, colorLineActive);

? ? ? ? focused = true;

? ? }


? ? @Override

? ? public void focusLost(FocusEvent e) {

? ? ? ? firePropertyChange(PROPERTY_LINE_COLOR, colorLineActive, colorLineInactive);

? ? ? ? focused = false;

? ? }

}




@Override

public void paintSafely(Graphics g) {

? ? super.paintSafely(g);

? ? paintLine(g);

? ? changeColorOnFocus(g);

}


protected void changeColorOnFocus(Graphics g) {

? ? boolean hasFocus = focused;

? ? JTextComponent c = getComponent();

? ? if (c == null) {

? ? ? ? return;

? ? }

? ? if (hasFocus && (activeBackground != null) && (activeForeground != null)) {

? ? ? ? logicForChangeColorOnFocus(c, activeBackground, activeForeground);

? ? ? ? //TODO create a new changePropriety

? ? ? ? paintLine(c.getGraphics());

? ? }


? ? if (!hasFocus && (inactiveBackground != null) && (inactiveForeground != null)) {

? ? ? ? logicForChangeColorOnFocus(c, inactiveBackground, inactiveForeground);

? ? ? ? paintLine(c.getGraphics());

? ? }

}


查看完整回答
反對 回復 2023-08-09
  • 1 回答
  • 0 關注
  • 108 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號