JAVA:JPanel e Graphics

Linguaggi di programmazione: php, perl, python, C, bash e tutti gli altri.
venti

JAVA:JPanel e Graphics

Messaggio da venti »

Ciao a tutti intanto posto il codice:

Codice: Seleziona tutto

import java.awt.Color;
import javax.swing.JFrame;
import java.awt.Graphics;
import java.awt.Label;
import java.awt.event.WindowAdapter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;


public class Main extends JFrame {
    
    public Main(){
        JPanel panel = new JPanel();
        setTitle("Second");
        setSize(960,800);
        setVisible(true);
        JLabel jf = new JLabel();
        jf.setSize(20, 40);
        jf.setText("Ciao");
        jf.setVisible(true);
        panel.add(jf);        
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        

    }
    
        
    public void paint(Graphics g){
        g.setColor(Color.BLUE);
        g.drawLine(60, 60, 160, 60);
        g.setColor(Color.GREEN);
        g.drawRect(60, 90, 160, 90);   //Punto partenza,Punto partenza,larghezza,altezza
        g.setColor(Color.orange);
        g.drawRect(60, 190, 90, 90);
        
    }

    public static void main(String[] args) {
        Main t = new Main();
     }
    
}
Dal codice se lo eseguite, si può notare dei grafici, fin qui tutto ok. Ma se voglio inserire una lbl e una stringa, ricorrendo magari al JFrame, come posso fare? Premetto che non sono molto bravo sui alcuni componenti di java, perché ricorro spesso all'ide di netbeans.

Grazie
venti

Re: JAVA:JPanel e Graphics

Messaggio da venti »

Questa è un'altro esempio:

Codice: Seleziona tutto

import java.awt.Color;
import javax.swing.JFrame;
import java.awt.Graphics;
import java.awt.Label;
import java.awt.event.WindowAdapter;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;


public class Main extends JPanel {
  /*  
    public Main(){
        JPanel panel = new JPanel();
        setTitle("Second");
        setSize(960,800);
        setVisible(true);
        JLabel jf = new JLabel();
        jf.setSize(20, 40);
        jf.setText("Ciao");
        jf.setVisible(true);
        panel.add(jf);        
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        

    }
    */
    
    
    public void paint(Graphics g){
        g.setColor(Color.BLUE);
        g.drawLine(60, 60, 160, 60);
        g.setColor(Color.GREEN);
        g.drawRect(60, 90, 160, 90);   //Punto partenza,Punto partenza,larghezza,altezza
        g.setColor(Color.orange);
        g.drawRect(60, 190, 90, 40);
        
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        JLabel lb1 = new JLabel();
        lb1.setLocation(250, 200);
        //lb1.setSize(20, 90);
        lb1.setText("Ciao");
        lb1.setVisible(true);
                
        frame.setSize(600,600);
        //ame.getContentPane().add(new Work());
        frame.getContentPane().add(new Main());
        frame.setLocationRelativeTo(null);
        frame.setBackground(Color.LIGHT_GRAY);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setVisible(true);
        frame.add(lb1);        
                        
     }
Scrivi risposta

Ritorna a “Programmazione”

Chi c’è in linea

Visualizzano questa sezione: 0 utenti iscritti e 5 ospiti