前言
上一篇我们共同学习了Jlable图标,本篇我们继续来学习一下JPanel和JScrollPane的实现。
一、JPanel面板和JScrollPane文本域面板
关于JPanel我们经过之前的学习相信大家都有很好的了解了,这里将不再赘述了,简单的看一下代码即可,今天我们将重点学习一下文本域面板JScrollPane的操作。
二、代码示例
1.JPanel面板
代码如下(示例):
public class JPanelDemo extends JFrame {
public JPanelDemo(){
Container container = this.getContentPane();
container.setLayout(new GridLayout(2,2,10,10));
JPanel jPanel1 = new JPanel(new GridLayout(1,3));
JPanel jPanel2 = new JPanel(new GridLayout(1,2));
JPanel jPanel3 = new JPanel(new GridLayout(2,1));
JPanel jPanel4 = new JPanel(new GridLayout(3,2));
jPanel1.add(new JButton("1"));
jPanel1.add(new JButton("1"));
jPanel1.add(new JButton("1"));
jPanel2.add(new JButton("2"));
jPanel2.add(new JButton("2"));
jPanel3.add(new JButton("3"));
jPanel3.add(new JButton("3"));
jPanel4.add(new JButton("4"));
jPanel4.add(new JButton("4"));
jPanel4.add(new JButton("4"));
jPanel4.add(new JButton("4"));
jPanel4.add(new JButton("4"));
jPanel4.add(new JButton("4"));
container.add(jPanel1);
container.add(jPanel2);
container.add(jPanel3);
container.add(jPanel4);
this.setVisible(true);
this.setSize(500,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JPanelDemo();
}
}
运行结果:
2.JScrollPane文本域面板
代码如下(示例):
JScrollDemo类
public class JScrollDemo extends JFrame {
public JScrollDemo(){
Container container = this.getContentPane();
//文本域
JTextArea jTextArea = new JTextArea(20, 50);
jTextArea.setText("这是一个文本域");
//JScroll面板
JScrollPane jScrollPane = new JScrollPane(jTextArea);
container.add(jScrollPane);
this.setVisible(true);
this.setBounds(100,100,300,350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JScrollDemo();
}
}
运行结果:
总结
本篇我门重点学习了文本域面板的操作,文本域需要放在文本域面板上才能生效。