GridBagLayout wczytanie obrazka (pomocy)

0

witam. chcę wczytać obrazek jako fragment GridBagLayout niestety kod nie działa prawidłowo (obrazek nie zostaje wczytany ) .
KOD:
import java.awt.*;

import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

class Obraz extends JPanel{
JButton but=new JButton("Przycisk");

public void paintComponent(Graphics g){
	MediaTracker tr=new MediaTracker(this);
	Image im=Toolkit.getDefaultToolkit().getImage("D:/mug.gif");
	tr.addImage(im, 1);
	try{
	tr.waitForID(1);
	}catch(InterruptedException e){
		
		Thread.currentThread().interrupt();
	}
g.drawImage(im, 0, 0,300,300, this);
}

}
public class GridBagLayout {
final static boolean shouldFill = true;
final static boolean shouldWeightX = true;
final static boolean RIGHT_TO_LEFT = false;

public static void addComponentsToPane(Container pane) {
    if (RIGHT_TO_LEFT) {
        pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
    }

    JButton button;
pane.setLayout(new java.awt.GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
if (shouldFill) {
//natural height, maximum width
c.fill = GridBagConstraints.HORIZONTAL;
}

Obraz ob=new Obraz();


if (shouldWeightX) {
c.weightx = 0.5;
}

// c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
pane.add(ob,c);
button = new JButton("Button 2");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 1;
c.gridy = 0;
pane.add(button, c);

button = new JButton("Button 3");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 2;
c.gridy = 0;
pane.add(button, c);

button = new JButton("Long-Named Button 4");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 40;      //make this component tall
c.weightx = 0.0;
c.gridwidth = 3;
c.gridx = 0;
c.gridy = 1;
pane.add(button, c);

button = new JButton("5");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 0;       //reset to default
c.weighty = 1.0;   //request any extra vertical space
c.anchor = GridBagConstraints.PAGE_END; //bottom of space
c.insets = new Insets(10,0,0,0);  //top padding
c.gridx = 1;       //aligned with button 2
c.gridwidth = 2;   //2 columns wide
c.gridy = 2;       //third row
pane.add(button, c);
}

/**
 * Create the GUI and show it.  For thread safety,
 * this method should be invoked from the
 * event-dispatching thread.
 */
private static void createAndShowGUI() {
    //Create and set up the window.
    JFrame frame = new JFrame("GridBagLayoutDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Set up the content pane.
    addComponentsToPane(frame.getContentPane());

    //Display the window.
    frame.pack();
    frame.repaint();
    frame.setVisible(true);
}

public static void main(String[] args) {
    //Schedule a job for the event-dispatching thread:
    //creating and showing this application's GUI.
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
        }
    });
}

}

Przykład działa natomiast w zwykłej ramce JFrame. Co robię źle ? Pomocy

0

chyba że ma ktoś jakiś przykładowy GridBagLayout z obrazkiem to bardzo bym prosił o umieszczenie kodu źródłowego.

1 użytkowników online, w tym zalogowanych: 0, gości: 1