JTextFieldUI 在 JTabbedPane 的另一个选项卡中绘制,具有我个人的外观和感觉

我正在开发新的外观和感觉,现在在 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());

        }



www说
浏览 153回答 1
1回答

梵蒂冈之花

问题在于你如何操纵绘画。在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 fieldprotected 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;    }}@Overridepublic 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());    }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java