2014-04-26 69 views
0

我在Netbeans中製作了一個圖形界面,您可以在文本框「punten」中放入一系列數字(例如:7 8 5 4 10 13),並且當您按下「ververs」按鈕時,所有數字的圖形線條圖應該出現(在我的面板中)。我創建了一個類「Gui」,它擴展了JFrame中的Textfield,按鈕和一個面板。我還創建了一個擴展JPanel的「Grafiek」類,並與我的「Gui」中的面板相關聯。重繪不起作用,我的私人領域的價值不會改變?

我遇到的問題是:repaint();命令不會去paintComponent(Graphics g) - 方法,我的私有變量不會改變(punt和punti的長度保持爲0)。

有人可以幫助我,我一直在這個項目上工作了好幾天。

我的GUI類:

import java.awt.Graphics; 

public class Gui extends javax.swing.JFrame { 

public Gui() { 
    initComponents(); 
    panel = new javax.swing.JPanel(); 
} 

/** 
* This method is called from within the constructor to initialize the form. 
* WARNING: Do NOT modify this code. The content of this method is always 
* regenerated by the Form Editor. 
*/ 
@SuppressWarnings("unchecked") 
// <editor-fold defaultstate="collapsed" desc="Generated Code"> 

private void initComponents() { 

    punten = new javax.swing.JTextField(); 
    fout = new javax.swing.JLabel(); 
    javax.swing.JButton ververs = new javax.swing.JButton(); 
    panel = new Grafiek(); 

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

    fout.setText("j"); 

    ververs.setText("Ververs"); 
    ververs.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      verversActionPerformed(evt); 
     } 
    }); 

    panel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 2)); 

    javax.swing.GroupLayout panelLayout = new javax.swing.GroupLayout(panel); 
    panel.setLayout(panelLayout); 
    panelLayout.setHorizontalGroup(
     panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGap(0, 0, Short.MAX_VALUE) 
    ); 
    panelLayout.setVerticalGroup(
     panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGap(0, 195, Short.MAX_VALUE) 
    ); 

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
    getContentPane().setLayout(layout); 
    layout.setHorizontalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() 
      .addContainerGap() 
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) 
       .addComponent(panel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
       .addComponent(punten, javax.swing.GroupLayout.Alignment.LEADING) 
       .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup() 
        .addComponent(ververs) 
        .addGap(6, 6, 6) 
        .addComponent(fout) 
        .addGap(0, 302, Short.MAX_VALUE))) 
      .addContainerGap()) 
    ); 
    layout.setVerticalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(layout.createSequentialGroup() 
      .addContainerGap() 
      .addComponent(punten, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
      .addGap(18, 18, 18) 
      .addComponent(panel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
      .addGap(18, 18, 18) 
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
       .addComponent(fout) 
       .addComponent(ververs)) 
      .addContainerGap()) 
    ); 

    pack(); 
}// </editor-fold>       

private void verversActionPerformed(java.awt.event.ActionEvent evt) {           
    Grafiek graf = new Grafiek(); 
    graf.verwerkData(punten.getText()); 
}          

/** 
* @param args the command line arguments 
*/ 
public static void main(String args[]) { 
    /* Set the Nimbus look and feel */ 
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> 
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. 
    * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
    */ 
    try { 
     for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { 
      if ("Nimbus".equals(info.getName())) { 
       javax.swing.UIManager.setLookAndFeel(info.getClassName()); 
       break; 
      } 
     } 
    } catch (ClassNotFoundException ex) { 
     java.util.logging.Logger.getLogger(Gui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (InstantiationException ex) { 
     java.util.logging.Logger.getLogger(Gui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (IllegalAccessException ex) { 
     java.util.logging.Logger.getLogger(Gui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
     java.util.logging.Logger.getLogger(Gui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } 
    //</editor-fold> 

    /* Create and display the form */ 
    java.awt.EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      new Gui().setVisible(true); 
     } 
    }); 
} 

// Variables declaration - do not modify      
private javax.swing.JLabel fout; 
private javax.swing.JPanel panel; 
private javax.swing.JTextField punten; 
// End of variables declaration     
} 

而我的 「Grafiek」 級:

import java.awt.Graphics; 

public class Grafiek extends javax.swing.JPanel { 

private String[] punt; 
private int[] punti; 
private int afstandX, afstandY, puntX1=0, puntY1=0, puntX2=0, puntY2=0; 
private int max=1; 

/** 
* Creates new form Grafiek 
*/ 
public Grafiek() { 
    initComponents(); 
} 

@Override 
public void paintComponent(Graphics g) { 
    super.paintComponent(g); 

    try{ 
     for(int i=0; i<punti.length; i++) { 
      if(max <= punti[i]) { 
       max = punti[i]; 
      } 
     } 

     afstandX = getWidth()/punt.length; 
     afstandY = getHeight()/max; 

     for(int i=0; i<punti.length; i++) { 
      puntX1 = puntX2; 
      if(i == 0) { 
       puntY1 = getHeight(); 
      } 
      else puntY1 = puntY2; 
      puntX2 += afstandX; 
      puntY2 = getHeight() - punti[i]*afstandY; 

      g.drawLine(puntX1, puntY1, puntX2, puntY2); 
     } 

     puntX2 = 0; 
     puntY2 = 0; 
    }catch(java.lang.NullPointerException npe) { 
     super.paintComponent(g); 
    } 
} 

public void verwerkData(String s) { 
    punt = s.split(" "); 

    punti = new int[punt.length]; 

    for(int i=0; i<punt.length; i++) { 
     punti[i] = Integer.parseInt(punt[i]); 
    } 

    repaint(); 
} 

/** 
* This method is called from within the constructor to initialize the form. 
* WARNING: Do NOT modify this code. The content of this method is always 
* regenerated by the Form Editor. 
*/ 
@SuppressWarnings("unchecked") 
// <editor-fold defaultstate="collapsed" desc="Generated Code">       
private void initComponents() { 

    setBackground(new java.awt.Color(255, 255, 255)); 

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); 
    this.setLayout(layout); 
    layout.setHorizontalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGap(0, 400, Short.MAX_VALUE) 
    ); 
    layout.setVerticalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGap(0, 300, Short.MAX_VALUE) 
    ); 
}// </editor-fold>       

} 

回答

0

,因爲你把你指向錯誤的對象的代碼無法正常工作。

首先,你panel對象Grafiek型,所有它首先在方法initComponents()初始化,然後在構造函數中再次改寫的第二個不行。

第二個問題是,在動作偵聽器調用的方法verversActionPerformed中,創建了一個新的Grafiek實例,該實例顯然不是您之前創建/添加的實例。

因此,爲了使其工作改變代碼如下:

的構造看起來應該如下:

public Gui() { 
    initComponents(); 
    //panel = new javax.swing.JPanel(); 
} 

這樣的方法:

private void verversActionPerformed(java.awt.event.ActionEvent evt) {           
    Grafiek graf = (Grafiek)panel; 
    graf.verwerkData(punten.getText()); 
} 

因此,它的工作原理如你所料。

但是,這是遠離良好的代碼。您應該將變量panel的類型設置爲正確的類型Grafiek