为了账号安全,请及时绑定邮箱和手机立即绑定

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 关注
  • 150 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号