//在组件中显示信息
import javax.swing.*;
import java.awt.*;
public class Main
{
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
JFrame frame = new NotHelloWorldFrame();//创建一个框架
frame.setTitle("NotHelloWorld");
frame.setSize(400,400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
class NotHelloWorldFrame extends JFrame
{
public NotHelloWorldFrame()
{
add(new NotHelloWorldComponent());//放入组件
pack();//使用组件的首选大小
}
}
class NotHelloWorldComponent extends JComponent
{
public static final int MESSAGE_X = 75;
public static final int MESSAGE_Y = 200;
private static final int DEFAULT_WIDTH = 300;
private static final int DEFUULT_HEIGHT = 200;
//在组件中绘图
public void paintComponent(Graphics g)
{
g.drawString("Not a Hello, World program", MESSAGE_X, MESSAGE_Y);
}
//返回组件的首选大小~V~没懂????
public Dimension getPreferendsize(){return new Dimension(DEFAULT_WIDTH, DEFUULT_HEIGHT);}
}
Java——图形程序设计2之在组件中显示信息
最新推荐文章于 2020-03-08 21:51:27 发布