面板 JPanel
阿新 • • 發佈:2018-08-09
info con 面板 visible oid led 滾動面板 void tco
容器中可以有多個JPanel面板,一個JPanel面板中可以有多個控件。
滾動面板 JScrollPane中只能有一個控件。
public class Demo extends JFrame { public Demo() { setBounds(100, 100, 600, 200); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(new GridLayout(1, 2, 10, 10));//創建2個面板 JPanel p1 = new JPanel(); p1.setLayout(new GridLayout(1, 3)); JPanel p2 = new JPanel(new BorderLayout()); p2.setBackground(Color.BLUE); //設置面板邊框,標題 p1.setBorder(BorderFactory.createTitledBorder("面板1")); p2.setBorder(BorderFactory.createTitledBorder("面板2")); p1.add(new JButton("b1")); p1.add(new JButton("b1")); p1.add(new JButton("b1")); p1.add(new JButton("b1")); p2.add(new JButton("b2"), BorderLayout.EAST); p2.add(new JButton("b2"), BorderLayout.WEST); p2.add(new JButton("b2"), BorderLayout.SOUTH); p2.add(new JButton("b2"), BorderLayout.NORTH); p2.add(new JButton("b2")); c.add(p1); c.add(p2); setVisible(true); } public static void main(String[] args) { new Demo(); } }
public class Demo extends JFrame { public Demo() { setBounds(100, 100, 200, 100); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); Container c = getContentPane(); JTextArea area=new JTextArea();//文本域 JScrollPane sp=new JScrollPane(area);//將文本域添加到滾動面板中 c.add(sp); setVisible(true); } public static void main(String[] args) { new Demo(); } }
面板 JPanel