2012-10-08 62 views
0

我正在使用NetBeans,並且我有一個擴展到JDialog的類,在該對話框中創建了一個選項卡式窗格。標籤式窗格包含6個不同的標籤,當然還有6個不同的標籤。我想要做的是當我點擊不同的標籤時,圖表應該用paint方法繪製。與選項卡式窗格一起使用JDialog繪製不同的圖片

我的問題是,如何在不同的面板上繪製不同的面板,只需要在Dialog類中調用另一個類中的一個繪製方法?

這裏是我的對話框類代碼:

package GUI;  

public class NewJDialog extends javax.swing.JDialog{ 

    /** 
    * Creates new form NewJDialog 
    */ 
    public NewJDialog(java.awt.Frame parent, boolean modal) { 
     super(parent, modal); 
     initComponents(); 
    } 

    /** 
    * 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() { 

     jTabbedPane1 = new javax.swing.JTabbedPane(); 
     jPanel1 = new javax.swing.JPanel(); 
     jPanel2 = new javax.swing.JPanel(); 
     jPanel3 = new javax.swing.JPanel(); 
     jPanel4 = new javax.swing.JPanel(); 
     jPanel5 = new javax.swing.JPanel(); 
     jPanel6 = new javax.swing.JPanel(); 
     jPanel7 = new javax.swing.JPanel(); 
     jLabel1 = new javax.swing.JLabel(); 
     jLabel2 = new javax.swing.JLabel(); 

     setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); 

     javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); 
     jPanel1.setLayout(jPanel1Layout); 
     jPanel1Layout.setHorizontalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 466, Short.MAX_VALUE) 
     ); 
     jPanel1Layout.setVerticalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 242, Short.MAX_VALUE) 
     ); 

     jTabbedPane1.addTab("FCFS", jPanel1); 

     javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2); 
     jPanel2.setLayout(jPanel2Layout); 
     jPanel2Layout.setHorizontalGroup(
      jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 466, Short.MAX_VALUE) 
     ); 
     jPanel2Layout.setVerticalGroup(
      jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 242, Short.MAX_VALUE) 
     ); 

     jTabbedPane1.addTab("SSTF", jPanel2); 

     javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3); 
     jPanel3.setLayout(jPanel3Layout); 
     jPanel3Layout.setHorizontalGroup(
      jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 466, Short.MAX_VALUE) 
     ); 
     jPanel3Layout.setVerticalGroup(
      jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 242, Short.MAX_VALUE) 
     ); 

     jTabbedPane1.addTab("LOOK", jPanel3); 

     javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4); 
     jPanel4.setLayout(jPanel4Layout); 
     jPanel4Layout.setHorizontalGroup(
      jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 466, Short.MAX_VALUE) 
     ); 
     jPanel4Layout.setVerticalGroup(
      jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 242, Short.MAX_VALUE) 
     ); 

     jTabbedPane1.addTab("LOOK C", jPanel4); 

     javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5); 
     jPanel5.setLayout(jPanel5Layout); 
     jPanel5Layout.setHorizontalGroup(
      jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 466, Short.MAX_VALUE) 
     ); 
     jPanel5Layout.setVerticalGroup(
      jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 242, Short.MAX_VALUE) 
     ); 

     jTabbedPane1.addTab("SCAN", jPanel5); 

     javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6); 
     jPanel6.setLayout(jPanel6Layout); 
     jPanel6Layout.setHorizontalGroup(
      jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 466, Short.MAX_VALUE) 
     ); 
     jPanel6Layout.setVerticalGroup(
      jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 242, Short.MAX_VALUE) 
     ); 

     jTabbedPane1.addTab("SCAN C", jPanel6); 

     getContentPane().add(jTabbedPane1, java.awt.BorderLayout.CENTER); 

     jLabel1.setText("Distancia:"); 

     jLabel2.setText("___________"); 

     javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7); 
     jPanel7.setLayout(jPanel7Layout); 
     jPanel7Layout.setHorizontalGroup(
      jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(jPanel7Layout.createSequentialGroup() 
       .addGap(21, 21, 21) 
       .addComponent(jLabel1) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
       .addComponent(jLabel2) 
       .addContainerGap(331, Short.MAX_VALUE)) 
     ); 
     jPanel7Layout.setVerticalGroup(
      jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(jPanel7Layout.createSequentialGroup() 
       .addContainerGap() 
       .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
        .addComponent(jLabel1) 
        .addComponent(jLabel2)) 
       .addContainerGap(15, Short.MAX_VALUE)) 
     ); 

     getContentPane().add(jPanel7, java.awt.BorderLayout.PAGE_START); 

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

    /** 
    * @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(NewJDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (InstantiationException ex) { 
      java.util.logging.Logger.getLogger(NewJDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (IllegalAccessException ex) { 
      java.util.logging.Logger.getLogger(NewJDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
      java.util.logging.Logger.getLogger(NewJDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } 
     //</editor-fold> 

     /* Create and display the dialog */ 
     java.awt.EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       NewJDialog dialog = new NewJDialog(new javax.swing.JFrame(), true); 
       dialog.addWindowListener(new java.awt.event.WindowAdapter() { 
        @Override 
        public void windowClosing(java.awt.event.WindowEvent e) { 
         System.exit(0); 
        } 
       }); 
       dialog.setVisible(true); 
      } 
     }); 
    } 
    // Variables declaration - do not modify      
    private javax.swing.JLabel jLabel1; 
    private javax.swing.JLabel jLabel2; 
    private javax.swing.JPanel jPanel1; 
    private javax.swing.JPanel jPanel2; 
    private javax.swing.JPanel jPanel3; 
    private javax.swing.JPanel jPanel4; 
    private javax.swing.JPanel jPanel5; 
    private javax.swing.JPanel jPanel6; 
    private javax.swing.JPanel jPanel7; 
    private javax.swing.JTabbedPane jTabbedPane1; 
    // End of variables declaration 
} 

這是我的paint方法創建的另一個類:我希望你們明白我想

import java.awt.Graphics; 
import javax.swing.JPanel; 

public class Lienzo { 

    private int width = 5; 
    private int height = 5; 
    private int y = 5; 
    private int x = 0; 
    private int x1 = 0; 

    public Graphics Draw(Graphics g, int[] pistas) { 
     //Im not sure if this is the correct way to do it 
     //The diagram gets drawn according to values from an array 
     //The array is not always the same thats why I used the different Panels 

     for (int i = 0; i < pistas.length; i++) { 
      x = pistas[i]; 
      x1 = pistas[i + 1]; 

      g.drawOval(x, y, width, height); 
      g.drawString(Integer.toString(x), x, y); 
      g.drawLine(x, y, x1, y); 

     } 
     return g; 
    } 
} 

用我的程序。

回答

3

我會製作一個類似於JPanel的「GraphPane」。在這個類中,我將提供一種方法,通過它可以提供圖形的陣列信息。我會採用draw方法中的邏輯,並在GraphPanepaintComponent方法內部添加。

然後,我會將這個GraphPane的一個實例添加到每個選項卡,爲每個選項卡填入適當的數組數據。

根據數據來自何處,當創建數據時,您可能還需要將陣列數據傳遞給對話框

相關問題