📄 processtable4.java
字号:
lab1[3].setText(String.valueOf(unalocate));
lab1[3].setBackground(Color.LIGHT_GRAY);
lab1[3].setOpaque(true);
}
}
else if(e.getActionCommand().equals("运行")) {
int k=((Integer)defaultModel1.getValueAt(0,1)).intValue();
int j=((Integer)defaultModel1.getValueAt(0,2)).intValue();
k--;
j--;
if(k==0){
for(int i = 0;i<lab1.length;i++){
if(lab[i].getText()==defaultModel1.getValueAt(0,0)){
lab1[i].setBackground(Color.LIGHT_GRAY);
if(i==0){
defaultModel3.addRow(new Object[]{new Integer(20),
defaultModel1.getValueAt(0,3),new Integer(1)});
}
else {
int l=0;
for(int n = 0;n<i;n++){
l +=Integer.parseInt(lab1[n].getText());
}
defaultModel3.addRow(new Object[]{new Integer(l+20),
defaultModel1.getValueAt(0,3),new Integer(1)});
}
}
}
defaultModel1.removeRow(0);
defaultModel1.addRow(new Object[]{(String)defaultModel2.getValueAt(0,0),
defaultModel2.getValueAt(0,1),defaultModel2.getValueAt(0,2),defaultModel2.getValueAt(0,3)});
for(int i = 0;i<lab1.length;i++)
{
if(Integer.parseInt(lab1[i].getText().toString()) >= Integer.parseInt(
defaultModel2.getValueAt(0,3).toString()) && lab1[i].getBackground() == Color.LIGHT_GRAY)
{
int temp = Integer.parseInt(lab1[i].getText());
int temp1=0;
int alocate1 = 0;
String str = lab1[i].getText();
lab1[i].setBackground(Color.yellow);
lab1[i].setText(defaultModel2.getValueAt(0,3).toString());
lab[i].setText(defaultModel2.getValueAt(0,0).toString());
temp1 = temp - Integer.parseInt(defaultModel2.getValueAt(0,3).toString());
for(int j1 = 0;j1 <= i;j1++){
alocate1 += Integer.parseInt(lab1[j1].getText());
}
if(i==0){
defaultModel3.addRow(new Object[]{new Integer(20+(Integer.parseInt(lab1[i].getText()))),new Integer(temp1),new Integer(1)
});
}
else {
//int temp2 = Integer.parseInt(lab1[i -1].getText());
defaultModel3.addRow(new Object[]{new Integer(alocate1+20),new Integer(temp1),new Integer(1)
});
}
defaultModel2.removeRow(0);
for(int k1 = 0;k1<defaultModel3.getRowCount();k1++){
if(Integer.parseInt(defaultModel3.getValueAt(k1,1).toString()) == Integer.parseInt(str))
defaultModel3.setValueAt(new Integer(0),k1,2);
}
}
/* else
{
new ErrorFrame();
}*/
}
}
else //asociate with "if(k==0)"
{
defaultModel1.setValueAt(new Integer(k),0,1);
defaultModel1.setValueAt(new Integer(j),0,2);
sortColumn(defaultModel1, 2, false);
//sortColumn(defaultModel1, 3, false);
}
}
else if(e.getActionCommand().equals("退出")){
System.exit(0);
}
}
static void sortColumn(DefaultTableModel defaultModel1, int colIndex, boolean ascending) {
Vector data =defaultModel1.getDataVector();
Object[] colData = new Object[defaultModel1.getRowCount()];
//Object[] colData1 = new Object[defaultModel1.getRowCount()];
// Copy the column data in an array
for (int i=0; i<colData.length; i++) {
colData[i] = ((Vector)data.get(i)).get(colIndex);
//colData[i].index = i;
//colData1[i] = ((Vector)data.get(i)).get(colIndex+1);
}
// Sort the array of column data
Arrays.sort(colData, new ColumnSorter(ascending));
// Copy the sorted values back into the table model
for (int i=0; i<colData.length; i++) {
((Vector)data.get(i)).set(colIndex, colData[i]);
// ((Vector)data.get(i)).set(colIndex+1, colData1[i]);
}
defaultModel1.fireTableStructureChanged();
}
public static void main(String[] args){
new ProcessTable4();
}
}
class ColumnSorter implements Comparator {
boolean ascending;
ColumnSorter(boolean ascending) {
this.ascending = ascending;
}
public int compare(Object a, Object b) {
// Treat empty strains like nulls
if (a instanceof String && ((String)a).length() == 0) {
a = null;
}
if (b instanceof String && ((String)b).length() == 0) {
b = null;
}
// Sort nulls so they appear last, regardless
// of sort order
if (a == null && b == null) {
return 0;
} else if (a == null) {
return 1;
} else if (b == null) {
return -1;
} else if (a instanceof Comparable) {
if (ascending) {
return ((Comparable)a).compareTo(b);
} else {
return ((Comparable)b).compareTo(a);
}
} else {
if (ascending) {
return a.toString().compareTo(b.toString());
} else {
return b.toString().compareTo(a.toString());
}
}
}
}
class AddFrame extends JFrame
{
public static JTextField PID,time,prioty,memory;
private JLabel PID1,time1,prioty1,memory1;
public AddFrame(){
super("Input Frame");
Container container=getContentPane();
container.setLayout(new GridLayout(6,2));
PID =new JTextField(20);
time=new JTextField(20);
prioty=new JTextField(20);
memory = new JTextField(20);
PID1 =new JLabel("PID");
time1=new JLabel("process time");
prioty1= new JLabel("process prioty");
memory1 = new JLabel("process memory");
//jb.addActionListener(new ProcessTable4());
container.add(PID1);
container.add(PID);
container.add(time1);
container.add(time);
container.add(prioty1);
container.add(prioty);
container.add(memory1);
container.add(memory);
setSize(400,200);
//show();
}
}
class ErrorFrame extends JFrame{
public ErrorFrame(){
super("Error Frame");
Container pane = getContentPane();
JLabel jlb = new JLabel("The Process is too larger!!!");
jlb.setBackground(Color.PINK);
jlb.setOpaque(true);
jlb.setBorder(BorderFactory.createLineBorder(Color.cyan,10));
pane.add(jlb);
setSize(200,200);
show();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -