按钮在Applet java中有位置吗?
我怎样才能把按钮放在正确的地方 我的代码:按钮在Applet java中有位置吗?,java,button,layout,applet,awt,Java,Button,Layout,Applet,Awt,我怎样才能把按钮放在正确的地方 我的代码: 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 Flo
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) {
}
}
例如,我的小程序中几乎没有标签和图像。我想把按钮放在某个地方。。
我还想设置按钮的大小,但是方法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);
您也可以使用
BorderLayout
尝试,只需在newJPanel
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);
要将按钮放置在何处?尝试执行
button1.设置位置(x,y)代码>然后还执行button1.setBounds(x,y,button1.getWidth(),button1.getHeight())代码>x和y值应该相同例如:@3kings请不要在Java GUI上给出建议,直到你意识到你的建议是多么不可行。是的,但我想把按钮放在某个地方,例如:好的,让我更改我的答案。对不起,但什么是img?根据你的快照,你有一个图像和按钮。这里,img表示设置了图标的标签。