why the height and width of the button in applet java is not changed using setBounds() method?

为什么Java Applet按钮的高度和宽度不使用setbounds()方法改变了吗?

Tags: java applet
标签: java applet

问题 (Question)

method setBounds(x, y, width, height) to change the position of the button is worked but width and height is not changed.

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

public class ButtonDemo extends Applet implements ActionListener {

    public void init() {
        setBounds(0, 30, 30, 60);
        Button yes = new Button("Yes");
        add(yes);
        yes.addActionListener(this);
    }

    public void actionPerformed (ActionEvent ae) {

    }
}

方法setBounds(x, y, width, height)改变按钮的位置,宽度和高度的工作但不改变。

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

public class ButtonDemo extends Applet implements ActionListener {

    public void init() {
        setBounds(0, 30, 30, 60);
        Button yes = new Button("Yes");
        add(yes);
        yes.addActionListener(this);
    }

    public void actionPerformed (ActionEvent ae) {

    }
}

最佳答案 (Best Answer)

In order to change the bounds of the button itself you need to apply it to the Button object:

Button yes = new Button("Yes");
yes.setBounds(0, 30, 30, 60);

What you're doing now only changes the bounds of the container, not the button.

EDIT: I'm not sure setBounds() is even supposed to work for a JButton. You should use JButton.setLocation() and JButton.setSize() instead.

SECOND EDIT: You should look at layout managers instead of using the setBounds(), setLocation(), or setSize() methods. It will be much easier and more stable.

为了改变按钮本身你需要应用到按钮对象的边界:

Button yes = new Button("Yes");
yes.setBounds(0, 30, 30, 60);

你现在做的只是改变容器的边界,没有按钮。

编辑:我不确定setBounds()甚至应该为一个JButton的工作。你应该使用JButton.setLocation()JButton.setSize()布局管理器

而不是使用setBounds(),或setLocation()方法。setSize()方法。这将是更容易和更稳定的。

答案 (Answer) 2

You must set the layout of your applet in order to size buttons added to it.

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class ButtonDemo extends Applet implements ActionListener {


    public void init() {
        setLayout(null)
        Button yes= new Button("Yes");
        yes.setBounds(0,30,30,60);


        add(yes);
        yes.addActionListener(this);
    }


    public void actionPerformed (ActionEvent ae) {

    }
}

Although I HIGHLY suggest you looking into the layouts Oracle has provided you, but if you just want to set bounds, you can set layout to null

你必须为你的程序集的大小按钮布局添加到它的。

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class ButtonDemo extends Applet implements ActionListener {


    public void init() {
        setLayout(null)
        Button yes= new Button("Yes");
        yes.setBounds(0,30,30,60);


        add(yes);
        yes.addActionListener(this);
    }


    public void actionPerformed (ActionEvent ae) {

    }
}

但我建议你考虑布局,Oracle提供了你,但如果你只是想设定界限,你可以设置为null布局

本文翻译自StackoverFlow,英语好的童鞋可直接参考原文:http://stackoverflow.com/questions/23452165