Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There's several issues with the code you've posted:</p> <ul> <li>You should use <code>getContentPane().add()</code> instead of simply <code>add()</code> in your <code>BorderLayoutFrame</code> class.</li> <li>You should really use <code>SwingUtilities.invokeLater()</code> to launch your JFrame from the tester class. Something like this:</li> </ul> <hr> <pre><code> SwingUtilities.invokeLater(new Runnable() { @Override public void run() { System.setProperty("DEBUG_UI", "true"); BorderLayoutFrame blf = new BorderLayoutFrame(); blf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); blf.setSize(600,600); blf.setVisible(true); } }); </code></pre> <ul> <li>Don't use Toolkit to load images! In the following code, if "Table.jpg" is in the same package as PicPanel, the image will correctly load. </li> </ul> <hr> <pre><code>public PicPanel() { super(); try { rUrl = getClass().getResource("Table.jpg"); if (rUrl != null) { img = ImageIO.read(rUrl); } } catch (IOException ex) { Logger.getLogger(PicPanel.class.getName()).log(Level.SEVERE, null, ex); } } </code></pre> <ul> <li>In <code>PicPanel.PaintComponent()</code> you call <code>super.paintComponents()</code> is the 's' a typeo?</li> <li><p>In <code>PicPanel.PaintComponent()</code>, you <strong>don't</strong> need all the width/height stuff, just do this:</p> <p><code>g.drawImage(img, 0, 0, getWidth(), getHeight(), this);</code></p></li> </ul> <p>And avoid the call to super.paintComponent all together because you're painting an image, why do you want the panel to paint at all?</p> <p>My final implementation of your stuff:</p> <pre><code>public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { System.setProperty("DEBUG_UI", "true"); BorderLayoutFrame blf = new BorderLayoutFrame(); blf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); blf.setSize(600,600); blf.setVisible(true); } }); } } class BorderLayoutFrame extends JFrame implements ActionListener { private final BorderLayout layout; private final JButton[] buttons; private final String names[] = {"North", "South", "East", "West", "Center"}; public BorderLayoutFrame() { super( "Philosofic Problem" ); layout = new BorderLayout( 5, 5 ); getContentPane().setLayout( layout ); buttons = new JButton[ names.length ]; for (int i=0; i&lt;names.length; i++) { buttons[i] = new JButton(names[i]); buttons[i].addActionListener(this); } getContentPane().add(buttons[0], BorderLayout.NORTH); getContentPane().add(buttons[1], BorderLayout.SOUTH); getContentPane().add(buttons[2], BorderLayout.EAST); getContentPane().add(buttons[3], BorderLayout.WEST); getContentPane().add(new PicPanel(), BorderLayout.CENTER); } public void actionPerformed(ActionEvent e) { // ignore } } class PicPanel extends JPanel { private URL rUrl; private BufferedImage img; public PicPanel() { super(); try { rUrl = getClass().getResource("UtilBtn.png"); if (rUrl != null) { img = ImageIO.read(rUrl); } } catch (IOException ex) { Logger.getLogger(PicPanel.class.getName()).log(Level.SEVERE, null, ex); } } @Override protected void paintComponent(Graphics g) { //super.paintComponent(g); g.drawImage(img, 0, 0, getWidth(), getHeight(), this); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload