一共两个例子。例子1. 在addWindowListener函数中,参数为WindowListener ,而在addWindowListener(new WinHandler());中,用了WinHandler类作为参数,WinHandler实现了WindowListener接口,就可以作为WindowListener的参数码?/*
* TestWindowListener.java
*
* Created on August 9, 2002, 2:32 PM
*/package ch14;import java.awt.*;
import java.awt.event.*;
/**
*
* @author Stephen Potts
*/
public class TestWindowListener extends Frame
{
Scrollbar sb;
TextField tField;
/** Creates a new instance of TestWindowListener */
public TestWindowListener()
{
sb=new Scrollbar(Scrollbar.VERTICAL, 0, 1, 0, 255);
add(sb);
tField = new TextField(30);
add(tField);
this.setLayout(new FlowLayout());
addWindowListener(new WinHandler());
setTitle("Using a Scrollbar Object");
setBounds( 100, 100, 400, 400);
setVisible(true);
}
public static void main(String[] args)
{
TestWindowListener twl= new TestWindowListener();
}
}class WinHandler implements WindowListener
{
public void windowClosing(WindowEvent e)
{
System.out.println("windowClosing() called");
System.exit(0);
}
public void windowActivated(java.awt.event.WindowEvent windowEvent)
{
System.out.println("windowActivated() called");
}
public void windowClosed(java.awt.event.WindowEvent windowEvent)
{
System.out.println("windowClosed() called");
}
public void windowDeactivated(java.awt.event.WindowEvent windowEvent)
{
System.out.println("windowDeactivated() called");
}
public void windowDeiconified(java.awt.event.WindowEvent windowEvent)
{
System.out.println("windowDeiconified() called");
}
public void windowIconified(java.awt.event.WindowEvent windowEvent)
{
System.out.println("windowIconified called");
}
public void windowOpened(java.awt.event.WindowEvent windowEvent)
{
System.out.println("windowOpened() called");
}
}------------------------------------------------------------------------------------------
例子2.在本例中,scrollRectToVisible个函数起什么作用?javadoc里这么说得:scrollRectToVisible
public void scrollRectToVisible(Rectangle aRect)Forwards the scrollRectToVisible() message to the JComponent's parent. Components that can service the request, such as JViewport, override this method and perform the scrolling. Parameters:
aRect - the visible Rectangle
See Also:
JViewport
--------------------------------------------------------------------------------------按javadoc里说明的,Rectangle(150,150,300,300)构造函数的四个参数分别为:
public Rectangle(int x,
int y,
int width,
int height)那按本例:坐标为150.150 宽高为300,是不是滚动条(矩形)为300的正方形?实际运行时不是这样的。/*
* TestJScrollPane.java
*
* Created on July 30, 2002, 11:35 AM
*/package ch16;import java.awt.*;
import javax.swing.*;
import java.awt.event.*;/**
*
* @author Stephen Potts
* @version
*/
public class TestJScrollPane extends JFrame
{
JScrollPane sp;
JTextField tf1;
JTextField tf2;
JTextField tf3;
JTextField tf4;
JTextField tf5;
JTextField tf6;
JTextField tf7;
JTextField tf8;
/** Creates new TestJScrollPane */
public TestJScrollPane()
{
//create eight text fields
tf1 = new JTextField("Text Field Number 1 ");
tf2 = new JTextField("Text Field Number 2 ");
tf3 = new JTextField("Text Field Number 3 ");
tf4 = new JTextField("Text Field Number 4 ");
tf5 = new JTextField("Text Field Number 5 ");
tf6 = new JTextField("Text Field Number 6 ");
tf7 = new JTextField("Text Field Number 7 ");
tf8 = new JTextField("Text Field Number 8 ");
//add the panel
JPanel p1 = new JPanel();
p1.add(tf1);
p1.add(tf2);
p1.add(tf3);
p1.add(tf4);
p1.add(tf5);
p1.add(tf6);
p1.add(tf7);
p1.add(tf8); //create the scroll pane
sp = new JScrollPane(p1);
sp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); getContentPane().add(sp);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Using a JScrollPane");
setBounds( 100, 100, 300, 300);
setVisible(true);
sp.scrollRectToVisible(new Rectangle(150,150,300,300));
}
public static void main(String[] args)
{
TestJScrollPane tsp = new TestJScrollPane();
}
}
* TestWindowListener.java
*
* Created on August 9, 2002, 2:32 PM
*/package ch14;import java.awt.*;
import java.awt.event.*;
/**
*
* @author Stephen Potts
*/
public class TestWindowListener extends Frame
{
Scrollbar sb;
TextField tField;
/** Creates a new instance of TestWindowListener */
public TestWindowListener()
{
sb=new Scrollbar(Scrollbar.VERTICAL, 0, 1, 0, 255);
add(sb);
tField = new TextField(30);
add(tField);
this.setLayout(new FlowLayout());
addWindowListener(new WinHandler());
setTitle("Using a Scrollbar Object");
setBounds( 100, 100, 400, 400);
setVisible(true);
}
public static void main(String[] args)
{
TestWindowListener twl= new TestWindowListener();
}
}class WinHandler implements WindowListener
{
public void windowClosing(WindowEvent e)
{
System.out.println("windowClosing() called");
System.exit(0);
}
public void windowActivated(java.awt.event.WindowEvent windowEvent)
{
System.out.println("windowActivated() called");
}
public void windowClosed(java.awt.event.WindowEvent windowEvent)
{
System.out.println("windowClosed() called");
}
public void windowDeactivated(java.awt.event.WindowEvent windowEvent)
{
System.out.println("windowDeactivated() called");
}
public void windowDeiconified(java.awt.event.WindowEvent windowEvent)
{
System.out.println("windowDeiconified() called");
}
public void windowIconified(java.awt.event.WindowEvent windowEvent)
{
System.out.println("windowIconified called");
}
public void windowOpened(java.awt.event.WindowEvent windowEvent)
{
System.out.println("windowOpened() called");
}
}------------------------------------------------------------------------------------------
例子2.在本例中,scrollRectToVisible个函数起什么作用?javadoc里这么说得:scrollRectToVisible
public void scrollRectToVisible(Rectangle aRect)Forwards the scrollRectToVisible() message to the JComponent's parent. Components that can service the request, such as JViewport, override this method and perform the scrolling. Parameters:
aRect - the visible Rectangle
See Also:
JViewport
--------------------------------------------------------------------------------------按javadoc里说明的,Rectangle(150,150,300,300)构造函数的四个参数分别为:
public Rectangle(int x,
int y,
int width,
int height)那按本例:坐标为150.150 宽高为300,是不是滚动条(矩形)为300的正方形?实际运行时不是这样的。/*
* TestJScrollPane.java
*
* Created on July 30, 2002, 11:35 AM
*/package ch16;import java.awt.*;
import javax.swing.*;
import java.awt.event.*;/**
*
* @author Stephen Potts
* @version
*/
public class TestJScrollPane extends JFrame
{
JScrollPane sp;
JTextField tf1;
JTextField tf2;
JTextField tf3;
JTextField tf4;
JTextField tf5;
JTextField tf6;
JTextField tf7;
JTextField tf8;
/** Creates new TestJScrollPane */
public TestJScrollPane()
{
//create eight text fields
tf1 = new JTextField("Text Field Number 1 ");
tf2 = new JTextField("Text Field Number 2 ");
tf3 = new JTextField("Text Field Number 3 ");
tf4 = new JTextField("Text Field Number 4 ");
tf5 = new JTextField("Text Field Number 5 ");
tf6 = new JTextField("Text Field Number 6 ");
tf7 = new JTextField("Text Field Number 7 ");
tf8 = new JTextField("Text Field Number 8 ");
//add the panel
JPanel p1 = new JPanel();
p1.add(tf1);
p1.add(tf2);
p1.add(tf3);
p1.add(tf4);
p1.add(tf5);
p1.add(tf6);
p1.add(tf7);
p1.add(tf8); //create the scroll pane
sp = new JScrollPane(p1);
sp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); getContentPane().add(sp);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Using a JScrollPane");
setBounds( 100, 100, 300, 300);
setVisible(true);
sp.scrollRectToVisible(new Rectangle(150,150,300,300));
}
public static void main(String[] args)
{
TestJScrollPane tsp = new TestJScrollPane();
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货