目录
1 实验名称
Java Swing常用组件
2 实验目的
掌握在IDEA中调试代码
掌握Java Swing中常用组件类的使用
掌握组件和容器的概念
3 实验源代码
Application.java:
public class Application {
public static void main(String[] args) {
MyJFrame myJFrame = new MyJFrame();//创建自定义窗口对象
myJFrame.init();//初始化窗口
}
}
MyJFrame.java:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MyJFrame extends JFrame {
private JMenuBar jMenuBar = null;// 菜单条 对象
private JMenu jMenu = null;//菜单
private JMenu jMenuFile = null;
private JMenu subMenu = null;//子菜单
private JMenuItem jMenuItem1 = null;//菜单项
private JMenuItem jMenuItem2 = null;
private JMenuItem jMenuItem3 = null;
private JMenuItem jMenuItem4 = null;
private JLabel jLabel1 = null;//定义标签
private JTextField jTextField1 = null;//定义文本框 组件
private JButton jButton1 = null;
private JTextArea jTextArea = null;//定义文本区
private MyListener myListener = null;//定义监听器对象
//初始化窗口的方法
void init(){
this.setLayout(new FlowLayout());//FlowLayout对象/设置窗口的布局管理器为FlowLayout
jMenuBar = new JMenuBar();//创建菜单条
this.setJMenuBar(jMenuBar);//让窗口对象调用setJMenuBar方法/向窗口对象中添加菜单条对象
jMenu = new JMenu("菜单1");//创建菜单对象
jMenuBar.add(jMenu);//把菜单添加到菜单条中
jMenuFile = new JMenu("File");
jMenuBar.add(jMenuFile);
jMenuItem1 = new JMenuItem("Java top",new ImageIcon("src/a.gif"));//创建对象
jMenuItem1.setAccelerator(KeyStroke.getKeyStroke('A'));//添加快捷键A ''单个字符
jMenuItem2 = new JMenuItem("动画话题",new ImageIcon("src/b.gif"));
jMenuItem2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK));//crt+s
jMenu.add(jMenuItem1);//把菜单项1放到菜单中
jMenu.add(jMenuItem2);//把菜单项2放到菜单中
subMenu = new JMenu("软件项目");
jMenu.add(subMenu);
jMenuItem3 = new JMenuItem("汽车信息系统",new ImageIcon("src/c.gif"));
jMenuItem4 = new JMenuItem("农场信息系统",new ImageIcon("src/d.gif"));
subMenu.add(jMenuItem3);
subMenu.add(jMenuItem4);
JPanel jPanel = new JPanel();//创建面板对象
this.add(jPanel);
jLabel1 = new JLabel("文本框:");
//this.add(jLabel1,BorderLayout.NORTH);
jPanel.add(jLabel1);
jTextField1 = new JTextField(10);//一个字符占一列,显示字符的个数
//this.add(jTextField1,BorderLayout.SOUTH);
jPanel.add(jTextField1);
jPanel.add(new JLabel("按钮"));
jButton1 = new JButton("确定");
jPanel.add(jButton1);
validate();//
myListener = new MyListener();//将监听器对象绑定在按钮
jPanel.add(new JLabel("选择框"));
JCheckBox jCheckBox1 = new JCheckBox("喜欢学Java");
jPanel.add(jCheckBox1);
JCheckBox jCheckBox2 = new JCheckBox("喜欢体育运动");
jPanel.add(jCheckBox2);
JCheckBox jCheckBox3 = new JCheckBox("喜欢互相交流");
jPanel .add(jCheckBox3);
this.add(new JLabel("单选按钮"));
JRadioButton jRadioButtonMale = new JRadioButton("男");//起名 见名知意
JRadioButton jRadioButtonFemale = new JRadioButton("女");
JRadioButton jRadioButtonAI = new JRadioButton("机器人");
ButtonGroup buttonGroup = new ButtonGroup();//创建互斥按钮组对象
buttonGroup.add(jRadioButtonMale);//将单选按钮添加到互斥按钮组中
buttonGroup.add(jRadioButtonFemale);
buttonGroup.add(jRadioButtonAI);
this.add(jRadioButtonMale);//将单选窗口放入窗口中
this.add(jRadioButtonFemale);
this.add(jRadioButtonAI);
this.add(new JLabel("下拉选项"));
JComboBox jComboBox = new JComboBox();
this.add(jComboBox);
jComboBox.addItem("跑步");
jComboBox.addItem("跳绳");
jComboBox.addItem("骑行");
this.add(new JLabel("文本区:"));
JTextArea jTextArea = new JTextArea(5,5);//20.30
// this.add(jTextArea);
JScrollPane jScrollPane = new JScrollPane(jTextArea);//向窗口中添加一个带文本区的滚动窗格
this.add(jScrollPane);
this.add(new JLabel("请输入密码:"));
JPasswordField jPasswordField = new JPasswordField(10);
this.add(jPasswordField);
JSplitPane jSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,new JLabel("标签1"),new JLabel("标签2"));
this.add(jSplitPane);
//设置窗口的常用属性
this.setTitle("我的第一个Java窗口");//设置窗口的标题
this.setBounds(400,200,600,400);//设置窗口的左顶点初始位置以及窗口的
this.setVisible(true);//设置窗口可见
this.setDefaultCloseOperation(EXIT_ON_CLOSE);//点击窗口关闭按钮就退出程序
Container container = this.getContentPane();
container.setBackground(Color.cyan);
}
//定义监听器类
private class MyListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
//做发生ActionEvent事件的处理器
String str = jTextField1.getText();//将文本框中的信息提取出来存放到str中
jTextArea.setText(str);//将在文本区中使用
jTextArea.setText("");//文本框信息清空/or "",null
}
}
}
4 实验运行结果图
5 总结
Java swing的常用组件有按钮、多选框、单选框、下拉列表、列表框、滑动条、标签等。JButton(按钮),JButton是继承AbstractButton类而来,而AbstractButton本身是一个抽象类,里面定义了许多组件设置的方法与组件事件驱动方法(Event handle),如addActionListener()、setText等,所提供的方法不下50种,可说是非常重要的一个类。JList(列表框),JList与JCheckBox有点相似,都可以让你选择一到多个选项,较不同的是,JList的选项方式是整列选取。默认情况下列表框是无法滚动的,如果我们要有滚动的效果,必须将JList放入滚动面版中(JScrollPane)。JLabel(标签),我们最常在JLabel上放置文字或图形,也因此我们常常需要调整JLabel上文字或图形。