simparamnsap.java
来自「一个小型网络仿真器的实现」· Java 代码 · 共 112 行
JAVA
112 行
/*
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.*;
import java.math.*;
public class SimParamNSAP extends SimParameter implements ActionListener,java.io.Serializable {
private BigInteger value;
private boolean isEditable;
private transient JComponent jcomp=null;
public SimParamNSAP(String aName,SimComponent comp,long creationTick,boolean isLoggable,
boolean editable,String val) {
super(aName,comp,creationTick,isLoggable);
try {
value=new BigInteger(val,16);
} catch (NumberFormatException e) {
value=new BigInteger("ffffffffffffffffffffffffffffffffffffffff",16);
}
isEditable=editable;
}
////////////// possible overrides //////////////////////////
public String getValue() {
return value.toString(16);
}
public void setValue(String val) {
try {
value=new BigInteger(val,16);
} catch(NumberFormatException e) {
value=new BigInteger("ffffffffffffffffffffffffffffffffffffffff",16);
}
if(jcomp!=null) scheduleUpdate();
}
//helper for thread safe GUI update
private void scheduleUpdate() {
Runnable r=new Runnable() {
public void run() {
if(isEditable) ((JButton)jcomp).setText(value.toString(16));
else ((JLabel)jcomp).setText(value.toString(16));
}
};
SwingUtilities.invokeLater(r);
}
public boolean globalSetValue(String val) {
if(!isEditable) return false;
try {
val=val.trim();
BigInteger newval=new BigInteger(val,16);
setValue(val);
return true;
} catch(NumberFormatException ex) {
return false;
}
}
public String getString() {
return value.toString(16);
}
public JComponent getJComponent() {
if(jcomp==null) {
if(isEditable) {
jcomp=new JButton(value.toString(16));
((JButton)jcomp).setHorizontalAlignment(JButton.LEFT);
((JButton)jcomp).addActionListener(this);
}
else {
jcomp=new JLabel(value.toString(16));
((JLabel)jcomp).setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(Color.black),
BorderFactory.createEmptyBorder(0,5,0,5)));
}
}
return jcomp;
}
public void actionPerformed(ActionEvent e) {
String newVal=JOptionPane.showInputDialog(theGUI.getMainFrame(),
"New value:",getName(),JOptionPane.QUESTION_MESSAGE);
if(newVal==null) return;
if(!globalSetValue(newVal)) {
JOptionPane.showMessageDialog(theGUI.getMainFrame(),"Not a valid value!","Error",
JOptionPane.INFORMATION_MESSAGE);
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?