繁体   English   中英

摇摆与继承

[英]Swing and inheritance

我有两个班级,一个班级扩展了另一个班级。 在它们两个中,我都有代表挥杆组件的字段。 问题是当我尝试使用子类中的组件时出现了问题-似乎Swing线程无法访问子组件。 我究竟做错了什么?

public abstract class AppViewBase {
    protected JPanel jContentPane = null;

    protected void initialize() {
        //...
        addOutputControlls(jContentPane);
        //...
    }
    protected abstract void addOutputControlls(JPanel jContentPane) ;
}

public class Titrai extends AppViewBase  {
    public JTextPane line1 = null;
    public JTextPane line2 = null;

    protected void addOutputControlls(JPanel jContentPane2) {
        jContentPane2.add(getJTextPane());
        jContentPane2.add(getJTextPane2());
    }

    public void setCurrentLine(Object selectedValue) {
        String s = (String) selectedValue;
        getJTextPane().setText(s);
        getJTextPane2().setText("");

        getJTextPane().repaint();
        //only gets repainted i i move line1, line2 fields to parent class
        getJTextPane2().repaint();
    }

}

编辑-注释中的代码

if(EventQueue.isDispatchThread()) { 
  initialize(); 
} else { 
  try { 
    EventQueue.invokeAndWait(new Runnable() { 
       @Override 
       public void run() { 
         initialize(); 
       } 
    }); 
  } catch (InterruptedException e) { 
    e.printStackTrace(); 
  } catch (InvocationTargetException e) { 
    e.printStackTrace();
  } 
}

编辑2-OP的其他注释中的代码

  JTextPane getJTextPane() {
     if (line1 == null) {
        line1 = new JTextPane();
        line1.setFont(new Font("Tahoma", Font.PLAIN, 13));
        line1.setForeground(Color.white);
        line1.setPreferredSize(new Dimension(385, 16));
        line1.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, false);
        line1.setEditorKit(getDefaultLineEditorKit());
        line1.setEditable(false);
        line1.setLocation(new Point(15, 15));
        line1.setSize(new Dimension(385, 16));
        line1.setBackground(Color.black);
        line1.setFocusable(false);
        line1.setBorder(null);
     }
     return line1;
  }

恩...据我所知,您不会像

protected JPanel jContentPane = null;

我想你应该将域代码修改为

protected JPanel jContentPane = new JPanel();

...因为null不是“准备使用”的对象值:)

我的意思是这种方法...

protected void initialize() {
        //...
        addOutputControlls(jContentPane);//<-- contain null 
        //...
    }

...似乎与null面板一起使用,因此初始化未正确完成。

祝好运 :)

暂无
暂无

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

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