【问题标题】:Swing and inheritance摇摆与继承
【发布时间】:2011-01-04 14:05:18
【问题描述】:

我有两个类,一个扩展另一个。在他们两个中,我都有代表摆动组件的字段。问题是当我尝试使用子类中的组件时出现问题 - 似乎 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;
  }

【问题讨论】:

  • 没有这样的限制。唯一的限制是必须在 EDT 上完成对 GUI 的所有更改。你在做这个吗?
  • 我的GUI初始化是这样的: if(EventQueue.isDispatchThread()) { initialize(); } else { 尝试 { EventQueue.invokeAndWait(new Runnable() { @Override public void run() { initialize(); } }); } catch (InterruptedException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } }
  • getJTextPane() 做什么/返回,在哪里定义? getJTextPane2() 也一样。
  • 你怎么知道子初始化不在 EDT 中运行?什么调试器告诉你的?
  • 您可以更好地将所有代码移动到 EDT,因此您在 main 中所做的唯一事情就是“切换”到 EDT。并发很难,所以保持简单。很高兴你解决了它。

标签: java multithreading swing


【解决方案1】:

Emm...我可以看到您没有将此字段初始化为

protected JPanel jContentPane = null;

我猜你应该修改域代码为

protected JPanel jContentPane = new JPanel();

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

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

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

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

祝你好运:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-03
    • 2019-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-30
    • 2015-06-20
    相关资源
    最近更新 更多