1.面板也是一种容器,不是一个单独的窗口,它只是包含在窗口中的一个区域,必须将面板添加到窗体中
2. 文本框
public class PanelDemo extends JFrame{
private JPanel panel;
private JButton button;
private void init(){
button=new JButton(“按钮”);
panel=new JPanel();//流式布局 居中对齐
panel.add(button);
panel.setBackground(Color.blue);
this.add(panel);
this.setResizable(false);
this.setTitle(“QQ登录”);
this.setSize(255,125);
this.setLocation(400,300);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public PanelDemo(){
init();}
}
public class Test{
public static void main(String[] args){
new PanelDemo();}}
3.屏幕中间位置
Dimension dim=getToolkit().getScreenSize();
int w=dim.width/2;
int h=dim.height/2;
this.setSize(255,125);
this.setLocation(w-255/2,h-125/2);
4.滚动条
public class PanelDemo extends JFrame{
private JPanel panel;
private JButton button;
private JTextArea area;
private JScrollPane scrollPane;
private void init(){
//显示文本,行数,列数
area=new JTextArea(“多行文本框”,5,20);
scrollPane=newJScrollPane(area);
this.setLayout(new FlowLayout());
this.add(scrollPane);
this.setTitle(“QQ登录”);
this.setSize(255,125);
this.setLocation(400,300);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public PanelDemo(){
init();}
}
public class Test{
public static void main(String[] args){
new PanelDemo();}}
5.多选框
public class PanelDemo extends JFrame{
private JPanel panel;
private JButton button;
private JTextArea area;
private JScrollPane scrollPane;
private JCheckBox box1,box2;
private void init(){
box1=new JCheckBox(“运动”);
box2=new JCheckBox(“读书”);
//多选按钮是否被选中
box1.isSelected();
//显示文本,行数,列数
area=new JTextArea(“多行文本框”,5,20);
scrollPane=newJScrollPane(area);
this.setLayout(new FlowLayout());
this.add(scrollPane);
this.add(box1);
this.add(box2)
this.setTitle(“QQ登录”);
this.setSize(255,125);
this.setLocation(400,300);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public PanelDemo(){
init();}
}
public class Test{
public static void main(String[] args){
new PanelDemo();}}
6.单选按钮
public class PanelDemo extends JFrame{
private JPanel panel;
private JButton button;
private JTextArea area;
private JScrollPane scrollPane;
private JCheckBox box1,box2;
//单选按钮必须放到一个按钮组
private JRAdioButton r1,r2;
//按钮组
private ButtonGroup group;
private void init(){
box1=new JCheckBox(“运动”);
box2=new JCheckBox(“读书”);
//判断多选按钮是否被选中
// box1.isSelected();
r1=new JReadioButton(“男”);
r2=new JReadioButton(“女”);
//判断单选按钮是否被选中
//r1.isSelected();
group=new ButtonGroup();
//放到按钮组中;
group.add(r1);
group.add(r2);
//显示文本,行数,列数
area=new JTextArea(“多行文本框”,5,20);
scrollPane=newJScrollPane(area);
this.setLayout(new FlowLayout());
this.add(scrollPane);
this.add(box1);
this.add(box2);
this.add(r1);
this.add(r2);
this.setTitle(“QQ登录”);
this.setSize(255,125);
this.setLocation(400,300);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public PanelDemo(){
init();}
}
public class Test{
public static void main(String[] args){
new PanelDemo();}}
7.列表框
public class PanelDemo extends JFrame{
private JPanel panel;
private JButton button;
private JTextArea area;
private JScrollPane scrollPane;
private JCheckBox box1,box2;
//单选按钮必须放到一个按钮组
private JRAdioButton r1,r2;
//按钮组
private ButtonGroup group;
//列表框
private JList list();
private void init(){
//列表框
list=new JList(new String[] {“A”,“B”,“C”,“D”});
//设置可见几项,配合滚动面板
list.setVisibleRowCount(3);
//获取选项
// list.getSelectedIndex();
//list.getSelectedIndices();
//list.getSelectedValues();
box1=new JCheckBox(“运动”);
box2=new JCheckBox(“读书”);
//判断多选按钮是否被选中
// box1.isSelected();
r1=new JReadioButton(“男”);
r2=new JReadioButton(“女”);
//判断单选按钮是否被选中
//r1.isSelected();
group=new ButtonGroup();
//放到按钮组中;
group.add(r1);
group.add(r2);
//显示文本,行数,列数
area=new JTextArea(“多行文本框”,5,20);
scrollPane=newJScrollPane(list);
this.setLayout(new FlowLayout());
this.add(scrollPane);
this.add(box1);
this.add(box2);
this.add(r1);
this.add(r2);
// this.add(list);
this.setTitle(“QQ登录”);
this.setSize(255,125);
this.setLocation(400,300);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public PanelDemo(){
init();}
}
public class Test{
public static void main(String[] args){
new PanelDemo();}}
8.构建模型(用对像时,使用模型更方便)
public JList(final E[] listDate){
this(
new AbstractListModel(){
public int getSize() {return listDate.length;}
public E getElementAt(int i){return listdate[i];没写完}
}
);
}
9.下拉框
public class PanelDemo extends JFrame{
private JPanel panel;
private JButton button;
private JTextArea area;
private JScrollPane scrollPane;
private JCheckBox box1,box2;
//单选按钮必须放到一个按钮组
private JRAdioButton r1,r2;
//按钮组
private ButtonGroup group;
//列表框
private JList list();
//下拉框
private JComboBox combobox;
private void init(){
//下拉框
combobox=new JComboBox<>(new String[]{“A”,“B”,“C”,“D”});
//列表框
list=new JList(new String[] {“A”,“B”,“C”,“D”});
//添加选项
comboBox.addItem(“e”);
//获取选项
// comboBox.getSelectedItem();
//comboBox.getSelectedIndex();
//设置可见几项,配合滚动面板
list.setVisibleRowCount(3);
//获取选项
// list.getSelectedIndex();
//list.getSelectedIndices();
//list.getSelectedValues();
box1=new JCheckBox(“运动”);
box2=new JCheckBox(“读书”);
//判断多选按钮是否被选中
// box1.isSelected();
r1=new JReadioButton(“男”);
r2=new JReadioButton(“女”);
//判断单选按钮是否被选中
//r1.isSelected();
group=new ButtonGroup();
//放到按钮组中;
group.add(r1);
group.add(r2);
//显示文本,行数,列数
area=new JTextArea(“多行文本框”,5,20);
scrollPane=newJScrollPane(list);
this.setLayout(new FlowLayout());
this.add(scrollPane);
this.add(box1);
this.add(box2);
this.add(r1);
this.add(r2);
this.add(comboBox);
// this.add(list);
this.setTitle(“QQ登录”);
this.setSize(255,125);
this.setLocation(400,300);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public PanelDemo(){
init();}
}
public class Test{
public static void main(String[] args){
new PanelDemo();}}
10.滚动条
public class PanelDemo extends JFrame{
private JPanel panel;
private JButton button;
private JTextArea area;
private JScrollPane scrollPane;
private JCheckBox box1,box2;
//单选按钮必须放到一个按钮组
private JRAdioButton r1,r2;
//按钮组
private ButtonGroup group;
//列表框
private JList list();
//下拉框
private JComboBox combobox;
//滚动条
private JProgressBar porgressBar;
private void init(){
//滚动条:无参 进度显示:
// progressBar=new JprogressBar(JProgressBar.HORIZONTAL);
//progressBar=new JprogressBar(JProgressBar.VERTICAL);
progressBar=new JprogressBar(0,100);
//设置进度
//progressBar.setValue(56);
//设置显示提示
progressBar.setStringPainted(true);
//设置是否精确进度条 false精确 true不精确
、、 progressBar.setIndetermainate(false);
for(int i=0;i<=100;++i){
try{
Thread.sleep(100);
} catch(InterruptedException e){
e.printStackTrace();
}
progressBar.setValue(i);
//下拉框
combobox=new JComboBox<>(new String[]{“A”,“B”,“C”,“D”});
//列表框
list=new JList(new String[] {“A”,“B”,“C”,“D”});
//添加选项
comboBox.addItem(“e”);
//获取选项
// comboBox.getSelectedItem();
//comboBox.getSelectedIndex();
//设置可见几项,配合滚动面板
list.setVisibleRowCount(3);
//获取选项
// list.getSelectedIndex();
//list.getSelectedIndices();
//list.getSelectedValues();
box1=new JCheckBox(“运动”);
box2=new JCheckBox(“读书”);
//判断多选按钮是否被选中
// box1.isSelected();
r1=new JReadioButton(“男”);
r2=new JReadioButton(“女”);
//判断单选按钮是否被选中
//r1.isSelected();
group=new ButtonGroup();
//放到按钮组中;
group.add(r1);
group.add(r2);
//显示文本,行数,列数
area=new JTextArea(“多行文本框”,5,20);
scrollPane=newJScrollPane(list);
this.setLayout(new FlowLayout());
this.add(scrollPane);
this.add(box1);
this.add(box2);
this.add(r1);
this.add(r2);
this.add(comboBox);
this.add(progressBar);
// this.add(list);
this.setTitle(“QQ登录”);
this.setSize(255,125);
this.setLocation(400,300);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public PanelDemo(){
init();}
}
public class Test{
public static void main(String[] args){
new PanelDemo();}}
11.菜单
public class PanelDemo extends JFrame{
private JPanel panel;
private JButton button;
private JTextArea area;
private JScrollPane scrollPane;
private JCheckBox box1,box2;
//单选按钮必须放到一个按钮组
private JRAdioButton r1,r2;
//按钮组
private ButtonGroup group;
//列表框
private JList list();
//下拉框
private JComboBox combobox;
//滚动条
private JProgressBar porgressBar;
//菜单
private JMenuBar jMenuBar;
private JMenu menu1,menu2;
private JMenuItem item1,item2,item3,item4;
private void init(){
//菜单
menuBar=new JMenuBar();
menu1=new JMenu(“文件”);
menu2=new JMenu(“编辑”);
item1=new JMenuItem(“新建”);
item2=new JMenuItem(“关闭”);
item3=new JMenuItem(“复制”);
item4=new JMenuItem(“粘贴”);
//添加菜单到菜单项
menu1.add(item1);
//添加分隔符
menu1.addSeparator();
menu1.add(item2);
menu2.add(item3);
menu2.add(item4);
//菜单添加到菜单工具栏中
menuBar.add(menu1);
menuBar.add(menu2);
//菜单栏添加到窗体上
this,setJMenuBar(menubar);
//滚动条:无参 进度显示:
// progressBar=new JprogressBar(JProgressBar.HORIZONTAL);
//progressBar=new JprogressBar(JProgressBar.VERTICAL);
progressBar=new JprogressBar(0,100);
//设置进度
//progressBar.setValue(56);
//设置显示提示
progressBar.setStringPainted(true);
//设置是否精确进度条 false精确 true不精确
、、 progressBar.setIndetermainate(false);
for(int i=0;i<=100;++i){
try{
Thread.sleep(100);
} catch(InterruptedException e){
e.printStackTrace();
}
progressBar.setValue(i);
//下拉框
combobox=new JComboBox<>(new String[]{“A”,“B”,“C”,“D”});
//列表框
list=new JList(new String[] {“A”,“B”,“C”,“D”});
//添加选项
comboBox.addItem(“e”);
//获取选项
// comboBox.getSelectedItem();
//comboBox.getSelectedIndex();
//设置可见几项,配合滚动面板
list.setVisibleRowCount(3);
//获取选项
// list.getSelectedIndex();
//list.getSelectedIndices();
//list.getSelectedValues();
box1=new JCheckBox(“运动”);
box2=new JCheckBox(“读书”);
//判断多选按钮是否被选中
// box1.isSelected();
r1=new JReadioButton(“男”);
r2=new JReadioButton(“女”);
//判断单选按钮是否被选中
//r1.isSelected();
group=new ButtonGroup();
//放到按钮组中;
group.add(r1);
group.add(r2);
//显示文本,行数,列数
area=new JTextArea(“多行文本框”,5,20);
scrollPane=newJScrollPane(list);
this.setLayout(new FlowLayout());
this.add(scrollPane);
this.add(box1);
this.add(box2);
this.add(r1);
this.add(r2);
this.add(comboBox);
this.add(progressBar);
// this.add(list);
this.setTitle(“QQ登录”);
this.setSize(255,125);
this.setLocation(400,300);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public PanelDemo(){
init();}
}
public class Test{
public static void main(String[] args){
new PanelDemo();}}
12.事件处理机制
系统会通知我们去处理这件事件,对这些事件做出相应处理的程序,成为事件处理器。
事件监听器是在一个事件发生时被通知的对象,也称为事件处理器