|
|
|
![]() |
|
Strumenti |
![]() |
#1 |
Senior Member
Iscritto dal: Jan 2006
Città: Perugia - San Benedetto del Tronto
Messaggi: 348
|
[Java]Update di un JPanel
Salve a tutti, ho un problema da cui non riesco a schiodarmi. Offro una birra a chi mi aiuterà a risolverlo
![]() Mi trovo a che fare con due classi: 1- classe MainFrame, creata con l'aiuto grafico di NetBeans. 2- classe PanelDifZon, creata da me. Questo disegno può aiutarvi a capire meglio: ![]() Il mio PanelDifZon è inserito a suo volta all'interno di un JPanel creato con NetBeans chiamato PanelEsterno. Siccome stò ancora agli inizi ho semplicemente implementato la creazione del mio JPanel con parametri settati di default tramite una riga di codice all'interno della classe MainFrame: Codice:
difZon = new PanelDifZon(funzione1,funzione2,2,2,"funzione2"); Senza stare ad entrare troppo nel dettaglio, quello è il risultato della differenza di due figure geometriche, nello specifico R1-R2 (notare l'ultimo parametro passato al costruttore di difZon, questo specifica infatti il sottraendo). Il problema nasce nel momento in cui l'utente preme sul pulsante "R2-R1" in quando il grafico deve essere completamente ridisegnato. Così ho aggiunto all'actionperformed del bottone in questione le seguenti righe di codice: Codice:
PanelDifZon difZon_nuovo = new PanelDifZon(funzione1, funzione2, 2, 2, "funzione1"); panelEsterno.removeAll(); panelEsterno.add(difZon_nuovo); panelEsterno.revalidate(); panelEsterno.repaint(); ![]() Qualcuno può aiutarmi a capire dove sbaglio? ![]() |
![]() |
![]() |
![]() |
#2 |
Senior Member
Iscritto dal: Nov 2004
Città: Tra Verona e Mantova
Messaggi: 4553
|
L'idea è ok. Il problema con tutta probabilità è nel layout manager di panelEsterno. Che tipo di LayoutManager usa?
__________________
Uilliam Scecspir ti fa un baffo? Gioffri Cioser era uno straccione? E allora blogga anche tu, in inglese come me! |
![]() |
![]() |
![]() |
#3 |
Senior Member
Iscritto dal: Jan 2006
Città: Perugia - San Benedetto del Tronto
Messaggi: 348
|
Ti posto direttamente tutto l'initComponent generato di default dal NetBeans. Tieni presente però che in un altro progetto, sempre usando NetBeans e un JPanel da me creato, tutto funzionava correttamente
![]() Codice:
private void initComponents() { jPanel1 = new javax.swing.JPanel(); panelEsterno = new javax.swing.JPanel(); difZon = new PanelDifZon(funzione1,funzione2,2,2,"funzione2"); jLabel1 = new javax.swing.JLabel(); jPanel3 = new javax.swing.JPanel(); jLabel2 = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); r1label = new javax.swing.JLabel(); r2label = new javax.swing.JLabel(); jPanel4 = new javax.swing.JPanel(); jCheckBox1 = new javax.swing.JCheckBox(); jCheckBox2 = new javax.swing.JCheckBox(); jCheckBox3 = new javax.swing.JCheckBox(); jLabel4 = new javax.swing.JLabel(); jCheckBox4 = new javax.swing.JCheckBox(); jCheckBox5 = new javax.swing.JCheckBox(); jButton1 = new javax.swing.JButton(); jButton2 = new javax.swing.JButton(); jLabel5 = new javax.swing.JLabel(); jMenuBar1 = new javax.swing.JMenuBar(); jMenu1 = new javax.swing.JMenu(); jMenu2 = new javax.swing.JMenu(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setBackground(new java.awt.Color(255, 255, 255)); jPanel1.setBackground(new java.awt.Color(204, 204, 255)); panelEsterno.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED)); difZon.setPreferredSize(new java.awt.Dimension(600, 601)); org.jdesktop.layout.GroupLayout difZonLayout = new org.jdesktop.layout.GroupLayout(difZon); difZon.setLayout(difZonLayout); difZonLayout.setHorizontalGroup( difZonLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(0, 602, Short.MAX_VALUE) ); difZonLayout.setVerticalGroup( difZonLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(0, 601, Short.MAX_VALUE) ); org.jdesktop.layout.GroupLayout panelEsternoLayout = new org.jdesktop.layout.GroupLayout(panelEsterno); panelEsterno.setLayout(panelEsternoLayout); panelEsternoLayout.setHorizontalGroup( panelEsternoLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(0, 622, Short.MAX_VALUE) .add(panelEsternoLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(panelEsternoLayout.createSequentialGroup() .addContainerGap() .add(difZon, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 602, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))) ); panelEsternoLayout.setVerticalGroup( panelEsternoLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(0, 623, Short.MAX_VALUE) .add(panelEsternoLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(panelEsternoLayout.createSequentialGroup() .addContainerGap() .add(difZon, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))) ); jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18)); jLabel1.setText("DIFFERENZA TRA DUE ZONOIDI"); jPanel3.setBackground(new java.awt.Color(204, 204, 255)); jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "f(x) , f'(x)", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION)); jLabel2.setText("R1 = f(x) ="); jLabel3.setText("R2 = f'(x) ="); r1label.setText("jLabel5"); r2label.setText("jLabel6"); org.jdesktop.layout.GroupLayout jPanel3Layout = new org.jdesktop.layout.GroupLayout(jPanel3); jPanel3.setLayout(jPanel3Layout); jPanel3Layout.setHorizontalGroup( jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(jPanel3Layout.createSequentialGroup() .addContainerGap() .add(jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(jPanel3Layout.createSequentialGroup() .add(jLabel2) .add(10, 10, 10) .add(r1label)) .add(jPanel3Layout.createSequentialGroup() .add(jLabel3) .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED) .add(r2label))) .addContainerGap(136, Short.MAX_VALUE)) ); jPanel3Layout.setVerticalGroup( jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(jPanel3Layout.createSequentialGroup() .addContainerGap() .add(jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) .add(jLabel2) .add(r1label)) .add(32, 32, 32) .add(jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) .add(jLabel3) .add(r2label)) .addContainerGap(34, Short.MAX_VALUE)) ); jPanel4.setBackground(new java.awt.Color(204, 204, 255)); jPanel4.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(102, 153, 255))); jCheckBox1.setBackground(new java.awt.Color(204, 204, 255)); jCheckBox1.setText("f(x) - f'(x)"); jCheckBox2.setBackground(new java.awt.Color(204, 204, 255)); jCheckBox2.setText("f(x) - g'(x)"); jCheckBox3.setBackground(new java.awt.Color(204, 204, 255)); jCheckBox3.setText("g(x) - f'(x)"); jLabel4.setText("Opzioni Visualizzazione"); jCheckBox4.setBackground(new java.awt.Color(204, 204, 255)); jCheckBox4.setText("g(x) - g'(x)"); jCheckBox5.setBackground(new java.awt.Color(204, 204, 255)); jCheckBox5.setText("Visualizza Bordo"); org.jdesktop.layout.GroupLayout jPanel4Layout = new org.jdesktop.layout.GroupLayout(jPanel4); jPanel4.setLayout(jPanel4Layout); jPanel4Layout.setHorizontalGroup( jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(jPanel4Layout.createSequentialGroup() .add(jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(jPanel4Layout.createSequentialGroup() .add(19, 19, 19) .add(jLabel4)) .add(jPanel4Layout.createSequentialGroup() .addContainerGap() .add(jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false) .add(org.jdesktop.layout.GroupLayout.LEADING, jCheckBox2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .add(org.jdesktop.layout.GroupLayout.LEADING, jCheckBox3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .add(org.jdesktop.layout.GroupLayout.LEADING, jCheckBox4, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .add(org.jdesktop.layout.GroupLayout.LEADING, jCheckBox1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))) .add(jPanel4Layout.createSequentialGroup() .addContainerGap() .add(jCheckBox5))) .addContainerGap(44, Short.MAX_VALUE)) ); jPanel4Layout.setVerticalGroup( jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(jPanel4Layout.createSequentialGroup() .addContainerGap() .add(jLabel4) .add(26, 26, 26) .add(jCheckBox1) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) .add(jCheckBox2) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) .add(jCheckBox3) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) .add(jCheckBox4) .add(18, 18, 18) .add(jCheckBox5) .addContainerGap(42, Short.MAX_VALUE)) ); jButton1.setText("R1 - R2"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jButton2.setText("R2 - R1"); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); jLabel5.setText("CALCULATES :"); org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(jPanel1Layout.createSequentialGroup() .add(44, 44, 44) .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false) .add(jPanel3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup() .add(10, 10, 10) .add(jLabel5) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .add(jButton1) .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED) .add(jButton2))) .add(69, 69, 69) .add(panelEsterno, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .add(18, 18, 18) .add(jPanel4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .addContainerGap()) .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup() .addContainerGap(588, Short.MAX_VALUE) .add(jLabel1) .add(352, 352, 352)) ); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(jPanel1Layout.createSequentialGroup() .add(138, 138, 138) .add(jPanel3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) .add(jButton2) .add(jButton1) .add(jLabel5)) .addContainerGap(414, Short.MAX_VALUE)) .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup() .addContainerGap() .add(jLabel1) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 31, Short.MAX_VALUE) .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(jPanel1Layout.createSequentialGroup() .add(jPanel4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .addContainerGap()) .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup() .add(panelEsterno, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .add(22, 22, 22)))) ); jMenu1.setText("File"); jMenuBar1.add(jMenu1); jMenu2.setText("Edit"); jMenuBar1.add(jMenu2); setJMenuBar(jMenuBar1); org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(layout.createSequentialGroup() .addContainerGap() .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(layout.createSequentialGroup() .addContainerGap() .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); pack(); }// </editor-fold> |
![]() |
![]() |
![]() |
#4 |
Senior Member
Iscritto dal: Nov 2004
Città: Tra Verona e Mantova
Messaggi: 4553
|
E' un GroupLayout. Cambialo in GridLayout(1, 1). C'è un pulsante nelle proprietà del componente sulla destra, "layout" o una cosa del genere. Premi sui puntini, compare una finestra, scegli il GridLayout eccetera eccetera.
Lo scambio funziona anche con FlowLayout (che è il layout manager predefinito di JPanel) o BorderLayout.
__________________
Uilliam Scecspir ti fa un baffo? Gioffri Cioser era uno straccione? E allora blogga anche tu, in inglese come me! |
![]() |
![]() |
![]() |
#5 |
Senior Member
Iscritto dal: Jan 2006
Città: Perugia - San Benedetto del Tronto
Messaggi: 348
|
PGI, innanzitutto grazie mille per la tua solita disponibilità.
Ad ogni modo, ho cambiato il Layout del PanelEsterno (immagino intendessi dire questo con il "componente sulla destra") e questo è il risultato: ![]() Ora sembra ridisegnare qualcosa, si intravvedono le linee della griglia. Il punto è che è tutto ridimensionato in questa strana maniera. Purtroppo NetBeans non permette di modificare tutto il codice che voglio, questo è quello che mi genera: Codice:
panelEsterno = new javax.swing.JPanel(); panelEsterno.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED)); panelEsterno.setLayout(new java.awt.GridLayout()); |
![]() |
![]() |
![]() |
#6 |
Senior Member
Iscritto dal: Nov 2004
Città: Tra Verona e Mantova
Messaggi: 4553
|
Sempre usando matisse - il GUI builder - prova ad allargare il pannello panelEsterno in modo che assuma la dimensione che aveva prima. Oppure dagli una dimensione preferita sufficiente a proiettare la griglia. Purtroppo matisse non digerisce molto bene i cambiamenti di layout. Se lo fai come vuoi dall'inizio va benissimo ma se ci ripensi sei fregato.
__________________
Uilliam Scecspir ti fa un baffo? Gioffri Cioser era uno straccione? E allora blogga anche tu, in inglese come me! |
![]() |
![]() |
![]() |
#7 |
Senior Member
Iscritto dal: Jan 2006
Città: Perugia - San Benedetto del Tronto
Messaggi: 348
|
Niente, non parte niente. Adesso mi dà un risultato totalmente diverso da ieri, mi crea un casino con il Layout. La cosa più assurda è che con un altro progetto usavo la stessa tecnica e partiva. Stò nero come una iena.
A questo punto cambio GUI builder. |
![]() |
![]() |
![]() |
#8 |
Senior Member
Iscritto dal: Jan 2006
Città: Perugia - San Benedetto del Tronto
Messaggi: 348
|
Ecco qua, ce l'ho fatta a risolvere
![]() La corretta sequenza era quella da me postata, ovvero: Codice:
PanelDifZon difZon_nuovo = new PanelDifZon(funzione1, funzione2, 2, 2, "funzione1",600,600); panelEsterno.removeAll(); panelEsterno.add(difZon_nuovo); panelEsterno.revalidate(); panelEsterno.repaint(); Quello che creava problemi era la mancata presenza di questa riga all'interno della classe del JPanel personalizzato: Codice:
super.setSize(this.larghezza, this.altezza); Se non specificate la dimensione non vedrete niente a schermo. Ecco fatto, adesso potete crearvi un vostro JPanel personalizzato e integrarlo tranquillamente alla GUI Builder del NetBeans. Il tutto senza problemi di update e utilizzando il Layout di default di Matisse. Spero possa tornare utile a qualcuno ![]() |
![]() |
![]() |
![]() |
#9 |
Senior Member
Iscritto dal: Nov 2004
Città: Tra Verona e Mantova
Messaggi: 4553
|
Felice che tu abbia risolto ma, onde evitare che qualche lettore ci caschi, no, non è così funziona la faccenda. Si vede con o senza setSize, con o senza GroupLayout.
__________________
Uilliam Scecspir ti fa un baffo? Gioffri Cioser era uno straccione? E allora blogga anche tu, in inglese come me! |
![]() |
![]() |
![]() |
#10 | |
Senior Member
Iscritto dal: Jan 2006
Città: Perugia - San Benedetto del Tronto
Messaggi: 348
|
Quote:
![]() Quello che ho visto io è che, usando il GroupLayout, senza il setSize non si vede nulla. |
|
![]() |
![]() |
![]() |
Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 07:48.