gpt4 book ai didi

java - Applet java 中按钮有位置吗?

转载 作者:行者123 更新时间:2023-12-01 13:09:31 26 4
gpt4 key购买 nike

如何将按钮放在正确的位置?

我的代码:

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) {
}

}

例如,我的 Applet 中几乎没有标签和图像。我想把按钮放在某个地方..而且我想设置按钮的大小,但方法 setSize() 和方法 setBounds() 不起作用..

最佳答案

你尝试过吗?

add(button1,BorderLayout.EAST);
<小时/>

如果有多个组件,可以尝试GridBagLayout

    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);
<小时/>

您也可以通过在新的 JPanel 中添加按钮来尝试使用 BorderLayout

    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);

关于java - Applet java 中按钮有位置吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22995743/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com