我是java的初学者。我有一个名为NumCols = 10
vars = [ 'x'+str(n) for n in xrange(1,NumCols+1) ]
coef = [1]*NumCols
cpx.linear_constraints.add(
lin_expr= [cplex.SparsePair(ind = vars, val = coef)] ,
senses=["L"],
rhs=[constantValue] )
的图像。我试图在我的1.png
上显示它,但是当我运行代码时,框架运行但没有图像。这是我的代码:
JFrame
}
限制:
我不能在这里使用
package practice; import java.awt.*; import java.awt.image.*; import java.io.*; import javax.imageio.*; import javax.swing.*; public class Practice { public static void main(String[] args) { Jf ob = new Jf(); ob.setVisible(true); } } public class Jf extends javax.swing.JFrame { BufferedImage img = null; public Jf() { initComponents(); try { img = ImageIO.read(new File("1.png")); } catch (IOException e) { } Graphics g = img.getGraphics(); g.drawImage(img, 0, 0, rootPane); } //below the code is auto-generated by netbeans , i did nothing /** * 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() { setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 400, Short.MAX_VALUE) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 300, Short.MAX_VALUE) ); pack(); }// </editor-fold> /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(Jf.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(Jf.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(Jf.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(Jf.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Jf().setVisible(true); } }); } // Variables declaration - do not modify // End of variables declaration
所以,请提及我的错误,并尝试编写更正,以便我能够轻松理解。 提前谢谢。
答案 0 :(得分:1)
使用JPanel。将其包含在JFrame类中:
public static class CustomPanel extends javax.swing.JPanel {
CustomPanel(width, height) {
super(width, height);
}
void paintComponent(Graphics g) {
g.drawImage(img, 0, 0, null);
}
}
在您的主要方法中,请包含以下内容:
ob.add(new CustomPanel(ob.getWidth(), ob.getHeight()));
答案 1 :(得分:1)
首先,查看图像的路径。您需要将图像放在项目根目录中。 (或者您可以使用绝对路径,如&#34; C:/.../ yourfile&#34;)。
不要让异常为空。编写e.printStackTrace(),因为在这里你无法看到是否抛出了异常。
您需要将代码放入窗格的paintComponent中。