AWT--中间容器--JPanel-JScrollPane

package JPanel01;


import java.awt.*;

import javax.swing.*;


执行后结果:



代码

public class JPanel01 extends JFrame {
/**

*/
private static final long serialVersionUID = 1L;
public JPanel01() {

this.setTitle("PanelDemo");
//创建一个滚动面板
JScrollPane scrollPane = new JScrollPane();
//设置水平滚动条策略--需要时显示
scrollPane.setHorizontalScrollBarPolicy
(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
//设置垂直滚动条--一直显示
scrollPane.setVerticalScrollBarPolicy
(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); //参数比较过,容易出问题

JPanel panel = new JPanel();
panel.add(new JButton("按钮1"));
panel.add(new JButton("按钮2"));
panel.add(new JButton("按钮3"));
panel.add(new JButton("按钮4"));

//设置JPanel在ScrollPanem滚动面板中显示
scrollPane.setViewportView(panel);
//将滚动面板添加到面底板的中部
this.add(scrollPane,BorderLayout.CENTER);

//将按钮添加到面板的南区
this.add(panel, BorderLayout.SOUTH);

//点击关闭按钮,面板关闭
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(400, 500);
this.setVisible(true);
this.setLocationRelativeTo(null);

}
public static void main(String [] args) {
new JPanel01();
}

}

猜你喜欢

转载自blog.csdn.net/weixin_41585557/article/details/80671486
AWT