将图像加载到JPanel SWING GUI

时间:2013-02-23 16:41:57

标签: java image swing jframe jpanel

我想弄清楚我需要添加什么或者我需要做什么才能让我的图像在gui上绘画。这是我的代码,有人可以告诉我该怎么做。

我创建了JFrame,JPanel,JButton和JLabel。在鼠标释放时,它会在您单击JButton时打开一个文件。我只是不知道如何正确地绘制图像

public class GUI extends javax.swing.JFrame {

/** Creates new form GUI */
public GUI() {
    initComponents();
}


/** This method is called from within the constructor to
 * initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is
 * always regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

    jPanel1 = new javax.swing.JPanel();
    jButton1 = new javax.swing.JButton();
    jLabel1 = new javax.swing.JLabel();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setMinimumSize(new java.awt.Dimension(512, 512));

    jPanel1.setMaximumSize(new java.awt.Dimension(512, 512));
    jPanel1.setMinimumSize(new java.awt.Dimension(512, 512));

    jButton1.setText("Launch");
    jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseReleased(java.awt.event.MouseEvent evt) {
            jButton1MouseReleased(evt);
        }
    });

    jLabel1.setText("Pic");

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
            .addContainerGap(455, Short.MAX_VALUE)
            .addComponent(jButton1)
            .addContainerGap())
        .addGroup(jPanel1Layout.createSequentialGroup()
            .addGap(99, 99, 99)
            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 261, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(172, Short.MAX_VALUE))
    );
    jPanel1Layout.setVerticalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel1Layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jButton1)
            .addGap(18, 18, 18)
            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 215, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(329, Short.MAX_VALUE))
    );

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );

    pack();
}// </editor-fold>

private void jButton1MouseReleased(java.awt.event.MouseEvent evt) {
  try {
        Desktop.getDesktop().open(new File("C:\\Users\\Vincent\\Desktop\\Folda\\technic-launcher.jar"));
    } catch (IOException ex) {
        System.out.println(ex.getMessage());
    }
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new GUI().setVisible(true);
        }
    });
}

public void paint(){

    try {
        BufferedImage BG = ImageIO.read(this.getClass().getResource("mojang.png"));
        JLabel BGL = new JLabel(new ImageIcon(BG));
    } catch (IOException ex) {
        System.out.println(ex.getMessage());
    }
}

// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanel1;
// End of variables declaration

}

0 个答案:

没有答案