Hi,
Can anybody tell me how can i set attributes of frame or components in a frame in ActionListener of a button.
Regards.
Hi,
Can anybody tell me how can i set attributes of frame or components in a frame in ActionListener of a button.
Regards.
Just quick pseudo-code
public void actionPerfomed
if resize
myFrame.setSize(new Dimension(300, 300))
else if changeFont
myFrame.setFont(new Font( "Serif", Font.BOLD, 20 ));
you should get idea of it
Just quick pseudo-code
public void actionPerfomed if resize myFrame.setSize(new Dimension(300, 300)) else if changeFont myFrame.setFont(new Font( "Serif", Font.BOLD, 20 ));
you should get idea of it
i have tried what has been told above as
this.setSize();
but "this" is not recognized within ActionListener.
And i have extended frame class so there is no name for the frame as well.
Regards.
Nobody knows any solution of the problem???
post some relevant code please
"this" IS recognised inside the actionlistener, it's just not what you think it is.
Learn a bit more about Java and you will see why, and what "this" is out there.
At the moment you're expecting quick hacks for everything without really understanding what you're doing.
That's the wrong way to go about programming.
Do simple things first until you thoroughly understand those, and only then try something more complicated (and adding an actionListener is NOT complicated, so if you have problems with that you need to really step back a lot).
post some relevant code please
class ImgMod8 extends Frame { String msg = ""; int mX1 = 150; int mY1 = 150; int mX2 = 0; int mY2 = 0; int turn = 1; static int dst = 0; double sum = 0.0; int count; Image rawImg; int imgCols;//Number of horizontal pixels int imgRows;//Number of rows of pixels Image modImg;//Reference to modified image //Inset values for the Frame int inTop; int inLeft; //To add only one picture at a time int ig = 1; static String theProcessingClass = "PrintMouse5"; // This image file will be processed if another file name is // not entered on the command line. static String theImgFile = "white.gif"; MediaTracker tracker; Display display = new Display();//A Canvas Button replotButton = new Button("Replot"); //References to arrays that store pixel data. int[][][] threeDPix; int[][][] threeDPixMod; int[] oneDPix; //Reference to the image-processing object. ImgIntfc04 imageProcessingObject; //-------------------------------------------// public static void main(String[] args){ // Program supports gif files and jpg files // and possibly some other file types as well. //Display name of processing program and // image file. System.out.println("Processing program: " + theProcessingClass); System.out.println("Image file: " + theImgFile); //Instantiate an object of this class ImgMod8 obj = new ImgMod8(); }//end main //-------------------------------------------// public ImgMod8(){//constructor //Get an image from the specified file. Can // be in a different directory if the path // was entered with the file name on the command line. rawImg = Toolkit.getDefaultToolkit().getImage(theImgFile); //Use a MediaTracker object to block until // the image is loaded or ten seconds has elapsed. tracker = new MediaTracker(this); tracker.addImage(rawImg,1); try{ if(!tracker.waitForID(1,10000)){ System.out.println("Load error."); System.exit(1); }//end if }catch(InterruptedException e){ e.printStackTrace(); System.exit(1); }//end catch //Make certain that the file was successfully // loaded. if((tracker.statusAll(false) & MediaTracker.ERRORED & MediaTracker.ABORTED) != 0){ System.out.println("Load errored or aborted"); System.exit(1); }//end if //Raw image has been loaded. Get width and // height of the raw image. imgCols = rawImg.getWidth(this); imgRows = rawImg.getHeight(this); this.setTitle("Get Pixel Distance"); this.setBackground(Color.YELLOW); this.add(display); this.add(replotButton,BorderLayout.SOUTH); //Make it possible to get insets and the // height of the button. setVisible(true); //Get and store inset data for the Frame and // the height of the button. inTop = this.getInsets().top; inLeft = this.getInsets().left; int buttonHeight = replotButton.getSize().height; //Size the frame so that a small amount of // yellow background will show on the right // and on the bottom when both images are // displayed, one above the other. Also, the // placement of the images on the Canvas // causes a small amount of background to // show between the images. this.setSize(inLeft+imgCols + 1,inTop + buttonHeight + imgRows + 7); //=========================================// //Anonymous inner class listener for replot // button. This actionPerformed method is // invoked when the user clicks the Replot // button. It is also invoked at startup // when this program posts an ActionEvent to // the system event queue attributing the // event to the Replot button. replotButton.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e){ //Calculate distance double a = mX1-mX2; double b = mY1-mY2; sum=(Math.sqrt(a*a+b*b)) ; System.out.println("This Distance is: " + sum); //Pass a 3D array of pixel data to the // processing object and get a modified 3D array of pixel data back. threeDPixMod = imageProcessingObject.processImg(threeDPix, imgRows, imgCols, mX2, mY2); //Convert the modified pixel data to a // 1D array of pixel data. oneDPix = convertToOneDim(threeDPixMod,imgCols,imgRows); //Use the createImage() method to // create a new image from the 1D array of pixel data. modImg = createImage(new MemoryImageSource(imgCols,imgRows,oneDPix,0,imgCols)); //Repaint the image display frame with // the original image at the top and // the modified pixel data at the bottom. display.repaint(); }//end actionPerformed }//end ActionListener );//end addActionListener //End anonymous inner class. //=========================================//
Here is the code i am using.
As i have extended the frame it has no name. And "this" in actionListener points to the button. Then how can i use frame.setSize()
post some relevant code please
Below is the code i am using.
As i have extended the frame it has no name. And "this" in actionListener points to the button. Then how can i use frame.setSize()
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
class ImgMod8 extends Frame {
String msg = "";
int mX1 = 150;
int mY1 = 150;
int mX2 = 0;
int mY2 = 0;
int turn = 1;
static int dst = 0;
double sum = 0.0;
int count;
Image rawImg;
int imgCols;//Number of horizontal pixels
int imgRows;//Number of rows of pixels
Image modImg;//Reference to modified image
//Inset values for the Frame
int inTop;
int inLeft;
//To add only one picture at a time
int ig = 1;
//This program will be executed to process the
// if the name of another program is not
// entered on the command line.
static String theProcessingClass = "PrintMouse5";
// This image file will be processed if another file name is
// not entered on the command line.
static String theImgFile = "white.gif";
MediaTracker tracker;
Display display = new Display();//A Canvas
Button replotButton = new Button("Replot");
//References to arrays that store pixel data.
int[][][] threeDPix;
int[][][] threeDPixMod;
int[] oneDPix;
//Reference to the image-processing object.
ImgIntfc04 imageProcessingObject;
//-------------------------------------------//
public static void main(String[] args){
// Program supports gif files and jpg files
// and possibly some other file types as well.
if(args.length == 0){
//Use default processing class and default image file. No code required here.
// Class and file names were specified above.
}else if(args.length == 1){
theProcessingClass = args[0];
//Use default image file
}else if(args.length == 2){
theProcessingClass = args[0];
theImgFile = args[1];
}else if(args.length == 3){
theProcessingClass = args[0];
theImgFile = args[1];
dst = Integer.parseInt(args[2]);
}else{
System.out.println("Invalid args");
System.exit(1);
}//end else
//Display name of processing program and
// image file.
System.out.println("Processing program: " + theProcessingClass);
System.out.println("Image file: " + theImgFile);
//Instantiate an object of this class
ImgMod8 obj = new ImgMod8();
}//end main
//-------------------------------------------//
public ImgMod8(){//constructor
//Get an image from the specified file. Can
// be in a different directory if the path
// was entered with the file name on the command line.
rawImg = Toolkit.getDefaultToolkit().getImage(theImgFile);
//Use a MediaTracker object to block until
// the image is loaded or ten seconds has elapsed.
tracker = new MediaTracker(this);
tracker.addImage(rawImg,1);
try{
if(!tracker.waitForID(1,10000)){
System.out.println("Load error.");
System.exit(1);
}//end if
}catch(InterruptedException e){
e.printStackTrace();
System.exit(1);
}//end catch
//Make certain that the file was successfully
// loaded.
if((tracker.statusAll(false) & MediaTracker.ERRORED & MediaTracker.ABORTED) != 0){
System.out.println("Load errored or aborted");
System.exit(1);
}//end if
//Raw image has been loaded. Get width and
// height of the raw image.
imgCols = rawImg.getWidth(this);
imgRows = rawImg.getHeight(this);
this.setTitle("Get Pixel Distance");
this.setBackground(Color.YELLOW);
this.add(display);
this.add(replotButton,BorderLayout.SOUTH);
//Make it possible to get insets and the
// height of the button.
setVisible(true);
//Get and store inset data for the Frame and
// the height of the button.
inTop = this.getInsets().top;
inLeft = this.getInsets().left;
int buttonHeight = replotButton.getSize().height;
//Size the frame so that a small amount of
// yellow background will show on the right
// and on the bottom when both images are
// displayed, one above the other. Also, the
// placement of the images on the Canvas
// causes a small amount of background to
// show between the images.
this.setSize(inLeft+imgCols + 1,inTop + buttonHeight + imgRows + 7);
//=========================================//
//Anonymous inner class listener for replot
// button. This actionPerformed method is
// invoked when the user clicks the Replot
// button. It is also invoked at startup
// when this program posts an ActionEvent to
// the system event queue attributing the
// event to the Replot button.
replotButton.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
//Calculate distance
double a = mX1-mX2;
double b = mY1-mY2;
sum=(Math.sqrt(a*a+b*b)) ;
System.out.println("This Distance is: " + sum);
//Pass a 3D array of pixel data to the
// processing object and get a modified 3D array of pixel data back.
threeDPixMod = imageProcessingObject.processImg(threeDPix, imgRows, imgCols, mX2, mY2);
//Convert the modified pixel data to a
// 1D array of pixel data.
oneDPix = convertToOneDim(threeDPixMod,imgCols,imgRows);
//Use the createImage() method to
// create a new image from the 1D array of pixel data.
modImg = createImage(new MemoryImageSource(imgCols,imgRows,oneDPix,0,imgCols));
//Repaint the image display frame with
// the original image at the top and
// the modified pixel data at the bottom.
display.repaint();
}//end actionPerformed
}//end ActionListener
);//end addActionListener
//End anonymous inner class.
//=========================================//
no, "this" in the actionlistener points to the actionlistener...
this in any class instance points to that instance.
And don't put comments on every closing brace, it's not needed and makes your code that much harder to read.
And don't use C++ style comments for blocks.
try having the action listener call a method that resizes the frame instead of resizing the frame directly
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.