Witam, walczę od dłuższego czasu i nie mogę znaleŹć odpowiedzi dlaczego ciągle pojawia mi się ten błąd:
" non static method cannot be referenced from a static context"
Nie rozumiem dlaczego tak się dzieje, jestem początkującym, piszę w NetBeans.
Jedyne co zmieniłem zostało ujęte w bloki // -------------- i kończę tym samym, pozostałych linii generowanych automatycznie nie zmieniałem.
Błąd wyrzuca w mainie przy próbie stworzenia obiektu : " new ImagePanelTest("pool2.jpg"); ".
Z góry dziękuję za pomoc.
Kod:
/*
* 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 welplanwiever;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
*
* @author Pawel
*/
public class welPlanWIever2 extends javax.swing.JFrame {
/**
* Creates new form welPlanWIever2
*/
public welPlanWIever2() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
// ------------------------------------------------------------------------------------------------------------------------
class ImagePanel extends JPanel
{
Image img;
boolean loaded = false; // czy obrazek został załadowany?
public ImagePanel(String imgFileName) {
loadImage(imgFileName);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (img != null && loaded) {
g.drawImage(img, 0, 0, getWidth(), getHeight(), this);
} else {
g.drawString("Bez obrazka", 10, getHeight() - 10);
}
}
private void loadImage(String imgFileName) {
img = Toolkit.getDefaultToolkit().getImage(imgFileName);
MediaTracker mt = new MediaTracker(this);
mt.addImage(img, 1);
try {
mt.waitForID(1);
} catch (InterruptedException exc) {
}
int w = img.getWidth(this); // szerokość obrazka
int h = img.getHeight(this); // wysokość obrazka
if (w != -1 && w != 0 && h != -1 && h != 0) {
loaded = true;
setPreferredSize(new Dimension(w, h));
} else {
setPreferredSize(new Dimension(200, 200));
}
}
}
public class ImagePanelTest extends JFrame
{
public ImagePanelTest(String fname) {
ImagePanel p = new ImagePanel(fname);
p.add(new JButton("Jakiś przycisk"));
add(p);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
}
//-----------------------------------------------------------------------------------------------------------------------------
public static void main(String args[])
{
new ImagePanelTest("pool2.jpg"); // argument - nazwa pliku graficznego
//( z roboczego katalogu aplikacji)
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(welPlanWIever2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(welPlanWIever2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(welPlanWIever2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(welPlanWIever2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>,
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new welPlanWIever2().setVisible(true);
}
});
}
// Variables declaration - do not modify
// End of variables declaration
}