12.14.2JScrollPane

来源:互联网 发布:vb.net 连接sql数据库 编辑:程序博客网 时间:2024/05/29 05:56

p { margin-bottom: 0.21cm; }

  • JscrollPane是由水平和垂直方向上的JscrollBar,以及一个JViewport组成

  • 调用JscrollPane.getViewport方法可以获得代表滚动窗口中的视图区的JViewport对象

  • 调用JViewport.setView方法,可以将滚动窗口中要显示的内容作为子组件增加到JViewport

例子

 

publicclassTestSwing extendsJFrame {

publicTestSwing() {

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JScrollPanesp = newJScrollPane();

JTextAreata = newJTextArea(50,50);

sp.getViewport().add(ta);

this.getContentPane().add(sp);

}

Swing的标准对话框

JOptionPane提供了showXxxDialog静态方法产生对话框

JFileChooser类专门实现文件存取对话框

JOptionPane.showConfirmDialog方法提供了用户交流对话框

例子

publicclassTestSwing extendsJFrame {

publicTestSwing() {

this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);

JOptionPane.showMessageDialog(null,"程序开始运行");

JScrollPanesp = newJScrollPane();

JTextAreata = newJTextArea(50,50);

sp.getViewport().add(ta);

this.getContentPane().add(sp);

addWindowListener(newWindowAdapter(){

@Override

publicvoidwindowClosing(WindowEvent e) {

//TODOAuto-generated method stub

super.windowClosing(e);

//第一个参数 父窗口 如果选择确定就结束程序

if(JOptionPane.OK_CANCEL_OPTION!= JOptionPane.showConfirmDialog(TestSwing.this,"真的要退出程序吗?","结束程序",JOptionPane.OK_CANCEL_OPTION)){

dispose();

System.exit(0);

}

}

});

}

publicstaticvoidmain(String[] args) {

TestSwingtestSwing = newTestSwing();

testSwing.setSize(400,400);

testSwing.setVisible(true);

}

}