-
Array: button problem need help.
Need some help regarding my code. Button btnMin not functioning well. When I try to click btnFill then btnMin, the button works fine, but when I try to click the four buttons consecutively, btnMin failed to work. Hope you can help me.
Code:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.util.*;
public class array extends Applet implements ActionListener
{
Button btnFill = new Button("Fill Array");
Button btnSort = new Button("Sort");
Button btnMax = new Button("Find Maximum");
Button btnMin = new Button("Find Minimum");
TextArea Display = new TextArea(10,20);
Label lblMaxMin = new Label("##################");
int[] myArray = new int[5];
int x;
String strOutput = "";
public void init()
{
Panel pnl1 = new Panel();
Panel pnl2 = new Panel();
Panel pnl3 = new Panel();
pnl1.setLayout(new GridLayout(4,0));
pnl1.add(btnFill); btnFill.addActionListener(this);
pnl1.add(btnSort); btnSort.addActionListener(this);
pnl1.add(btnMax); btnMax.addActionListener(this);
pnl1.add(btnMin); btnMin.addActionListener(this);
pnl2.setLayout(new GridLayout(1,1));
pnl2.add(Display);
pnl3.setLayout(new GridLayout(6,0));
pnl3.add(lblMaxMin);
add(pnl1);
add(pnl2);
add(pnl3);
}
public void actionPerformed(ActionEvent a)
{
Object Obj = a.getSource();
if(Obj == btnFill)
{
Display.setText("");
for(int x=0; x<myArray.length; x++)
{
myArray[x] = (int)(Math.random()*100)+1;
Display.append("[" + (x+1) + "]" + " " + myArray[x] + "\n");
}
}
else if(Obj == btnSort)
{
Display.setText("");
Arrays.sort(myArray);
for(int x=0; x<myArray.length; x++)
{
Display.append("[" + (x+1) + "]" + " " + myArray[x] + "\n");
}
}
else if(Obj == btnMax)
{
lblMaxMin.setText("");
int max = myArray[0];
for(int x=0; x<myArray.length; x++)
{
if(myArray[x] > max)
{
max = myArray[x];
lblMaxMin.setText("Maximum Number =" + max);
}
}
}
else
{
lblMaxMin.setText("");
int min = myArray[0];
for(int x=0; x<myArray.length; x++)
{
if(myArray[x] < min)
{
min = myArray[x];
lblMaxMin.setText("Minimum Number =" + min);
}
}
}
}
}
-
replace you code with this
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.util.*;
public class array extends Applet implements ActionListener
{
Button btnFill = new Button("Fill Array");
Button btnSort = new Button("Sort");
Button btnMax = new Button("Find Maximum");
Button btnMin = new Button("Find Minimum");
TextArea Display = new TextArea(10,20);
Label lblMaxMin = new Label("##################");
int[] myArray = new int[5];
int x;
String strOutput = "";
public void init()
{
Panel pnl1 = new Panel();
Panel pnl2 = new Panel();
Panel pnl3 = new Panel();
pnl1.setLayout(new GridLayout(4,0));
pnl1.add(btnFill); btnFill.addActionListener(this);
pnl1.add(btnSort); btnSort.addActionListener(this);
pnl1.add(btnMax); btnMax.addActionListener(this);
pnl1.add(btnMin); btnMin.addActionListener(this);
pnl2.setLayout(new GridLayout(1,1));
pnl2.add(Display);
pnl3.setLayout(new GridLayout(6,0));
pnl3.add(lblMaxMin);
add(pnl1);
add(pnl2);
add(pnl3);
}
public void actionPerformed(ActionEvent a)
{
Object Obj = a.getSource();
if(Obj == btnFill)
{
Display.setText("");
for(int x=0; x<myArray.length; x++)
{
myArray[x] = (int)(Math.random()*100)+1;
Display.append("[" + (x+1) + "]" + " " + myArray[x] + "\n");
}
}
else if(Obj == btnSort)
{
Display.setText("");
Arrays.sort(myArray);
for(int x=0; x<myArray.length; x++)
{
Display.append("[" + (x+1) + "]" + " " + myArray[x] + "\n");
}
}
else if(Obj == btnMax)
{
lblMaxMin.setText("");
int max = myArray[0];
for(int x=0; x<myArray.length; x++)
{
if(myArray[x] > max)
{
max = myArray[x];
}
}
lblMaxMin.setText("Maximum Number =" + max);
}
else if (Obj==btnMin)
{
//lblMaxMin.setText("");
int min = myArray[0];
for(int x=0; x<myArray.length; x++)
{
if(myArray[x] < min)
{
min = myArray[x];
}
}
lblMaxMin.setText("Minimum Number =" + min);
}
}
}
// just put set text outside the loop
Similar Threads
-
By James Graham in forum .NET
Replies: 4
Last Post: 10-23-2011, 02:47 PM
-
By shamsam1 in forum VB Classic
Replies: 6
Last Post: 04-23-2008, 10:26 AM
-
By brouse in forum VB Classic
Replies: 1
Last Post: 05-10-2005, 02:19 PM
-
By Ravindra in forum ASP.NET
Replies: 1
Last Post: 05-12-2003, 11:56 PM
-
By David Vowles in forum VB Classic
Replies: 0
Last Post: 10-12-2001, 12:32 PM
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks