为什么我不能编辑我的JButton的位置?

huangapple go评论121阅读模式
英文:

Why can I not edit the location of my JButton?

问题

public class launcher implements ActionListener {

    private static JFrame window;
    private static JPanel panel; 

    private JButton createPassword;
    private JButton seePassword;

    public launcher() {

        window = new JFrame(); 
        panel = new JPanel();

        window.setTitle("Password Vault");
        window.setSize(400, 260);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setLocationRelativeTo(null);
        window.setResizable(false);
        window.setVisible(true);
        window.getContentPane().add(panel); // Adds panel to JFrame

        createPassword = new JButton("Create Password");
        createPassword.setBounds(20, 100, 150, 100); // 设置按钮位置和大小
        createPassword.addActionListener(this);

        seePassword = new JButton("View Password");
        seePassword.setBounds(20, 50, 150, 100); // 设置按钮位置和大小
        seePassword.addActionListener(this);

        panel.setLayout(null); // 使用绝对布局
        panel.add(createPassword);
        panel.add(seePassword);
    }

    public static void main(String[] args) {

        new launcher();
    }
}

请注意,在代码中,我添加了注释以解释如何使用 setBounds 方法来设置按钮的位置和大小,并使用 setLayout(null) 来将面板的布局设置为绝对布局,以便手动控制组件的位置。

英文:

Code:

public class launcher implements ActionListener {

private static JFrame window;
private static JPanel panel; 

private JButton createPassword;
private JButton seePassword;

public launcher() {
	
	window = new JFrame(); 
	panel = new JPanel();
	
	window.setTitle("Password Vault");
	window.setSize(400, 260);
	window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	window.setLocationRelativeTo(null);
	window.setResizable(false);
	window.setVisible(true);
	window.getContentPane().add(panel); // Adds panel to JFrame
	
	createPassword = new JButton("Create Password");
	createPassword.setBounds(20, 100, 150, 100);
	createPassword.addActionListener(this);
	
	seePassword = new JButton("View Password");
	seePassword.setLocation(20, 50);
	seePassword.addActionListener(this);
	
	panel.add(createPassword);
	panel.add(seePassword);
}

public static void main(String[] args) {

	new launcher();
}

Why can I not change the location of my JButtons? I have tried the setBounds and setLocation function but my buttons still stay on the top middle part of the JFrame window. I have also tried declaring my buttons inside the launcher() method and declaring them as a static variable.

答案1

得分: 1

你不应该尝试设置按钮的大小和位置。

Swing被设计用于与布局管理器一起使用。布局管理器将根据布局管理器的规则设置按钮的大小和位置。

JPanel的默认布局管理器是FlowLayout,这就是为什么您看到按钮居中显示的原因。

如果您想要以不同的方式定位按钮,那么您需要更改布局管理器。

阅读Swing教程中关于布局管理器的部分以获取更多信息和示例。

看起来您希望垂直显示按钮,所以也许可以根据您的确切需求使用BoxLayoutGridLayout

您的代码还有其他问题:

  1. 类名应以大写字母开头。
  2. 没有必要使用静态变量。这不是应该使用静态关键字的方式。
  3. 在使框架可见之前应将组件添加到框架中。
  4. 您应该在框架上调用pack(),然后再调用setVisible(...),以便组件可以以其首选大小显示。
英文:

You should NOT attempt to set the size/location of your buttons.

Swing was designed to be used with layout managers. The layout manager will set the size/location of the button based on the rules of the layout manager.

The default layout manager for a JPanel is the FlowLayout, which is why you see the button centered.

If you want to position the buttons differently, then you need to change the layout manager.

Read the section from the Swing tutorial on Layout Managers for more information and examples.

It looks like you want the buttons displayed vertically, so maybe a BoxLayout or GridLayout can be used depending on your exact requirement.

Other issues with your code:

  1. Class names should start with an upper case character
  2. There is no need to use static variables. That is not how the static keyword should be used
  3. Components should be added to the frame BEFORE the frame is made visible.
  4. You should be invoking pack() on the frame, BEFORE invoking setVisible(...) so the components can be displayed at their preferred size.

答案2

得分: -1

如果您在没有布局管理器的情况下改变组件的位置,确实不应该这样做。但是,有一种方法。如果您将以下代码行放入您的代码中,它应该与 setBounds 一起工作:

window.setLayout(null);
英文:

While you really shouldn't change the place of your components without a layout manager, there is a way. If you put this line of code into your code, it should work with setBounds:

window.setLayout(null);

huangapple
  • 本文由 发表于 2020年8月6日 09:13:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/63275510.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定