Hallo,
in einem Applet füge ich zwei components, ein JProgressor und ein JTextField.
Wenn ich das Explorer Fenester verkleinere oder vergrössere, erscheint bei mir manchmal zweimal das JProgressor auf meine Seite.
So sieht ein Teil des Applets aus:
public class FlashServer extends Applet implements ActionListener {
ServerSocket sSocket = null;
Socket clientSocket = null;
PrintWriter out = null;
BufferedReader in = null;
JProgressBar pb;
Thread serverThread;
File fIn;
JTextField rDateFeld, sFeld, statusFeld;
JButton reloadbutton;
JButton openbutton;
private String sMaintenance;
String fileName, directory;
JFrame parent;
JPanel progressPane;
Dimension d, progressD;
GregorianCalendar cal = new GregorianCalendar();
SimpleDateFormat sdate = new SimpleDateFormat(„dd/MM/yyyy“);
boolean errorFlag = false;
public void init() {
String date = sdate.format(cal.getTime());
Color bgColor = new Color(222, 222, 222);
setLayout(new FlowLayout());
setBackground(bgColor);
JPanel globalPane = new JPanel();
globalPane.setLayout(new GridLayout(0, 1));
globalPane.setBackground(bgColor);
//globalPane.setBorder(new TitledBorder(„Reload“));
JPanel reloadPanel = new JPanel();
reloadPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
reloadPanel.setBackground(bgColor);
//filenamePane.setBackground(Color.RED);
d = new Dimension(750, 40);
reloadPanel.setPreferredSize(d);
reloadPanel.add(reloadLabel);
JPanel titlePanel = new JPanel();
titlePanel.setLayout(new FlowLayout(FlowLayout.LEFT));
titlePanel.setBackground(bgColor);
//filenamePane.setBackground(Color.RED);
JPanel statusPanel = new JPanel();
statusPanel.setLayout(new FlowLayout());
statusPanel.setBackground(bgColor);
statusFeld = new JTextField(25);
statusFeld.setEditable(false);
statusFeld.setText("");
statusPanel.add(statusLabel);
statusPanel.add(statusFeld);
progressPane = new JPanel();
progressPane.setBackground(bgColor);
pb = new JProgressBar(JProgressBar.HORIZONTAL, 0, length);
progressD = new Dimension(350, 25);
pb.setPreferredSize(progressD);
pb.setStringPainted(true);
progressPane.add(pb);
actionpane.setBackground(bgColor);
reloadbutton = new JButton(„Reload“);
d = new Dimension(80, 26);
reloadbutton.setPreferredSize(d);
reloadbutton.setEnabled(false);
reloadbutton.addActionListener(this);
openbutton = new JButton(„Open“);
openbutton.addActionListener(this);
openbutton.setPreferredSize(d);
if(sVersion!=null && sMaintenance==null)
openbutton.setEnabled(true);
else
openbutton.setEnabled(false);
actionpane.add(reloadbutton);
actionpane.add(openbutton);
globalPane.add(reloadPanel);
globalPane.add(titlePanel);
globalPane.add(progressPane);
globalPane.add(statusPanel);
globalPane.add(actionpane);
add(globalPane);
if(sVersion!=null)
{
versionfeld1.setText(sVersion);
versionfeld1.setToolTipText(sVersion);
}
else
{
errorManager(„Error: No valid actualy software version“, false, false);
}
}
private void startServerThread() {
Runnable r = new Runnable() {
public void run() {
runServerThread();
}
};
serverThread = new Thread(r, „Timer“);
serverThread.start();
}
private void stopServerThread() {
serverThread.interrupt();
}
public void runServerThread() {
char buf[] = new char[65536];
int read;
int DataLength = 0;
try {
sSocket = new ServerSocket(80);
System.out.println(„Server started“);
try {
System.out.println(„wait of Connection“);
clientSocket = sSocket.accept();
System.out.println(„Connection accepted“);
try {
out =
new PrintWriter(clientSocket.getOutputStream(), true);
in =
new BufferedReader(
new InputStreamReader(
clientSocket.getInputStream()));
String inputLine, outputLine;
FileReader bin = new FileReader(fileName);
statusFeld.setFont(new Font(„Arial“,Font.BOLD, 11));
statusFeld.setForeground(Color.black);
String s;
while ((read = bin.read(buf, 0, 65536)) > 1) {
out.write(buf, 0, read); // send data
out.flush();
s = in.readLine(); // read recieved dats
if(s!=null && !s.equals(„OK“))
{
System.out.println(„s1“ + s);
statusFeld.setText(s);
s = in.readLine(); // check data („data valid“)
statusFeld.setText(s);
System.out.println(„s2“ + s);
}
DataLength += read/2;
pb.setValue(DataLength);
}
s = in.readLine(); // read recieved data
if(s==null)
{
System.out.println(„s3“ + „null“);
}
else
{
System.out.println(„s4:“ + s);
statusFeld.setText(s);
}
if(s!=null && s.equals(„Ethernet Transfer Successful“))
{
do
{
s = in.readLine(); // read recieved data
//System.out.println(„s“ + s);
statusFeld.setText(s);
System.out.println(„s5“ + s);
if(!s.equals(„End of Programming Sector“)) // um null exception zu vermeiden
{
s = in.readLine(); // check data („data valid“)
statusFeld.setText(s);
System.out.println(„s6“ + s);
DataLength += length/(15*2);
pb.setValue(DataLength);
}
}while(!s.equals(„End of Programming Sector“));
}
s = in.readLine(); // Result of reprogramming
if(s.equals(„Flash Reprogramming complete with no errors“))
statusFeld.setText(s);
System.out.println(„s7“ + s);
//System.out.println(„File was successfully transfered“);
openbutton.setEnabled(true);// active button
out.close();
in.close();
clientSocket.close();
sSocket.close();
} catch (IOException e1) {
//e1.printStackTrace();
System.out.println(„Stream Error“);
errorManager(„Error: Stream Error.“, false, true);
}
} catch (IOException e) {
clientSocket = null;
System.out.println(„connection not accepted“);
errorManager(„Error: Connection not accepted.“, false, true);
}
} catch (IOException e) {
System.out.println(„Access denieded on port: 80.“);
errorManager(„Error: access denieded on port: 80.“, false, true);
}
}
public void destroy() {
if (errorFlag) {
stopServerThread();
try {
if (clientSocket != null)
{ if(out!= null)
{
out.close();
System.out.println(„output closed“);
}
if(in!=null)
{
in.close();
System.out.println(„input closed“);
}
clientSocket.close();
System.out.println(„client closed“);
}
if (sSocket != null){
sSocket.close();
System.out.println(„server closed“);
}
} catch (IOException e1) {
sSocket = null;
}
}
}
public void actionPerformed(ActionEvent eve) {
String cmd;
cmd = eve.getActionCommand();
String urlString = new String(getCodeBase() + „flashClient.cgi“);
if (cmd.equals(„Reload“)) {
try {
statusFeld.setFont(new Font(„Arial“,Font.BOLD, 11));
statusFeld.setForeground(Color.black);
statusFeld.setText(„Programming started“);
//System.out.println(„in reload“);
getAppletContext().showDocument(new URL(urlString), „ausgab“);
openbutton.setEnabled(false);
reloadbutton.setEnabled(false);
//errorFlag = true; // d.h server ist gestartet// es wird in destroy benutzt
} catch (java.net.MalformedURLException e) {
System.out.println(e.toString());
}
}
if (cmd.equals(„Open“)) {
parent = new JFrame(„Open“);
parent.setLocation(500, 300);
FileDialog fd =
new FileDialog(
parent,
„Please choose a file:“,
FileDialog.LOAD);
fd.setFile("*.hex");
fd.show();
fd.dispose();
directory = fd.getDirectory();
fileName = fd.getFile();
pb.setValue(0);
if (directory != null && fileName != null) {
fileName = directory + fileName;
fIn = new File(fileName);
length = (int) fIn.length();
statusFeld.setFont(new Font(„Arial“,Font.BOLD, 11));
statusFeld.setForeground(Color.black);
statusFeld.setText(„File opened“);
pb.setMaximum(length);
pb.setStringPainted(true);
repaint();
reloadbutton.setEnabled(true);
openbutton.setEnabled(false);
startServerThread();
errorFlag = true; // d.h server ist gestartet// es wird in destroy benutzt
} else {
fileName = „“;
errorFlag = false;
sFeld.setText(fileName);
sVersion = „“;
versionfeld2.setText(sVersion);
errorManager(„Error: No valid file name!“, false, true);
}
}
}
public void errorManager(String s, boolean reload, boolean open) {
statusFeld.setFont(new Font(„Arial“,Font.BOLD, 11));
statusFeld.setForeground(Color.red);
statusFeld.setText(s);
reloadbutton.setEnabled(reload);
openbutton.setEnabled(open);
}
}
Was mache ich überhaupt falsch und wie kann man so ein Problem lösen?
Grüss.
Hicham