Java JFrame:如何禁用窗口大小调整?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18031704/
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
JFrame: How to disable window resizing?
提问by Zulfiqar Junejo
I am creating a JFrameand I call the method setSize(500, 500). Now the desired behaviour is that JFrame should not be resized by user in any condition. Either by maximizing or by dragging the borders. It should be 500x500. How can I do it? I have also attached the code in case you can guide me better.
我正在创建一个JFrame并调用该方法setSize(500, 500)。现在所需的行为是用户在任何情况下都不应该调整 JFrame 的大小。通过最大化或拖动边框。它应该是 500x500。我该怎么做?我还附上了代码,以防您可以更好地指导我。
package com.techpapa;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MainWindow extends JFrame{
private JTextField
write;
private JRadioButton
rb1,
rb2,
rb3;
private ButtonGroup
bg;
private ActionListener al = new ActionListener(){
public void actionPerformed(ActionEvent e){
write.setText("JRadioButton : " + ((JRadioButton)e.getSource()).getText());
}
};
public MainWindow(){
//Frame Initialization
setSize(500, 500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(null);
setTitle(".:JRadioButton:.");
setVisible(true);
//Components Initialization
write = new JTextField(20);
write.setEditable(false);
rb1 = new JRadioButton("Male", false);
rb1.addActionListener(al);
rb2 = new JRadioButton("Female", false);
rb2.addActionListener(al);
rb3 = new JRadioButton("I don't want to specify", true);
rb3.addActionListener(al);
bg = new ButtonGroup();
//Add radio buttons to buttongroup
bg.add(rb1); bg.add(rb2); bg.add(rb3);
//Add to window
add(write);
write.setBounds(140, 100, 150, 20);
write.setDragEnabled(true);
add(rb1);
rb1.setBounds(180, 200, 100, 30);
add(rb2);
rb2.setBounds(180, 225, 100, 30);
add(rb3);
rb3.setBounds(180, 250, 130, 30);
}
public static void main(String[] args) {
new MainWindow();
}
}
采纳答案by tbodt
You can use a simple call in the constructor under "frame initialization":
您可以在“框架初始化”下的构造函数中使用简单的调用:
setResizable(false);
After this call, the window will not be resizable.
在此调用之后,窗口将无法调整大小。
回答by mael
Use setResizableon your JFrame
在 JFrame 上使用setResizable
yourFrame.setResizable(false);
But extending JFrame is generally a bad idea.
但是扩展 JFrame 通常是一个坏主意。
回答by Caffe Latte
it's easy to use:
它易于使用:
frame.setResizable(false);
回答by Butani Vijay
This Code May be Help you : [ Both maximizing and preventing resizing on a JFrame ]
此代码可能对您有所帮助:[在 JFrame 上最大化和防止调整大小]
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setVisible(true);
frame.setResizable(false);
回答by Ipsit Gaur
Simply write one line in the constructor:
只需在构造函数中写一行:
setResizable(false);
This will make it impossible to resize the frame.
这将导致无法调整框架的大小。
回答by Deepak S. Gavkar
You can use this.setResizable(false);or frameObject.setResizable(false);
您可以使用this.setResizable(false);或frameObject.setResizable(false);
回答by Yaroslav
Just in case somebody didn't understand the 6th time around:
以防万一有人不理解第 6 次:
setResizable(false);

