这是我的代码片段:
PreparedStatement ps = con.prepareStatement("select * from patrika where jantacode = ?");
ps.setString(1, jComboBox1.getItemAt(jComboBox1.getSelectedIndex()));
ResultSet rs = ps.executeQuery();
rs.next();
jTextField2.setText(rs.getString("companycode"));
jTextField3.setText(rs.getString("manufacturer"));
jTextField4.setText(rs.getString("purchaseprice"));
jTextField5.setText(rs.getString("wholesaleprice"));
jTextField6.setText(rs.getString("retailprice"));
jTextField7.setText(rs.getString("location"));
jTextField1.setText(rs.getString("stock"));
//Getting and displaying image
Blob blob = rs.getBlob("image");
int blobLength = (int) blob.length();
byte[] bytes = blob.getBytes(1, blobLength);
blob.free();
BufferedImage img = ImageIO.read(new ByteArrayInputStream(bytes));
Graphics2D g = img.createGraphics();
jPanel1.paint(g);
jPanel1.repaint();
一切正常,但jPanel无法在其上绘制图像。
还附上了它的快照。有谁能帮我找出我的错误吗?
答案 0 :(得分:1)
如果您希望将图像绘制到JPanel,则必须覆盖其paintComponent()方法,因为您希望每帧都绘制它。
看起来像这样,来自文档:
public void paintComponent(Graphics g) {
// Let UI Delegate paint first, which
// includes background filling since
// this component is opaque.
super.paintComponent(g);
g.drawString("This is my custom Panel!",10,20);
redSquare.paintSquare(g);
}
现在您加载图像并仅将其绘制到面板上。
花更多时间在文档上:) 这样做会很棒:https://docs.oracle.com/javase/tutorial/uiswing/painting/index.html