Ciao a tutti,
E' da poco che ho preso in mano java e sono arrivato a studiare i Jframe e fin li tutto apposto
Questa è la classe del Jframe
codice:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package provajframemain;
import java.awt.Dimension;
import java.awt.LayoutManager;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
*
* @author Giovanni
*/
public class ProvaJFrame extends JFrame {
public ProvaJFrame() {
setTitle("Questo è un JFrame !");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension dimensioniSchermo = Toolkit.getDefaultToolkit().
getScreenSize();
int larghezzaFrame, altezzaFrame;
larghezzaFrame = (int) (dimensioniSchermo.getWidth() / 4);
altezzaFrame = (int) (dimensioniSchermo.getHeight() / 2);
setSize(larghezzaFrame, altezzaFrame);
setLocation(((int) dimensioniSchermo.getWidth() / 4),
((int) dimensioniSchermo.getHeight() / 4));
setVisible(true);
}
}
Poi ho voluto implementare la classe PannelloConImmagine che visualizzasse una immagine dentro al JFrame.
E qui mi blocco perchè dopo aver fatto copia incolla del codice da un esempio da una dispensa JavaAwtSwin, non so proprio cosa scrivere nella classe Main per far visualizzare l'immagine! (il JFrame va).
Sono andato a vedere vari tutorials su youtube che spiegano come farlo e con il loro metodo sono riuscito, ma solo perchè loro facevano tutto direttamente nel main e usando Jlabel o Jpanel, e non è il metodo che voglio usare io, dato che ho le 3 classi separate.
Il problema è che non ho proprio idea di cosa scrivere, e di cosa implementare nel main per far funzionare il tutto.. Aparte il JFrame che quello son riuscito.
Ecco il resto delle classi..
PannelloConImmagine-
codice:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package provajframemain;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.LayoutManager;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JLabel;
import javax.swing.JPanel;
/**
*
* @author Giovanni
*/
class PannelloConImmagine extends JPanel {
private BufferedImage miaImmagine;
private JPanel pannello;
public PannelloConImmagine() {
try {
miaImmagine = ImageIO.read(getClass().getClassLoader().getResource("images/120926.gif"));
} catch (IOException ex) {
miaImmagine = null;
}
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(miaImmagine, 0, 0, null);
}
}
Classe Main-
codice:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package provajframemain;
import java.awt.Graphics;
/**
*
* @author Giovanni
*/
public class ProvaJframeMain {
private static Graphics Graphics;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
ProvaJFrame frame = new ProvaJFrame();
}
}
Grazie.