badCoder
badCoder

Reputation: 770

Do button have a placement in Applet java?

How can I put a button in the right place ?

My code:

import java.applet.*;
import java.awt.*;

public class Banner extends Applet {

int x, y;

Button button1 = new Button("Try!");

public void init() {
    setSize(1200, 500);
    setLayout(new BorderLayout());  //also I tried FlowLayout..

            //button1.setBounds(500, 250, 25, 50); // not worked..

    add("East", button1); 

    button1.addActionListener(this);
}

public void start() {
}

public void paint(Graphics g) {
}

}

For example I have few labels and images in my Applet. And I want put the button somewhere.. And also I want to set size the button, but method setSize() and method setBounds() isn't worked..

Upvotes: 0

Views: 110

Answers (1)

Braj
Braj

Reputation: 46841

Have you tried?

add(button1,BorderLayout.EAST);

you can try GridBagLayout if there are multiple components

    setLayout(new GridBagLayout());
    GridBagConstraints gc = new GridBagConstraints();

    gc.gridy = 0;
    gc.anchor = GridBagConstraints.NORTH;

    Image image = ImageIO.read(new File("resources/Tulips.jpg"));
    JLabel label = new JLabel(new ImageIcon(image));
    JButton button1 = new JButton("Try!");

    gc.gridx = 0;
    gc.insets = new Insets(5, 5, 5, 5);
    add(label, gc);

    gc.insets = new Insets(50, 5, 5, 50);
    gc.gridx = 1;
    add(button1, gc);

You can try it with BorderLayout also by just adding button in new JPanel

    Image image = ImageIO.read(new File("resources/Tulips.jpg"));
    JLabel label = new JLabel(new ImageIcon(image));

    setLayout(new BorderLayout(10,10));
    add(label, BorderLayout.CENTER);

    JPanel panel=new JPanel(new FlowLayout(FlowLayout.CENTER,50,10));
    JButton button1 = new JButton("Try!");
    panel.add(button1);
    add(panel, BorderLayout.EAST);

Upvotes: 1

Related Questions