Visualizzazione dei risultati da 1 a 3 su 3
  1. #1
    Utente di HTML.it
    Registrato dal
    Oct 2001
    Messaggi
    532

    [JAVA] JPanel dentro JFrame

    Questo è un semplice codice per testare i panel:
    l'errore che ottengo è:

    Exception in thread "main" java.lang.NullPointerException
    at mio2.<init>(mio2.java:48)
    at mio2.main(mio2.java:68)
    Press any key to continue...



    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.TitledBorder;

    public class mio2 extends JFrame
    {
    private JPanel pannelloSCUp;
    private JPanel pannelloSCCenter;
    private JPanel pannelloSCDown;

    private JButton button1 = new JButton("p1");
    private JButton button2 = new JButton("p2");
    private JButton button3 = new JButton("p3");


    public mio2()
    {
    setLayout(new GridLayout(3,1));
    setTitle("Titolo del software");
    //line 48
    pannelloSCUp.setLayout(new FlowLayout());
    pannelloSCUp.setBackground(Color.pink);
    pannelloSCUp.add(button1);

    pannelloSCCenter.setLayout(new FlowLayout());
    pannelloSCCenter.setBackground(Color.red);
    pannelloSCCenter.add(button2);

    pannelloSCDown.setLayout(new BorderLayout());
    pannelloSCDown.setBackground(Color.green);
    pannelloSCDown.add(button3);

    this.getContentPane().add(pannelloSCUp);
    this.getContentPane().add(pannelloSCCenter);
    this.getContentPane().add(pannelloSCDown);

    }

    // Main method
    public static void main(String[] args)
    {
    //line 68
    mio2 frame = new mio2();
    frame.setSize(800, 600);
    frame.setVisible(true);

    }
    }

  2. #2
    Utente di HTML.it
    Registrato dal
    Aug 2002
    Messaggi
    8,013
    Sistema un po' il codice (devi inizializzare i JPanel - tu li hai solo dichiarati, ma l'oggetto non esiste, creando tutti i problemi del caso), prova a trasformare le prime righe del costruttore in
    codice:
    public mio2() {
      super("Titolo del software");
      this.getContentPane().setLayout(new GridLayout(3,1));
      pannelloSCUp = new JPanel(); 
      pannelloSCCenter = new JPanel(); 
      pannelloSCDown  = new JPanel();
    <´¯)(¯`¤._)(¯`»ANDREA«´¯)(_.¤´¯)(¯`>
    "The answer to your question is: welcome to tomorrow"

  3. #3
    Utente di HTML.it
    Registrato dal
    Oct 2001
    Messaggi
    532
    grazie

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 © 2024 vBulletin Solutions, Inc. All rights reserved.