简体   繁体   English

java jframe中的按钮不会调整大小吗?

[英]Button in java jframe will not resize?

I started java programming yesterday, and have developed this. 我昨天开始Java编程,并且已经开发了这个程序。 I have run into a problem, as the button will not resize. 我遇到了问题,因为按钮无法调整大小。 Please help if you can and thank you in advance. 如果可以的话请帮助,并提前谢谢。

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

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. 任何JFrame.add(component)本质上都会将您的组件添加到JFrame的内容窗格中。 After setting layout to null you have added the bgPanel as content pane to the JFrame, which has BorderLayout as its layout manager. 将layout设置为null您已将bgPanel作为内容窗格添加到JFrame,该JFrame具有BorderLayout作为其布局管理器。 Adding your button to the content pane ie, bgPanel will add your registerButton with BorderLayout.Center constraint. 将按钮添加到内容窗格,即bgPanel将添加带有BorderLayout.Center约束的registerButton That is why this button is expanding to the size of the screen. 这就是为什么此按钮扩展到屏幕大小的原因。

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. 在您自己的示例中,您无法找到Button扩展到窗口大小的原因。 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.... 好了,您将能够发现浪费时间的问题,并通过设置xxxSizesetLocationsetBounds等来解决,但是...

people can be rich in money, they can't be rich in time. 人们可以有很多钱,他们不能有很多时间。

Start learning LayoutManager: Lesson: Laying Out Components Within a Container 开始学习LayoutManager: 课程:在容器中布置组件

Try to use registerButton.setSize(new Dimension(width, height)) instead of setBounds . 尝试使用registerButton.setSize(new Dimension(width, height))代替setBounds Remember to replace width and height for new values 切记将widthheight替换为新值

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. 布局并不难,实际上很容易。

Don't use a null layout!!! 不要使用空布局!!!

Swing was designed to be used with layout managers. Swing旨在与布局管理器一起使用。 And don't forget to follow Mike's suggestion. 并且不要忘记遵循迈克的建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM