Visualizzazione dei risultati da 1 a 3 su 3

Discussione: Problemi con i panel

  1. #1

    Problemi con i panel

    salve
    ho il seguente component:
    import java.awt.*;
    import javax.swing.*;

    public class PlotTest extends JComponent
    {
    int PAD = 20;
    boolean drawLine = true;
    boolean drawDots = true;
    int dotRadius = 3;

    // the y coordinates of the points to be drawn; the x coordinates are evenly spaced
    int[] data = { 25, 30, 42, 45 };

    protected void paintComponent (Graphics g)
    {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D)g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASIN G, RenderingHints.VALUE_ANTIALIAS_ON);
    int w = getWidth();
    int h = getHeight();
    g2.drawLine(PAD, PAD, PAD, h-PAD);
    g2.drawLine(PAD, h-PAD, w-PAD, h-PAD);
    double xScale = (w - 2*PAD) / (data.length + 1);
    double maxValue = 100.0;
    double yScale = (h - 2*PAD) / maxValue;
    // The origin location
    int x0 = PAD;
    int y0 = h-PAD;

    // draw connecting line
    if (drawLine)
    {
    for (int j = 0; j < data.length-1; j++)
    {
    int x1 = x0 + (int)(xScale * (j+1));
    int y1 = y0 - (int)(yScale * data[j]);
    int x2 = x0 + (int)(xScale * (j+2));
    int y2 = y0 - (int)(yScale * data[j+1]);
    g2.drawLine(x1, y1, x2, y2);
    }
    }

    // draw the points as little circles in red
    if (drawDots)
    {
    g2.setPaint(Color.red);
    for (int j = 0; j < data.length; j++)
    {
    int x = x0 + (int)(xScale * (j+1));
    int y = y0 - (int)(yScale * data[j]);
    g2.fillOval(x-dotRadius, y-dotRadius, 2*dotRadius, 2*dotRadius);
    }
    }
    }
    }
    che vorrei visualizzare all'interno di un panel contenuto in un'applet dopo il click di un bottone
    l'applet è la seguente:
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {

    PlotTest d = new PlotTest();
    d.setVisible(true);
    jPanel2.add(d);

    jPanel2.setVisible(true);
    jPanel2.updateUI();

    }
    ma non mi visulizza il component PlotTest

    se invece scrivo:
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
    JFrame f = new JFrame();

    f.getContentPane().add(new PlotTest());
    f.setSize(400,400);
    f.setLocation(200,200);
    f.setVisible(true);
    }
    riesco a visualizzare il component PlotTest, ma lo visualizza all'interno di un'altra finestra
    io invece appunto voglio visualizzarlo all'interno del panel jPanel2
    se volete posso scrivere tutto il codice dell'applet ma non credo che servi
    ringrazio anicipatemnte chi puo aiutarmi
    ciauz

  2. #2
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284

    Re: Problemi con i panel

    Originariamente inviato da simba1982
    io invece appunto voglio visualizzarlo all'interno del panel jPanel2
    La questione è che aggiungere/rimuovere componenti in un "contenitore" che è già visibile, richiede come minimo un passo in più.
    E nota che updateUI() NON ti serve. Non fa quello che forse pensavi.

    Innanzitutto bisogna vedere quale layout manager usa il contenitore (il tuo jPanel2, nel tuo caso). Generalmente basta un validate() sul contenitore. Ma come ripeto, dipende anche dal layout manager.
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

  3. #3
    intanto grazie per la risposta
    ho provato a scrivere questo

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {

    PlotTest d = new PlotTest();

    jPanel2.removeAll();
    d.setVisible(true);
    jPanel2.add(d);

    jPanel2.setVisible(true);
    jPanel2.validate();
    jPanel1.validate();


    }
    il JPanel 2 è contenuto all'interno del jPanel1(il macro componente ke contiene anke altri componenti tra cui il bottone che dovrebbe aggiornare il jPanel2) quindi ho pensato di fare il validate anche in quest'ultimo...ma niente
    il layout impostato è free design
    se vuoi posso scrivere l'intero codice della GUI

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.