我正在创建一个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();
}
}