JFrame:如何禁用窗口大小调整? [英] JFrame: How to disable window resizing?
本文介绍了JFrame:如何禁用窗口大小调整?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在创建 JFrame
,我调用方法 setSize(500,500)
。现在,期望的行为是用户不应在任何条件下调整JFrame的大小。通过最大化或拖动边框。它应该是500x500。我该怎么做?我还附上了代码,以防你更好地指导我。
I am creating a JFrame
and 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.
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();
}
}
推荐答案
你可以在帧初始化下的构造函数中使用一个简单的调用:
You can use a simple call in the constructor under "frame initialization":
setResizable(false);
此次通话后,窗口将无法调整大小。
After this call, the window will not be resizable.
这篇关于JFrame:如何禁用窗口大小调整?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文