2016.11.05
前面我们学习java的Swingj界面时,出来的框都是固定的,并且不是很好看,如果我们想改变一下,那应该怎么办呢?
查找资料发现是可以做到的。
用setUndecorated可以去掉窗口的修饰
import java.awt.Button;
import java.awt.Shape;
import java.awt.geom.Area;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import javax.swing.JFrame;
import com.sun.awt.AWTUtilities;
public class MyShape {
public void specialUI(){
JFrame jf = new JFrame();
jf.setSize(400,400);
//加按钮
Button bu=new Button("登录");
jf.setUndecorated(true);//去掉窗体的修饰
jf.add(bu);
Shape elipes=new Ellipse2D.Float(0,0,300,300);//圆
Shape rect =new Rectangle2D.Double(0,0,100,100);//矩形
Area ar1=new Area(elipes);
Area ar2=new Area(rect);
//两个图形合在一起
ar1.add(ar2);
/*
* When using com.sun.awt.AWTUtilities, Eclipse gives a error:
- Access restriction: The method setWindowOpacity(Window, float) from the type AWTUtilities is not accessible due to restriction on required library C:\Program Files\Java\jre6\lib\rt.jar
- Access restriction: The type AWTUtilities is not accessible due to restriction on required library C:\Program Files\Java\jre6\lib\rt.jar
You can fix it like this:
Preferences -> Java -> Compiler -> Errors/Warnings -> Deprecated and restricted API ->
Forbidden reference -> change "error" to "ignore"
*/
AWTUtilities.setWindowShape(jf,ar1);//设置窗体的形状
jf.setVisible(true);
}
public static void main(String[] args) {
MyShape ud=new MyShape();
ud.specialUI();
}
}
这样出来的结果如下:
另外的代码表示:
public class MyShape extends JFrame {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame.setDefaultLookAndFeelDecorated(true);
MyShape frame = new MyShape();
frame.setSize(new Dimension(200, 300));
/** 设置圆角 */
AWTUtilities.setWindowShape(frame, new RoundRectangle2D.Double(
0.0D, 0.0D, frame.getWidth(), frame.getHeight(), 26.0D,
26.0D));
frame.setVisible(true);
}
});
}
}
路漫漫其修远兮 吾将上下而求索,如果想要做的更好一点,还需付出大量时间。