JPanel
public class JPanelDemo extends JFrame { public JPanelDemo(){ Container container = this.getContentPane(); container.setLayout(new GridLayout(2,1,10,10));//设置表格参数后面两个参数是上下两个的间距 JPanel panel1 = new JPanel(new GridLayout(1,3)); JPanel panel2 = new JPanel(new GridLayout(1,2)); JPanel panel3 = new JPanel(new GridLayout(2,1)); JPanel panel4 = new JPanel(new GridLayout(3,2)); panel1.add(new JButton("1")); panel1.add(new JButton("1")); panel1.add(new JButton("1")); panel2.add(new JButton("2")); panel2.add(new JButton("2")); panel3.add(new JButton("3")); panel3.add(new JButton("3")); panel4.add(new JButton("4")); panel4.add(new JButton("4")); panel4.add(new JButton("4")); panel4.add(new JButton("4")); panel4.add(new JButton("4")); panel4.add(new JButton("4")); container.add(panel1); container.add(panel2); container.add(panel3); container.add(panel4); setVisible(true); setSize(500,500); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//default 默认 } public static void main(String[] args) { new JPanelDemo(); } }
JScrollPanel
public class JScrollDemo extends JFrame { public JScrollDemo(){ Container container = this.getContentPane(); //文本域 JTextArea textArea = new JTextArea(20, 50); textArea.setText("欢迎来学习狂神学java"); //Scroll面板 JScrollPane scrollPane = new JScrollPane(textArea); container.add(scrollPane); this.setVisible(true); this.setBounds(100,100,300,150); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new JScrollDemo(); } }