2020.5.11第二节课
import java.awt.;
import javax.swing.;
public class Pane extends JFrame{
private JPanel pane;//容器
private JButton button;
private JTextArea area;
//滚动面板
private JScrollPane scrollPne;
//复选框
private JCheckBox box1,box2;
//单选按钮必须放在一个按钮组里
private JRadioButton r1,r2;
//按钮组
private ButtonGroup group;
//列表框
private JList list;
//下拉框
private JComboBox comBox;
//滚动条
private JProgressBar progressBar;
//菜单
private JMenuBar menuBar;
private JMenu menu1,menu2;
private JMenuItem item1,item2,item3,item4;
private void init() {
Icon icon=new ImageIcon(“Capture001.png”);
//菜单
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);
menu1.addSeparator();
menu2.add(item3);
menu2.add(item4);
//菜单添加到菜单工具栏
menuBar.add(menu1);
menuBar.add(menu2);
//菜单栏添加到窗体
this.setJMenuBar(menuBar);
//滚动条:无参,进度显示
progressBar=new JProgressBar(0,100);
//设置进度
progressBar.setValue(66);
//设置显示提示
progressBar.setStringPainted(true);
//设置是否精确进度条 false 精确 true 不精确
progressBar.setIndeterminate(false);
for(int i=0;i<=100;i++) {
progressBar.setValue(i);
}
//下拉框
comBox=new JComboBox(new String[] {“A”,“B”,“C”,“D”});
//再次添加文本
comBox.addItem(“fff”);
//获取选项
// comBox.getSelectedItem();
// comBox.getSelectedIndex();
//列表框
list=new JList(new String[] {“A”,“B”,“C”,“D”});
//设置可见几项:配合滚动面板
//list.setVisibleRowCount(3);
//JScrollPane scrollPane = new JScrollPane(list);
// //this.add(list);
//获取选项值
//list.getSelectedIndex();
//list.getSelectedIndices();
//list.getSelectedValues();
box1=new JCheckBox("运动");
box2=new JCheckBox("读书");
//判断多选按钮是否被选中
// box1.isSelected();
r1=new JRadioButton("女");
r2=new JRadioButton("男");
group=new ButtonGroup();
//把单选按钮放在按钮组中
//单选按钮也有判断是否被选中
group.add(r1);
group.add(r2);
//显示文本 行数 列数
area=new JTextArea("多行文本",5,4);
JScrollPane scrollPane = new JScrollPane(area);
this.setLayout(new FlowLayout());
this.add(scrollPane);
this.add(box1);
this.add(box2);
this.add(r1);
this.add(r2);
this.add(list);
this.add(comBox);
this.add(progressBar);
button=new JButton("按钮");
pane=new JPanel();//默认是流式布局,居中显示
pane.add(button);
pane.setBackground(Color.blue);
this.add(pane);
this.setTitle("这是一个窗口");
this.setSize(300, 300);
this.setLocation(400, 300);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private void add(Icon icon) {
// TODO 自动生成的方法存根
}
public Pane() {
init();
}
}
测试类
public class text {
public static void main(String args[]) {
new Pane();
}
}