简体   繁体   English

重新实现paintComponent(Graphics g)

[英]Reimplement paintComponent(Graphics g)

Is possible change implementation of paintComponent(Graphics g) method of created component? 是否可以更改所创建组件的paintComponent(Graphics g)方法的实现?

Usecase: (Simplified for 2 components) 用例:(简化为2个组件)
1. Create 2 components JLabel jlabel1, JLabel jlabel2 in some black box. 1.在某个黑框中创建2个组件JLabel jlabel1和JLabel jlabel2。
2. Put them in to HashMap<String, Component> components , where first argument is id, and second are our components jLabel1, jlabel2. 2.将它们放入HashMap<String, Component> components ,其中第一个参数是id,第二个是我们的组件jLabel1,jlabel2。
This part is blackbox . 这部分是黑盒 Components are provided in this HashMap. 该HashMap中提供了组件。
3. Load HashMap components, go over the components and get them from the map. 3.加载HashMap组件,遍历这些组件并从地图上获取它们。
4. Here I need change paining method of provided componets from HashMap. 4. 在这里,我需要 HashMap提供的组件的更改痛苦方法。


Note: Change in this case means rotation. 注意:这种情况下的更改意味着旋转。
Note 2: I know how to do rotation. 注2:我知道怎么做旋转。 Problem is how to reimplement paint(resp paintComponent) method of already existing component to be able apply the code which will do the rotation . 问题是如何重新实现已经存在的组件的paint(resp paintComponent)方法才能应用执行旋转的代码 When components are provided as blackbox in hashmap. 在哈希图中将组件作为黑盒提供时。

IS THAT POSSIBLE? 那可能吗?

You can create your own MyLabel extends JLabel . 您可以创建自己的MyLabel extends JLabel And override method paintComponent(Graphics g) . 并重写方法paintComponent(Graphics g) In your MyLabel component you can add flag to rotate or not label. 在MyLabel组件中,您可以添加标志以旋转或不标记。

You can create a new class which extends a component and override the component's method in this class. 您可以创建一个扩展组件的新类,并在此类中重写该组件的方法。 Ex: 例如:

public class Label extends JLabel {
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
    }
}

Or, you can create a component and override its methods directly, ex: 或者,您可以创建一个组件并直接覆盖其方法,例如:

JLabel l = new JLabel() {
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
    }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM