[color=blue]***********************在面板中显示信息************************
JFrame中有四层面板: 根面板、层级面板、玻璃面板和内容窗格,swing程序员对前三者不太关心,它们是用
来组织菜单栏和内容窗格以及实现观感的,swing程序员最关心内容窗格,在设计框架的时候,要使用下面代码
把所有组件放到内容窗格中:
Container contentPane = frame.getContentPane();
Componet c= ..;
contentPane.add(c);
在面板上绘图前提:
1.定义一个扩展于JPanel的类
2.在这个类中,覆盖paintComponent方法
eg:
class MyPanel extends JPanel{
public void paintComponent(Graphics g){
..//code for drawing will go here
}
}
只要窗口需要重绘,事件处理器就会通告组件,从而调用painComponent方法
如果需要强制重绘,就需调用repaint方法,而不是painComponent
显示文本是一种特殊的绘图,Graphics类有个drawString 方法,调用的格式为:
g.drawString(text,x,y)
eg:g.drawString(“not a hello world”,100,150);
为了确保超类完成自己那份工作,必须在绘制自己内容前调用super.paintComponent方法
import javax.swing.*;
import java.awt.*;
public class NotHelloWorld{
public static void main(String[] args){
NotHelloWorldFrame frame = new NotHelloWorldFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_CLOSE);
frame.setVisible(true);
}
}
class NotHelloWorldFrame extends JFrame{
public NotHelloWorldFrame(){
setTitle("nothelloworld");
setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
NotHelloWorlPanel panel = new NotHelloWorldPanel();
add(panel);
}
public static final int DEFALUT_WIDTH = 300;
public static final int DEFALUT_HEIGHT = 200;
}
class NotHelloWorldPanel extends JPane{
public void paintComponet(Grapics g);{
super.paintComponet(g);
g.drawString("not a hello world",MESSAGE_X,MESSAGE_Y);
}
public static final int MESSAGE_X = 75;
public static final int MESSAGE_Y = 100;
}
************************注释******************************
-------------javax.swing.JFrame
1.Container getContentPane()
返回该JFrame内容的窗格对象
2.void add(Component c)
将给定的组件添加到该框架的内容窗格中
--------------java.awt.component
1.repaint()
“尽可能快的”重绘组件
2.public void repaint(int x,int y,int width ,int height)
“尽可能快的”重绘组件的一部分
-------------javax.swing.JComponent
void paintComponent(Graphics g)
覆盖这个方法来描述应该如何绘制自己的组件[color=blue][/color][/color]
JFrame中有四层面板: 根面板、层级面板、玻璃面板和内容窗格,swing程序员对前三者不太关心,它们是用
来组织菜单栏和内容窗格以及实现观感的,swing程序员最关心内容窗格,在设计框架的时候,要使用下面代码
把所有组件放到内容窗格中:
Container contentPane = frame.getContentPane();
Componet c= ..;
contentPane.add(c);
在面板上绘图前提:
1.定义一个扩展于JPanel的类
2.在这个类中,覆盖paintComponent方法
eg:
class MyPanel extends JPanel{
public void paintComponent(Graphics g){
..//code for drawing will go here
}
}
只要窗口需要重绘,事件处理器就会通告组件,从而调用painComponent方法
如果需要强制重绘,就需调用repaint方法,而不是painComponent
显示文本是一种特殊的绘图,Graphics类有个drawString 方法,调用的格式为:
g.drawString(text,x,y)
eg:g.drawString(“not a hello world”,100,150);
为了确保超类完成自己那份工作,必须在绘制自己内容前调用super.paintComponent方法
import javax.swing.*;
import java.awt.*;
public class NotHelloWorld{
public static void main(String[] args){
NotHelloWorldFrame frame = new NotHelloWorldFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_CLOSE);
frame.setVisible(true);
}
}
class NotHelloWorldFrame extends JFrame{
public NotHelloWorldFrame(){
setTitle("nothelloworld");
setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
NotHelloWorlPanel panel = new NotHelloWorldPanel();
add(panel);
}
public static final int DEFALUT_WIDTH = 300;
public static final int DEFALUT_HEIGHT = 200;
}
class NotHelloWorldPanel extends JPane{
public void paintComponet(Grapics g);{
super.paintComponet(g);
g.drawString("not a hello world",MESSAGE_X,MESSAGE_Y);
}
public static final int MESSAGE_X = 75;
public static final int MESSAGE_Y = 100;
}
************************注释******************************
-------------javax.swing.JFrame
1.Container getContentPane()
返回该JFrame内容的窗格对象
2.void add(Component c)
将给定的组件添加到该框架的内容窗格中
--------------java.awt.component
1.repaint()
“尽可能快的”重绘组件
2.public void repaint(int x,int y,int width ,int height)
“尽可能快的”重绘组件的一部分
-------------javax.swing.JComponent
void paintComponent(Graphics g)
覆盖这个方法来描述应该如何绘制自己的组件[color=blue][/color][/color]