JPanel
import javax.swing.*;
import java.awt.*;
public class JPanelTest extends JFrame {
public JPanelTest(){
Container container = this.getContentPane();
container.setLayout(new GridLayout(2,1,10,10));//后面参数是间距
JPanel jPanel = new JPanel(new GridLayout(1,3));
JPanel jPane2 = new JPanel(new GridLayout(1,1));
JPanel jPane3 = new JPanel(new GridLayout(2,1));
JPanel jPane4 = new JPanel(new GridLayout(3,1));
jPanel.add(new JButton("1"));jPanel.add(new JButton("1"));jPanel.add(new JButton("1"));
jPane2.add(new JButton("2"));
jPane3.add(new JButton("3"));jPane3.add(new JButton("3"));
jPane4.add(new JButton("4"));jPane4.add(new JButton("4"));jPane4.add(new JButton("4"));
container.add(jPanel);container.add(jPane2);
container.add(jPane3);container.add(jPane4);
this.setVisible(true);
this.setSize(500,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JPanelTest();
}
}
运行结果为:
JScroll
import javax.swing.*;
import java.awt.*;
public class JScrollTest extends JFrame {
public JScrollTest() {
Container container = this.getContentPane();
//文本域
JTextArea jTextArea = new JTextArea(20, 50);
jTextArea.setText("文本域内容");
//jcroll面板
JScrollPane jScrollPane = new JScrollPane(jTextArea);
container.add(jScrollPane);
this.setVisible(true);
setBounds(100,100,300,150);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JScrollTest();
}
}
运行结果为:
当窗口大小小于设定时,出现滑动条,当放大窗口使其大于设定时,滑动条消失