总体包含关系(JFrame(JPanel(各种小组件)))
jPanel1.add(jl);
jf.add(jPanel1);
类具有单一职责,一般JFrame一个类,JPanel一个类
public class MyUi extends JFrame{//为JFrame设置一个单独的类,负责相关组件组装 }
public class MyJpanel extends JPanel{}//为JPanel设置单独类,只负责JPanel相关设置
一、窗口
JFrame jf=new JFrame("进程调度");
设置窗口大小
jf.setSize(width, height);//设置窗口大小
this.setPreferredSize(new Dimension(width, height));//用这个方法窗口边框会显示不出来
设置窗口居中
Toolkit kit=Toolkit.getDefaultToolkit();//设置窗口居中 Dimension screensize=kit.getScreenSize(); jf.setLocation((screensize.width-width)/2, (screensize.height-height)/2);//width ,height为窗口的宽和高
设置窗口关闭
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭 jf.setVisible(true);//设置窗口是否可见
二、布局
1.流式布局
FlowLayout f=new FlowLayout();//流式布局 jf.setLayout(f);//往窗口内添加布局
三、面板组件(JPanel)
JFrame窗口中添加JPanel控制面板组件
JPanel jPanel=new JPanel();
设置JPanel的边框大小
myJpanel.setPreferredSize(new Dimension(200, 300));//这个能显示 myJpanel.setSize(new Dimension(200, 300));//这个边框会显示不出来
四、各种小组件
1.标签组件(JLable)
JLabel jl3=new JLabel("运行结果");
2.按钮(JButton)
JButton jButton=new JButton("开始调度实行");
一般情况下为按钮注册监听器
jButton.addActionListener(new ActionListener() { });//内部监听器
3.文本框(JTextFile)
JTextField jTextField=new JTextField();
为文本框设计大小
jTextField.setPreferredSize(new Dimension(300, 200));
4.文本区(JTextArea)
JTextArea jTextArea=new JTextArea();
为文本区设计大小
jTextArea.setPreferredSize(new Dimension(300, 650));
为文本区或文本框添加下拉框
JScrollPane jScrollPane=new JScrollPane(jTextArea);//为文本区添加边框
当为文本区(JTextArea)或文本框(JTextFile)设计边框大小时,若加了滚动框,直接设计滚动框的大小即可(若加滚动框后,去设计文本框或文本区大小时,若其内容超过大小,不显示滚动条框)
jScrollPane.setPreferredSize(new Dimension(300, 400));
5.单选按钮(JRadioButton)
JRadioButton jr1=new JRadioButton("FCFS"); JRadioButton jr2=new JRadioButton("SJF"); JRadioButton jr3=new JRadioButton("PSA"); JRadioButton jr4=new JRadioButton("RR时间片轮转");
判断单选框选项是否被选中(可用在监听器中判断是否被选中)
jr1.isSelected()
6.多选框(JCheckBox)
JCheckBox jCheckBox1=new JCheckBox("苹果"); JCheckBox jCheckBox2=new JCheckBox("香蕉"); JCheckBox jCheckBox3=new JCheckBox("梨子", true);//设置是否被选中
判断是否被选中
jCheckBox1.isSelected()
可以为其添加状态监听器,用于监听其状态的改变
jCheckBox1.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { JCheckBox checkBox=(JCheckBox) e.getSource();//返回鼠标点击对象 System.out.println(checkBox.getText()+"状态改变!"); } });