p { margin-bottom: 0.21cm; }
-
JscrollPane 是由水平和垂直方向上的JscrollBar ,以及一个JViewport 组成
-
调用JscrollPane.getViewport 方法 可以获得代表滚动窗口中的视图区的JViewport 对象
-
调用JViewport.setView 方法,可以将滚动窗口中要显示的内容作为子组件增加到JViewport 上
例子
public class TestSwing extends JFrame {
public TestSwing() {
this .setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE );
JScrollPane sp = new JScrollPane();
JTextArea ta = new JTextArea(50,50);
sp. getViewport ().add(ta);
this .getContentPane().add(sp);
}
Swing 的标准对话框
JOptionPane 提供了 showXxxDialog 静态方法产生对话框
JFileChooser 类专门实现文件存取对话框
JOptionPane. showConfirmDialog 方法提供了用户交流对话框
例子
public class TestSwing extends JFrame {
public TestSwing() {
this .setDefaultCloseOperation(WindowConstants. DO_NOTHING_ON_CLOSE );
JOptionPane. showMessageDialog ( null , " 程序开始运行 " );
JScrollPane sp = new JScrollPane();
JTextArea ta = new JTextArea(50,50);
sp.getViewport().add(ta);
this .getContentPane().add(sp);
addWindowListener( new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
super .windowClosing(e);
// 第一个参数 父窗口 如果选择确定就结束程序
if (JOptionPane. OK_CANCEL_OPTION != JOptionPane. showConfirmDialog (TestSwing. this , " 真的要退出程序吗 ?" , " 结束程序 " ,JOptionPane. OK_CANCEL_OPTION )){
dispose();
System. exit (0);
}
}
});
}
public static void main(String[] args) {
TestSwing testSwing = new TestSwing();
testSwing.setSize(400,400);
testSwing.setVisible( true );
}
}