java jframe 中的按钮不会调整大小?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19572390/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Button in java jframe will not resize?
提问by Auspexis
I started java programming yesterday, and have developed this. I have run into a problem, as the button will not resize. Please help if you can and thank you in advance.
我昨天开始了java编程,并开发了这个。我遇到了一个问题,因为按钮不会调整大小。如果可以,请提供帮助,并提前致谢。
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
class BgPanel extends JPanel {
Image bg = new ImageIcon("C:\Users\********\Pictures\tiger.jpg").getImage();
@Override
public void paintComponent(Graphics g) {
g.drawImage(bg, 0, 0, getWidth(), getHeight(), this);
}
}
public class FrameTestBase extends JFrame {
public static void main(String args[]) {
JPanel bgPanel = new BgPanel();
bgPanel.setLayout(new BorderLayout());
final FrameTestBase t = new FrameTestBase();
ImageIcon img = new ImageIcon("C:\Users\********\Pictures\gear-icon.png");
t.setLayout(null);
t.setIconImage(img.getImage());
t.setTitle("Login");
t.setSize(600,600);
t.setLocationRelativeTo(null);
t.setContentPane(bgPanel);
t.setDefaultCloseOperation(EXIT_ON_CLOSE);
t.setVisible(true);
JButton registerButton = new JButton("register");
registerButton.setBounds(80, 80, 80, 80);
t.add(registerButton);
}
}
采纳答案by Sage
I have run into a problem, as the button will not resize. Please help if you can and thank you in advance.
我遇到了一个问题,因为按钮不会调整大小。如果可以,请提供帮助,并提前致谢。
bgPanel.setLayout(new BorderLayout());
// --------- your other code
t.setLayout(null);
//--------------- your other code
t.setContentPane(bgPanel); // you are setting bgPanel which has BorderLayout
JButton registerButton = new JButton("register");
registerButton.setBounds(80, 80, 80, 80);
t.add(registerButton); // t is the JFrame, your main window
Any JFrame.add(component)
will essentially add your component to the content pane of the JFrame. After setting layout to null
you have added the bgPanel
as content pane to the JFrame, which has BorderLayout as its layout manager. Adding your button to the content pane i.e.,bgPanel
will add your registerButton
with BorderLayout.Center
constraint. That is why this button is expanding to the size of the screen.
AnyJFrame.add(component)
基本上会将您的组件添加到 JFrame 的内容窗格中。将布局设置为null
您已将bgPanel
作为内容窗格添加到 JFrame,它具有 BorderLayout 作为其布局管理器。将您的按钮添加到内容窗格即bgPanel
会添加您registerButton
的BorderLayout.Center
约束。这就是这个按钮扩展到屏幕大小的原因。
As you are so eager to see an output do the following:
由于您非常渴望看到输出,请执行以下操作:
// registerButton.setBounds(80, 80, 80, 80); comment out this line
registerButton.setPreferedSize(new Dimension(80, 80));
t.add(registerButton, BorderLayout.PAGE_START)
Now, About using NULL Layout:
现在,关于使用 NULL 布局:
In your own example you have lost to find the reason why the Button is expanding to the window size. In near future you will see that one of your component has head but lost its tail by going outside of the window border. You will see that one of your component is going to jump over the other without no reason. You will see that you have changed a position of component with relative to another component, but it will make relation with other component. Well you will be able to find the issues wasting lost of time and get fixed by setting xxxSize
, setLocation
, setBounds
etc but....
在您自己的示例中,您找不到 Button 扩展到窗口大小的原因。在不久的将来,您将看到您的一个组件有头部,但通过超出窗口边界而失去了尾部。您将看到您的一个组件将无缘无故地跳过另一个组件。您会看到您已经更改了组件相对于另一个组件的位置,但它会与其他组件建立关系。那么你就可以找到问题消瘦失去的时间,只需设置固定xxxSize
,setLocation
,setBounds
等,但....
people can be rich in money, they can't be rich in time.
人可以富于金钱,却不能富于时间。
Start learning LayoutManager: Lesson: Laying Out Components Within a Container
开始学习 LayoutManager:课程:在容器内布置组件
回答by Gabriel Camara
Try to use registerButton.setSize(new Dimension(width, height))
instead of setBounds
.
Remember to replace width
and height
for new values
尝试使用registerButton.setSize(new Dimension(width, height))
而不是setBounds
. 记住要替换width
和height
换新值
And I forget to say the same thing guys are telling you:
我忘了说同样的事情人们告诉你:
Don't use null layout.
不要使用空布局。
The sooner you learn, the better.
Layouts are not difficult, they're actually easy.
越早学习越好。
布局并不难,实际上很容易。
回答by camickr
Don't use a null layout!!!
不要使用空布局!!!
Swing was designed to be used with layout managers. And don't forget to follow Mike's suggestion.
Swing 旨在与布局管理器一起使用。并且不要忘记遵循迈克的建议。