楼主的代码有以下几个需要注意的问题:一、控件重复 add 你的代码中有两次调用了 north.add(mini); 这句话 相当于同一个控件被重复两次添加到同一个面板上 其实这样是无法实现添加两个控件的效果的 最终只能有一个 mini 存在于这个面板上 如果一定要这么写,应该这样写: JLabel mini = new JLabel("mini"); north.add(easy); north.add(mini); north.add(abnormal);mini = new JLabel("mini"); north.add(mini);二、重载了重要的父类方法,导致 JFrame 无法正确的展现 你的类中有这样两个方法: public int getWidth() public int getHeight() 它们看起来人畜无害,只是你一个内部变量的 get 方法 其实你却没有意识到,他们覆盖了父类中的同名方法 造成 JFrame 无法正确获得窗体的大小进行展示 再者,既然你这两个成员变量是——静态,公共的,何必写 get、set 方法?
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
FlowLayout fl=new FlowLayout(FlowLayout.LEFT);
//fl.setHgap();
//fl.setVgap(500);
JPanel north=new JPanel();
north.setLayout(new FlowLayout(FlowLayout.LEFT));
ButtonGroup bg=new ButtonGroup();
easy=new JRadioButtonMenuItem("简单(6*6网格)");
easy.setSize(25, 20);
abnormal=new JRadioButtonMenuItem("正常(12*12网格)");
abnormal.setSize(25, 20);
hard=new JRadioButtonMenuItem("困难(18*18网格)");
hard.setSize(25, 20);
autoset=new JRadioButtonMenuItem("自定义((6~24)*(6~24))");
autoset.setSize(25, 20);
bg.add(easy);
bg.add(abnormal);
bg.add(hard);
bg.add(autoset);
JLabel mini=new JLabel("mini");
north.add(easy);
//north.add(mini); north.add(abnormal);
//north.add(mini); north.add(hard);
//north.add(new JLabel("mini")); north.add(autoset);
//north.add(new JLabel("mini"));
easy.addActionListener(this);
abnormal.addActionListener(this);
hard.addActionListener(this);
autoset.addActionListener(this);
JLabel width=new JLabel(" 宽 度 :(6-20)");
//width.setSize(15,5);
widthtext=new JTextField(12);
//widthtext.setSize(15, 5);
widthtext.setEditable(false);
JLabel height=new JLabel(" 高 度 :(6-20)");
//height.setSize(15,5);
heighttext=new JTextField(12);
heighttext.setEditable(false);
//heighttext.setSize(15,5);
ensure=new JButton(" 确 定");
cancel=new JButton(" 取 消");
ensure.addActionListener(this);
cancel.addActionListener(this);
north.add(width);
north.add(widthtext);
north.add(height);
north.add(heighttext);
north.add(ensure);
north.add(cancel);
this.add(north,BorderLayout.CENTER);
this.setLocation(720 ,400 );
this.setSize(350,450);
this.setVisible(true);
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JTextField;public class SetFrame extends JFrame implements ActionListener {
static Gui father;
JRadioButtonMenuItem easy;
JRadioButtonMenuItem abnormal;
JRadioButtonMenuItem hard;
JRadioButtonMenuItem autoset;
JTextField widthtext;
JTextField heighttext;
JButton ensure;
JButton cancel;
public static int width=6;
public static int height=6;
SetFrame(){
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
FlowLayout fl=new FlowLayout(FlowLayout.LEFT);
//fl.setHgap();
//fl.setVgap(500); JPanel north=new JPanel();
north.setLayout(new FlowLayout(FlowLayout.LEFT));
//ButtonGroup bg=new ButtonGroup();
easy=new JRadioButtonMenuItem("简单(6*6网格)");
easy.setSize(25, 20);
abnormal=new JRadioButtonMenuItem("正常(12*12网格)");
abnormal.setSize(25, 20);
hard=new JRadioButtonMenuItem("困难(18*18网格)");
hard.setSize(25, 20);
autoset=new JRadioButtonMenuItem("自定义((6~24)*(6~24))");
autoset.setSize(25, 20);
// bg.add(easy);
// bg.add(abnormal);
// bg.add(hard);
// bg.add(autoset);
JLabel mini=new JLabel("mini");
north.add(easy);
north.add(mini); north.add(abnormal);
north.add(mini); north.add(hard);
//north.add(new JLabel("mini")); north.add(autoset);
//north.add(new JLabel("mini"));
easy.addActionListener(this);
abnormal.addActionListener(this);
hard.addActionListener(this);
autoset.addActionListener(this);
JLabel width=new JLabel(" 宽 度 :(6-20)");
//width.setSize(15,5);
widthtext=new JTextField(12);
//widthtext.setSize(15, 5);
widthtext.setEditable(false);
JLabel height=new JLabel(" 高 度 :(6-20)");
//height.setSize(15,5);
heighttext=new JTextField(12);
heighttext.setEditable(false);
//heighttext.setSize(15,5);
ensure=new JButton(" 确 定");
cancel=new JButton(" 取 消");
ensure.addActionListener(this);
cancel.addActionListener(this);
north.add(width);
north.add(widthtext);
north.add(height);
north.add(heighttext);
north.add(ensure);
north.add(cancel);
this.add(north,BorderLayout.CENTER);
this.setLocation(720 ,400 );
this.setSize(350,450);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e){
//System.out.println("aaaa");
if(e.getSource()==easy){
this.widthtext.setEditable(false);
this.heighttext.setEditable(false);
}
if(e.getSource()==abnormal){
this.widthtext.setEditable(false);
this.heighttext.setEditable(false);
}
if(e.getSource()==hard){
this.widthtext.setEditable(false);
this.heighttext.setEditable(false);
}
if(e.getSource()==autoset){
if (autoset.isSelected()) {
this.widthtext.setEditable(true);
this.heighttext.setEditable(true);
}
}
if(e.getSource()==ensure){
if(easy.isSelected()){
this.width=6;
this.height=6;
((Gui)this.father).select(this.height,this.width);
this.dispose();
return;
}
if(abnormal.isSelected()){
this.width=12;
this.height=12;
((Gui)this.father).select(this.height,this.width);
this.dispose();
return;
}
if(hard.isSelected()){
this.width=18;
this.height=18;
((Gui)this.father).select(this.height,this.width);
this.dispose();
return;
}
int Ywidth=0;
int Yheight=0;
try{
Ywidth=Integer.parseInt(this.widthtext.getText());
Yheight=Integer.parseInt(this.widthtext.getText());
}
catch(NumberFormatException el){
JOptionPane.showMessageDialog(this,"请输入有效数字!");
this.heighttext.setText(null);
this.widthtext.setText(null);
return;
}
if(Yheight>=6&&Yheight<=20&&Ywidth>=6&&Ywidth<=20){
this.width=Integer.parseInt(this.widthtext.getText());
this.height=Integer.parseInt(this.heighttext.getText());
((Gui)this.father).select(this.height,this.width);
this.dispose();
}
else
{
JOptionPane.showMessageDialog(this,"请确认高度和宽度为6-20!");
this.heighttext.setText(null);
this.widthtext.setText(null);
}
}
if(e.getSource()==cancel){
this.heighttext.setText(null);
this.widthtext.setText(null);
}
}
public static void main(String []args){
new SetFrame();
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
}
你的代码中有两次调用了 north.add(mini); 这句话
相当于同一个控件被重复两次添加到同一个面板上
其实这样是无法实现添加两个控件的效果的
最终只能有一个 mini 存在于这个面板上
如果一定要这么写,应该这样写:
JLabel mini = new JLabel("mini");
north.add(easy);
north.add(mini);
north.add(abnormal);mini = new JLabel("mini");
north.add(mini);二、重载了重要的父类方法,导致 JFrame 无法正确的展现
你的类中有这样两个方法:
public int getWidth()
public int getHeight()
它们看起来人畜无害,只是你一个内部变量的 get 方法
其实你却没有意识到,他们覆盖了父类中的同名方法
造成 JFrame 无法正确获得窗体的大小进行展示
再者,既然你这两个成员变量是——静态,公共的,何必写 get、set 方法?