您的位置:首页 > 编程语言 > Java开发

禁止改变窗体大小以及图标Java

2017-05-20 09:53 686 查看
禁止改变大小

public class ControlFormSize extends JFrame{

public ControlFormSize(){//构造方法
setTitle("设置窗体大小");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置关闭方式
setSize(400,300);
JPanel contentPane=new JPanel();//创建面板对象
contentPane.setLayout(new BorderLayout(0,0));//设置布局
setContentPane(contentPane);//设置内容面班
JLabel label=new JLabel("宽度:400,高度:300");//标签
contentPane.add(label,BorderLayout.CENTER);
JButton b=new JButton("禁值改变窗体大小");
contentPane.add(b,BorderLayout.NORTH);
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
do_button_actionPerformed(e);
}
});

}

 public void do_button_actionPerformed(ActionEvent e){
setResizable(false);

 }
 
public static void main(String[] args) {
ControlFormSize cf=new ControlFormSize();
cf.setVisible(true);
}

}

图标在窗口显示一个圆形图标

public class DrawIcon implements Icon{

private int width;

private int height;

public int getIconHeight(){
return this.height;

}

public int getIconWidth(){
return this.width;

}

public DrawIcon(int width,int height){
this.width=width;
this.height=height;

}

public void paintIcon(Component arg0,Graphics arg1,int x,int y){
arg1.fillOval(x,y,width,height);//绘制一个圆形

}

public static void main(String[] args) {
DrawIcon icon=new DrawIcon(15,15);
JLabel j=new JLabel("测试",icon,SwingConstants.CENTER);
JFrame jf=new JFrame();
Container c=jf.getContentPane();
j.setIcon(icon);//为标签设置图标
c.add(j);
jf.setSize(250,200);//窗体大小,通过jframe对象实例化
jf.setVisible(true);//窗体显示

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: