201671010131 2016-2017-2 《Java程序设计》

本周学习了图形程序设计。

基本内容有:简单的图形框架的构建。

EventQueue.invokeLater(() ->
{
SimpleFrame frame = new SimpleFrame();

frame.setTitle();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
});

private static String message;

public static String getMessage() {
return message;
}
public SimpleFrame()
{
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
}

图形提示语的编写: fra.setTitle("welcome!");//窗口提示语

框架定位:

// set frame width, height and let platform pick screen location

setSize(screenWidth /2, screenHeight /2);//长,宽,为屏幕的一半,
setLocationByPlatform(true);//决定框架所放的位置。

在组件中显示信息:

public void paintComponent(Graphics g)//描述绘制组件的方法。
{
Graphics2D g2 = (Graphics2D) g;

// draw a rectangle

double leftX = 100;
double topY = 100;
double width = 200;
double height = 150;。。。。}

还有组件中图形的构建:

public DrawFrame()
{
add(new DrawComponent());//将一个给定组件添加到内容窗格中,本题为矩形。
pack();//调整窗口大小。
}

Rectangle2D rect = new Rectangle2D.Double(leftX, topY, width, height);
g2.draw(rect);

 g2.draw(new Line2D.Double(leftX, topY, leftX + width, topY + height));//。左上角的位置和长高。

可以生成多个窗口,但是不能做成多层的。

原文地址:https://www.cnblogs.com/tgg1314/p/7823426.html