Domanda Sfondo app java

Stato
Discussione chiusa ad ulteriori risposte.

Axel93x

Utente Emerald
22 Maggio 2011
958
89
261
565
Ragazzi, mi stavo chiedendo: come applico un sfondo in un JPanel?


import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JPanel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
import javax.swing.LayoutStyle.ComponentPlacement;

public class Finestra {

private JFrame frmMlgSoundboard;
private JPanel panel;
private JButton btnNewButton;
private JTextField textField;
private JLabel lblNome;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Finestra window = new Finestra();
window.frmMlgSoundboard.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the application.
*/
public Finestra() {
initialize();
}

/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmMlgSoundboard = new JFrame();
frmMlgSoundboard.setResizable(false);
frmMlgSoundboard.setTitle("MLG SOUNDBOARD");
frmMlgSoundboard.setBounds(100, 100, 450, 300);
frmMlgSoundboard.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

panel = new JPanel();
GroupLayout groupLayout = new GroupLayout(frmMlgSoundboard.getContentPane());
groupLayout.setHorizontalGroup(
groupLayout.createParallelGroup(Alignment.TRAILING)
.addComponent(panel, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 434, Short.MAX_VALUE)
);
groupLayout.setVerticalGroup(
groupLayout.createParallelGroup(Alignment.TRAILING)
.addGroup(Alignment.LEADING, groupLayout.createSequentialGroup()
.addContainerGap()
.addComponent(panel, GroupLayout.DEFAULT_SIZE, 263, Short.MAX_VALUE))
);

btnNewButton = new JButton("Accedi");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
 
dovresti ridefinire il metodo paintComponent con qualcosa tipo:
Codice:
protectedvoid paintComponent(Graphics g){
    g.drawImage(img,0,0,null);
    super.paintComponent(g);
  }
io farei una classe apposita che estende JPanel.
 
crei una nuova classe MyJPanel (esempio) e la estendi a JPanel, quindi inserisci quel metodo che altro non fa che ridefinire la funzione di paint. Ora non ho gli strumenti adatti su questo pc per poter fare delle prove ma a livello teorico dovrebbe andare. Se cerchi su internet trovi qualche esempio.
 
Stato
Discussione chiusa ad ulteriori risposte.