我的jtextarea有问题。我尝试使用textArea.append()在jtextarea中附加计算折扣详细信息,但textarea中没有显示。启动/停止RMI服务器textArea.append()似乎在单击按钮后工作正常。
public class ServerRMI extends UnicastRemoteObject implements InterfaceRMI
{
private JFrame frame;
private JTextArea textArea;
private static Registry registry;
private static ServerRMI rmiserver;
private static Date date;
private static DecimalFormat twoDForm;
/**
* Launch the application.
*/
public static void main(String[] args) throws Exception
{
rmiserver = new ServerRMI();
registry = LocateRegistry.createRegistry(8080);
date = new Date();
twoDForm = new DecimalFormat("#.##");
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ServerRMI window = new ServerRMI();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public ServerRMI() throws RemoteException
{
super();
initialize();
}
/**
* Calculate Discount.
*/
public double getDiscount(String value,String price,String isbn) throws RemoteException
{
double discount = ((Double.parseDouble(value))/100) * (Double.parseDouble(price));
double disprice = (Double.parseDouble(price)) - discount;
textArea.append(date.toString() + " - Discount " + value + "% " + "for ISBN " + isbn);
textArea.append(" Price RM" + price + ". Discounted Price: " + Double.valueOf(twoDForm.format(discount)));
textArea.append(". New Price: " + Double.valueOf(twoDForm.format(disprice)) + "\n");
return Double.valueOf(twoDForm.format(disprice));
}
/**
* Start the server.
*/
public void ServerStart()
{
textArea.append(date.toString() + " - Server is ready.\n");
try{
registry.rebind("MyServer",rmiserver);
}catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
/**
* Stop the server.
*/
public void ServerStop()
{
textArea.append(date.toString() + " - Server is Stopped.\n");
try{
UnicastRemoteObject.unexportObject(registry,true);
}catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setTitle("RMI Server");
frame.getContentPane().setFont(new Font("Tahoma", Font.PLAIN, 12));
frame.setBounds(100, 100, 800, 450);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JButton btnStartRmiServer = new JButton("Start RMI Server");
btnStartRmiServer.setFont(new Font("Trebuchet MS", Font.PLAIN, 11));
btnStartRmiServer.setBounds(10, 11, 127, 32);
frame.getContentPane().add(btnStartRmiServer);
btnStartRmiServer.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
ServerStart();
}
});
JButton btnStopRmiServer = new JButton("Stop RMI Server");
btnStopRmiServer.setFont(new Font("Trebuchet MS", Font.PLAIN, 12));
btnStopRmiServer.setBounds(147, 11, 127, 32);
frame.getContentPane().add(btnStopRmiServer);
btnStopRmiServer.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
ServerStop();
}
});
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 54, 764, 347);
frame.getContentPane().add(scrollPane);
textArea = new JTextArea();
textArea.setEditable(false);
textArea.setFont(new Font("Trebuchet MS", Font.PLAIN, 13));
scrollPane.setViewportView(textArea);
textArea.setWrapStyleWord(true);
}
}