MyPane n = new MyPane(new ImageIcon("images/kk.jpg"));//class MyPane extends JPanel
{
private ImageIcon image = null; // 要画的图形
/**
* 构造,接收ImageIcon参数
* @param image 要画的图形参数
*/
public MyPane(ImageIcon image) {
this.image = image;
}
/**
* 重载printComponent(Graphics g)方法来画图
*/
protected void paintComponent(Graphics g) {
int w=this.getSize().width;
int h=this.getSize().height;
super.paintComponent(g);
g.drawImage(image.getImage(), 0, 0, w, h, this); // 将固定大小的Image画到固定的位置
}
}//// 鼠标点击表格监听
public void jTable1_mouseClicked(MouseEvent e) {
//BUTTON1常量 指示鼠标按键 #1;由 getButton() 使用。
if (e.getButton() == e.BUTTON1) {
int row = jTable1.getSelectedRow(); //getSelectedRow() 返回首个选定行的索引,如果没有选定的行,则返回 -1。
DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
//表示将jTable1信息选中行的值设到相应的编辑框中
this.name.setText(model.getValueAt(row, 1).toString());
this.auhor1.setText(model.getValueAt(row, 2).toString());
price1.setText(model.getValueAt(row, 3).toString());
agio1.setText(model.getValueAt(row, 4).toString());
chu1.setText(model.getValueAt(row, 7).toString());
chuDate1.setText(model.getValueAt(row, 8).toString());
//问题在这里。为什么不能随着选项而改变图片
n = new MyPane(new ImageIcon(model.getValueAt(row, 11).toString()));
n.setBounds(30, 50, 169, 220);
}
}model.getValueAt(row, 11).toString() //返回的地址 正确 images/xxx.jpg
或用其他方法达到效果
{
private ImageIcon image = null; // 要画的图形
/**
* 构造,接收ImageIcon参数
* @param image 要画的图形参数
*/
public MyPane(ImageIcon image) {
this.image = image;
}
/**
* 重载printComponent(Graphics g)方法来画图
*/
protected void paintComponent(Graphics g) {
int w=this.getSize().width;
int h=this.getSize().height;
super.paintComponent(g);
g.drawImage(image.getImage(), 0, 0, w, h, this); // 将固定大小的Image画到固定的位置
}
}//// 鼠标点击表格监听
public void jTable1_mouseClicked(MouseEvent e) {
//BUTTON1常量 指示鼠标按键 #1;由 getButton() 使用。
if (e.getButton() == e.BUTTON1) {
int row = jTable1.getSelectedRow(); //getSelectedRow() 返回首个选定行的索引,如果没有选定的行,则返回 -1。
DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
//表示将jTable1信息选中行的值设到相应的编辑框中
this.name.setText(model.getValueAt(row, 1).toString());
this.auhor1.setText(model.getValueAt(row, 2).toString());
price1.setText(model.getValueAt(row, 3).toString());
agio1.setText(model.getValueAt(row, 4).toString());
chu1.setText(model.getValueAt(row, 7).toString());
chuDate1.setText(model.getValueAt(row, 8).toString());
//问题在这里。为什么不能随着选项而改变图片
n = new MyPane(new ImageIcon(model.getValueAt(row, 11).toString()));
n.setBounds(30, 50, 169, 220);
}
}model.getValueAt(row, 11).toString() //返回的地址 正确 images/xxx.jpg
或用其他方法达到效果
model.getValueAt(row, 11).toString().trim()忘了 取值 没去空格