simparaminttag.java
来自「一个小型网络仿真器的实现」· Java 代码 · 共 106 行
JAVA
106 行
/*
JaNetSim --- Java Network Simulator
-------------------------------------
This software was developed at the Network Research Lab, Faculty of
Computer Science and Information Technology (FCSIT), University of Malaya.
This software may be used and distributed freely. FCSIT assumes no responsibility
whatsoever for its use by other parties, and makes no guarantees, expressed or
implied, about its quality, reliability, or any other characteristic.
We would appreciate acknowledgement if the software is used.
FCSIT ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" CONDITION AND
DISCLAIM ANY LIABILITY OF ANY KIND FOR ANY DAMAGES WHATSOEVER RESULTING
FROM THE USE OF THIS SOFTWARE.
*/
package janetsim.component;
import janetsim.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SimParamIntTag extends SimParameter implements ItemListener,java.io.Serializable {
private String [] tags;
private int value;
private boolean isEditable;
private transient JComponent jcomp=null;
public SimParamIntTag(String aName,SimComponent comp,long creationTick,boolean isLoggable,
boolean editable,String [] sometags,int val) {
super(aName,comp,creationTick,isLoggable);
if(sometags==null) {
sometags=new String[1];
sometags[0]="N/A";
}
tags=sometags;
if(val<0 || val>=tags.length) val=0;
value=val;
isEditable=editable;
}
////////////// possible overrides //////////////////////////
public int getValue() {
return value;
}
public void setValue(int val) {
if(val<0 || val>=tags.length) return;
value=val;
if(jcomp!=null) scheduleUpdate();
}
//helper for thread safe GUI update
private void scheduleUpdate() {
Runnable r=new Runnable() {
public void run() {
if(isEditable) ((JComboBox)jcomp).setSelectedIndex(value);
else ((JLabel)jcomp).setText(getString());
}
};
SwingUtilities.invokeLater(r);
}
public boolean globalSetValue(String val) {
if(!isEditable) return false;
try {
int newval=Integer.parseInt(val.trim());
if(newval<0 || newval>tags.length) return false;
setValue(newval);
return true;
} catch(NumberFormatException ex) {
return false;
}
}
public String getString() {
return tags[value];
}
public JComponent getJComponent() {
if(jcomp==null) {
if(isEditable) {
jcomp=new JComboBox(tags);
((JComboBox)jcomp).setSelectedIndex(value);
((JComboBox)jcomp).addItemListener(this);
}
else {
jcomp=new JLabel(getString());
((JLabel)jcomp).setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(Color.black),
BorderFactory.createEmptyBorder(0,5,0,5)));
}
}
return jcomp;
}
public void itemStateChanged(ItemEvent e) {
if(jcomp!=null) {
value=((JComboBox)jcomp).getSelectedIndex();
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?