문제

I am trying to use the gridbagconstraints in a class that extends JPanel but after putting that codes like ths :

    import java.awt.Insets;
    import java.awt.Toolkit;

    import javax.swing.JLabel;
    import javax.swing.JPanel;

    public class RightPanel extends JPanel {

private static final long serialVersionUID = 2L;

JLabel start;
JLabel first;

GridBagConstraints c;

public RightPanel() {

    setVisible(true);

    c = new GridBagConstraints();
    c.insets = new Insets(10 ,10, 10, 10);

    start = new JLabel("Start");
    start.setFont(new Font("comic sans ms", Font.PLAIN, 20));
    start.setForeground (new Color(111,0,0));

    c.gridx = 0;
    c.gridy = 5;

    add(start, c);

    first = new JLabel("About");
    first.setFont(new Font("comic sans ms", Font.PLAIN, 20));
    first.setForeground (new Color(111,0,0));

    c.gridx = 0;
    c.gridy = 5;
    add(first ,c);
}

public void paint(Graphics g) {
    Image a = Toolkit.getDefaultToolkit().getImage("C:\\Users\\Favour's Computer\\workspace\\Physics Calculator\\src\\res\\rightside.PNG");
    g.drawImage(a, 0, 0, getSize().width, getSize().height, this);
    super.paint(g);
}

    }

Now the problem is where to add the declaration code in the jpanel constucter i.e RightPanel rp = new RightPanel(new GridBagLayout());, but the new GridBagLayout() dosent work, please help

도움이 되었습니까?

해결책

RightPanel rp = new RightPanel(new GridBagLayout());

The code above will not work because you don't have any parameter in your constructor..

public class RightPanel extends JPanel

//codes...

public RightPanel() //You don't have any parameter in your constructor

If you are trying to set the Layout of your class use

setLayout(new GridBagLayout())
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top