panel布局默认是流式布局
JTextArea 多行文本框 三个特性 文本 ,行数,列数(“文本”,5,20)
JScrollPane滚动条 用滚动的组件需要添加进滚动条里
JChrckBox 多选按钮
多选按钮是否被选中 box1.isselected();
单选按钮 JRadioButton 需要按钮组 ButtonGroup 添加组件用group.add(r1);放进按钮组
列表框 JList 初始化 list=new JList泛型
setvisivleRowCount();设置可见几项 最后还是放在滚动条里。配合滚动条使用
获取选项的值 getSelectedIndex();
getSelectedIndices();
getSelectedValues();
下拉框
JComboBox combox;
和列表框设置差不多
换可以自定义添加属性 combobox.addItem(“”ffff");
进度条 JProgressBar
设置进度 setValue(66);
设置显示提示 setStringPainteed(true);
设置精确 setIndeterminate() false精确 true不精确
菜单
JMenuBar menar
JMenu menu1
JMenuItem item1
添加菜单 this.setJMenuBar(menuBar);
添加分隔符menu1.addSeparator();
import java.awt.*;
import javax.swing.*;
public class PanelDemo extends JFrame{
private JTextArea area;
private JScrollPane scrollpane;
private JCheckBox box1,box2;
private JRadioButton r1,r2;
private ButtonGroup group;
private JList list;
private JComboBox <String> combobox;
private JProgressBar progressbar;
private JMenuBar menubar;
private JMenu menu1,menu2;
private JMenuItem item1,item2,item3,item4;
/*
* private JPanel panel; private JButton button;
*/
public void init() {
setLayout(new FlowLayout());
area= new JTextArea("二选一",5,15);
/* scrollpane =new JScrollPane(area); */
box1=new JCheckBox("篮球");
box2=new JCheckBox("hei球");
r1=new JRadioButton("男");
r2=new JRadioButton("女");
group= new ButtonGroup();
list=new JList<String>(new String[] {"A","B","C","D","D","E","F"});
list.setVisibleRowCount(3);
scrollpane =new JScrollPane(list);
/* this.add(list); */
combobox =new JComboBox<String>(new String[] {"A","B","C","D","D","E","F"});
combobox.addItem("123123");
progressbar =new JProgressBar(0,100);
progressbar.setValue(45);
progressbar.setStringPainted(true);
this.add(progressbar);
this.add(combobox);
/* box1.isSelected(); */
group.add(r1);
group.add(r2);
this.add(r1);
this.add(r2);
this.add(box1);
this.add(box2);
this.add(scrollpane);
/*
* button =new JButton("按钮"); panel =new JPanel(); panel.add(button);
* panel.setBackground(Color.LIGHT_GRAY); this.add(panel);
*/
}
public PanelDemo() {
init();
Icon icon=new ImageIcon("quest.png");
menubar=new JMenuBar();
menu1=new JMenu("文件");
menu2=new JMenu("编辑");
item1=new JMenuItem("复制",icon);
item2=new JMenuItem("粘贴");
item3=new JMenuItem("删除");
item4=new JMenuItem("简介");
menu1.add(item1);
menu1.addSeparator();
menu1.add(item2);
menu2.add(item3);
menu2.addSeparator();
menu2.add(item4);
menubar.add(menu1);
menubar.add(menu2);
this.setJMenuBar(menubar);
/* this.setResizable(false); */
this.setTitle("QQ登录");
this.setSize(200,200);
this.setLocation(1366/2-100,768/2-100);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}