12.14.2JScrollPane

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 );

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值