代码如下
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class example extends JFrame{
public static ImageIcon exampleImage=new ImageIcon(ProjectPreparationFrame.class.getResource("exampleimg.jpg"));
JLabel exIL = new JLabel();
JPanel jPanel = new JPanel();
JLabel egL = new JLabel();
public void eIinit(){
exIL.setText("");
exIL.setIcon(exampleImage);
egL.setText("example for set the spectrums");
getContentPane().add(jPanel);
jPanel.add(egL,BorderLayout.NORTH);
jPanel.add(exIL,BorderLayout.SOUTH);
}
public void example(){
eIinit();
}
public static void main(String args[]){
example f = new example();
f.setVisible(true);
}
}
------解决方案--------------------
最关键的问题是你 声明了public void example()这样就没有构造方法,加了一个void就变成普通的方法了
下面这是我修改的,具体的图片位置你自己改一下就可以了
- Java code
import java.awt.*;import java.awt.event.*;import javax.swing.*;public class Example extends JFrame{ public static ImageIcon exampleImage=new ImageIcon(Example.class.getResource("thu.jpg")); JLabel exIL = new JLabel(); JPanel jPanel = new JPanel(); JLabel egL = new JLabel(); public void eIinit(){ exIL.setText(""); exIL.setIcon(exampleImage); egL.setText("example for set the spectrums"); jPanel.add(egL,BorderLayout.NORTH); jPanel.add(exIL,BorderLayout.SOUTH); getContentPane().setLayout(new FlowLayout()); getContentPane().add(jPanel); } public Example(){// super(); eIinit(); } public static void main(String args[]){ Example f = new Example(); f.pack(); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }}
------解决方案--------------------
public void example() 加了一个void就变成普通的方法了