我的重新验证/重绘有一些问题

时间:2015-03-31 09:33:23

标签: java swing jpanel refresh panel

我试图用revalidate()& repaint()但它对我不起作用,我搜索了一些更多的解决方案,但每次尝试它都不适合我,有人可以帮我吗? 非常感谢,

这是我的脚本(不完整)

 public JPanel createPanel() throws SQLException, ClassNotFoundException
 {
      FormLayout formlayout1 = new 

    FormLayout("FILL:DEFAULT:NONE,FILL:119PX:NONE,
    FILL:30PX:NONE,FILL:24PX:NONE,FILL:130PX:NONE,
    FILL:16PX:NONE","CENTER:DEFAULT:NONE,CENTER:30PX:NONE,
    CENTER:30PX:NONE,CENTER:25PX:NONE,CENTER:25PX:NONE,
    CENTER:25PX:NONE,CENTER:25PX:NONE,CENTER:25PX:NONE");

          CellConstraints cc = new CellConstraints();
          jpanel1.setLayout(formlayout1);

          JLabel lbl = null;
          JTextArea col = null;
          int i = 3;

         for (OverzichtVo o : OverzichtBusiness.getOverzicht()){
         lbl = new JLabel(mapServices.get(o.getServiceid()));
         jpanel1.add(lbl, cc.xy(2, i));
         col = new JTextArea();
         col.setEditable(false);
         LineBorder lineborder1 = new LineBorder(new Color(0,0,0),2,false);
         col.setBorder(lineborder1);
         col.setBackground(colors[o.getStatusid()-1]);        
         jpanel1.add(col, cc.xy(3, i));
         lbl = new JLabel(o.getExtrainfo());
         jpanel1.add(lbl, cc.xy(5, i));
         i++;

      button.setFont(new Font("Poor Richard",Font.PLAIN,20));
      button.setName("button");
      LineBorder lnbord = new LineBorder(new Color(0,0,0),2,false);
      button.setBorder(lnbord);
      refresh();
      jpanel1.add(button,cc.xy(5,2));

      m_lbloverzicht.setFont(new Font("Poor Richard",Font.PLAIN,30));
      m_lbloverzicht.setName("lbloverzicht");
      m_lbloverzicht.setText("Overzicht");
      LineBorder lineborder1 = new LineBorder(new Color(0,0,0),2,false);
      m_lbloverzicht.setBorder(lineborder1);
      jpanel1.add(m_lbloverzicht,cc.xy(2,2));

      addFillComponents(jpanel1,new int[]{ 1,2,3,4,5,6 },new int[]{ 1,2,3,4,5,6,7,8 });
      return jpanel1;
 }
 private void refresh(){
    button.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e)
        {
            jpanel1.revalidate();
         jpanel1.repaint();
        }
    });
}

我在另一个类中激活它:

public static void main(String args[]) throws SQLException, ClassNotFoundException{
    JFrame frame = new JFrame();
    AlgemeneFrm frm_alg = new AlgemeneFrm();
    frame.add(frm_alg);
    frame.setVisible(true); 
    frame.setSize(350,250); 
    frame.addWindowListener(new WindowAdapter()
    {
        public void windowClosing(WindowEvent evt)
        {
            System.exit(0);
        }
    });
}

我正在尝试刷新我的整个Jpanel /面板,因为我的Jtextarea(背景)是绿色的,当我改变某些东西时,它会变成红色。如果我停止应用程序并再次启动它,这对我来说很好,但我添加了一个按钮,所以我可以刷新/更新我的应用程序或只是面板。但它不适用于'panelname'.revalidate();和'panelname'.repaint();中的'refresh'按钮。 请帮忙

0 个答案:

没有答案