Results 1 to 4 of 4
- 03-21-2012, 01:46 AM #1
Member
- Join Date
- Jul 2011
- Posts
- 17
- Rep Power
- 0
Oval does not appear in the panel
I must be doing something wrong and oval is not appearing at all.
Here is the code :
The ide that I am using is netbeans.Java Code:/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package learn; import java.awt.Color; import java.awt.Graphics; import javax.swing.JPanel; /** * * @author skiabox */ public class Oval extends javax.swing.JFrame { /** * Creates new form Oval */ public Oval() { 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() { setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(0, 400, Short.MAX_VALUE) ); layout.setVerticalGroup( layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(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(Oval.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(Oval.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(Oval.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(Oval.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 Oval().setVisible(true); } }); } // Variables declaration - do not modify // End of variables declaration } class NewPanel extends JPanel { public void paintComponent(Graphics g) { g.setColor(getBackground()); g.fillRect(0, 0, getWidth(), getHeight()); g.setColor(Color.RED); g.fillOval(0, 0, getWidth(), getHeight()); } }
-
Re: Oval does not appear in the panel
- 03-21-2012, 10:28 AM #3
Member
- Join Date
- Jul 2011
- Posts
- 17
- Rep Power
- 0
Re: Oval does not appear in the panel
I tried to add
add(new NewPanel());
in the Oval method but nothing happens.
-
Re: Oval does not appear in the panel
I did not see this attempt in the original code you've posted. For this to work, you need to,
- Make sure that the layout of the container accepting your JPanel will display the JPanel.
- this means you should not be using GroupLayout, but rather one of the more user-friendly layout managers. Check the tutorials for information on this.
- Either add your component to the GUI before packing and calling setVisible(true) on the GUI or if done after the GUI has been rendered, calling revalidate() and repaint() on any container that has had its components changed.
- Avoid using NetBeans code generation when you are learning Swing. Instead read the tutorials and code by hand.
Similar Threads
-
OVal 1.80
By java software in forum Java SoftwareReplies: 0Last Post: 10-06-2011, 06:54 PM -
JPanel and Oval/Ellipse Problems
By PorgrammingNoob117 in forum AWT / SwingReplies: 1Last Post: 05-04-2011, 09:59 PM -
how to draw oval using Graphics object
By usha in forum AWT / SwingReplies: 4Last Post: 11-30-2010, 12:15 PM -
Clicking on an oval in JPanel
By chielt in forum Java 2DReplies: 6Last Post: 03-17-2010, 12:08 AM -
OVal 1.10
By JavaBean in forum Java SoftwareReplies: 0Last Post: 11-03-2007, 06:00 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks