2

爪哇摇摆。我有一个JPanel名为panel_table的它将客户端加载到 ajTable中,它包含在 ajScrollPane中。

总之:jPanel> jScrollPane> jTable

  • 如果表格中有数据,那么我们绘制表格。
  • 如果表中没有数据,那么我们隐藏jTable并在 中绘制图像JPanel

我的问题是,如果没有数据绘制图像但.png 文件的尺寸不正确-说图像-。为什么?我不明白。

代码:

public class Listado_clientes extends javax.swing.JFrame{
    private TableRowSorter TRSFiltro;

    public class Imagen extends javax.swing.JPanel {
        public Imagen() {
            this.setSize(849, 377); //I put these measures because they are those of the JPanel "panel_tabla".
        }

        public void paint(Graphics grafico) {
            Dimension height = getSize();

            ImageIcon imagen = new ImageIcon(getClass().getResource("/imagenes/error.png")); 
            grafico.drawImage(imagen.getImage(), 0, 0, height.width, height.height, null);

            setOpaque(false);
            super.paintComponent(grafico);
        }
    }

    public Listado_clientes() {
        initComponents();

        try {
            DefaultTableModel modelo = new DefaultTableModel();
            tabla_clientes.setModel(modelo);

            //We make the query to the database... ... ...
            //We create columns and measures from the table... ... ...

            while (rs.next()) {
                Object[] filas = new Object[cantidadColumnas];
                for (int i=0; i<cantidadColumnas; i++) {
                    filas[i] = rs.getObject(i+1);
                }
                modelo.addRow(filas);
            }
        } catch (SQLException ex) {
            System.err.println(ex.toString());
        }

        //If when loading the JFrame the database is empty ...
        int count = tabla_clientes.getModel().getRowCount();
        if(count==0){
            panel_top.setVisible(false);

            panel_tabla.setVisible(true);
            jScrollPane.setVisible(false); //hide JScrollPane and JTable

            Imagen Imagen = new Imagen();
            panel_tabla.add(Imagen);
            panel_tabla.repaint();
        }
    }
}

当表中存在数据时显示图像:

在此处输入图像描述

表中没有数据时的我的图像-此处错误-:

在此处输入图像描述

我选择绘制的图像如下 - 尺寸:1200x490:

https://www.solvetic.com/uploads/tutorials/monthly_05_2019/53b64448ef3386307fbbff957c37ae19.png

4

0 回答 0