我是Java开发的初学者。想要将图片添加为JPanel
的背景。我在过去4个小时内尝试了很多解决方案,最终出现了这个错误:
线程“AWT-EventQueue-0”中的异常 java.lang.IllegalArgumentException:input == null!在 javax.imageio.ImageIO.read(ImageIO.java:1348)at schedule.loginPrintTable $ 1.run(loginPrintTable.java:107)at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
请注意我在这个文件中没有main(),我不想使用main()来添加图像作为背景。
class loginPrintTable extends JLabel{
JFrame tableframe;
private table[][][] ttable;
DefaultTableModel model = new DefaultTableModel();
JTable Table;
int stgrp;
int i;
JPanel south;
inputdata input;
public loginPrintTable(int nostgrp, inputdata input1) {
// ttable = t;
tableframe = new JFrame();
stgrp = nostgrp;
south = new JPanel();
}
void print() {
EventQueue.invokeLater(new Runnable() {
public void run() {
tableframe.setTitle("CLASS SCHEDULING");
tableframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
tableframe.setSize(800, 600);
tableframe.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.weightx = 0;
c.gridx = 1000;
c.gridy = 50;
c.fill = GridBagConstraints.CENTER;
int j;
JPanel panel;
JLabel label1, label2;
final JTextField text1, text2;
label1 = new JLabel();
label1.setText("Username:");
text1 = new JTextField(15);
JButton next = new JButton("Login");
JButton next1 = new JButton("Cancel");
label2 = new JLabel();
label2.setText("Password:");
text2 = new JPasswordField(15);
panel = new JPanel(new GridLayout(3, 1));
panel.add(label1);
panel.add(text1);
panel.add(label2);
panel.add(text2);
panel.add(next);
panel.add(next1);
try {
InputStream imageStream = this.getClass().getResourceAsStream("asset/login.jpeg");
BufferedImage image;
image = ImageIO.read(imageStream);
JLabel picLabel = new JLabel(new ImageIcon(image));
panel.add(picLabel);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JPanel panel2;
JLabel label3;
label3 = new JLabel();
label3.setText("Progressing...Please wait....");
panel2 = new JPanel(new GridLayout(3, 1));
panel2.add(label3);
JScrollPane scroll = new JScrollPane(Table);
// panel=new loginPanel(ttable,stgrp,input);
JPanel south = new JPanel();
south.setLayout(new GridLayout(1, 10));
for (j = 0; j < 2; j++) {
south.add(new JLabel(" "));
}
south.add(next);
south.add(next1);
tableframe.add(panel);
c.gridx = 0;
c.gridy = 10;
tableframe.add(south, c);
tableframe.setVisible(true);
}
});
}
}