help to checking the error!!
the question is You can change the size of a JFrame by using the setSize(int h, int v) method, where h and v give the horizontal and vertical dimensions of the applet’s window in pixels. Write a GUI application that contains two JButtons, labeled “Big” and “Small.” Whenever the user clicks on Small, set the applets dimensions to 200 x 100, and whenever the user clicks on Big, set the dimensions to 300 x 200.
my program on below:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class tma2 extends JFrame implements ActionListener
{
public tma2()
{
JButton startButton = new JButton("BIG");
JButton startButton = new JButton("SMALL");
JPanel panel = new JPanel();
panel.add(startButton);
}
public void actionPerformed(ActionEvent e)
{
Object src = evt.getSource();
if (src == button1) {
setSize(200,100);
}
else if (src == button2)
{
setSize(300,200);
}
}
}
please let me know the error of this question!!!
Re: help to checking the error!!
Quote:
Originally Posted by
vitorloke
please let me know the error of this question!!!
What error? Post it in its entirety, and while we want to help don't expect us to know what you are talking about - be precise and concise in your question(s)
1 Attachment(s)
Re: help to checking the error!!
pls refer the image on below:
Attachment 1190
Re: help to checking the error!!
can you let me know why i just only change 1 time only!if i want to click each time to change the size then how to modify?below is my code:
Code :
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class tma2 extends JFrame implements ActionListener
{
private JButton m_btn1;
private int index;
public tma2()
{
initialize();
}
private void initialize()
{
index = 0;
m_btn1 = new JButton("BIG");
m_btn1.addActionListener(this);
setLayout(new BorderLayout());
add(m_btn1, BorderLayout.NORTH);
}
public void actionPerformed(ActionEvent e)
{
if(index < 0)
{
index++;
}
else
{
setSize(300, 100);
}
}
public static void main(String[] p)
{
tma2 thisFrame = new tma2();
thisFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisFrame.setSize(200,200);
thisFrame.setVisible(true);
}
}
Re: help to checking the error!!
Quote:
Originally Posted by
vitorloke
can you let me know why i just only change 1 time only!if i want to click each time to change the size then how to modify?
Hello vitorloke!
You are not changing only one time the size. You are changing it every time you click the button. The problem is that every time you are setting it to the same size. setSize(300,200);
To understand that it works every time you click the button, print a message when the else block executes.
Re: help to checking the error!!
Quote:
Originally Posted by
andreas90
Hello vitorloke!
You are not changing only one time the size. You are changing it every time you click the button. The problem is that every time you are setting it to the same size. setSize(300,200);
To understand that it works every time you click the button, print a message when the else block executes.
this is my last modify code:
Code :
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class tma2 extends JFrame implements ActionListener
{
private JButton m_btn1;
private int index;
public tma2()
{
initialize();
}
private void initialize()
{
index = 0;
m_btn1 = new JButton("BIG");
m_btn1.addActionListener(this);
setLayout(new BorderLayout());
add(m_btn1, BorderLayout.NORTH);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == 0)
{
setSize(200, 100);
}
else
{
setSize(300, 100);
}
}
public static void main(String[] p)
{
tma2 thisFrame = new tma2();
thisFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisFrame.setSize(200,200);
thisFrame.setVisible(true);
}
}
Re: help to checking the error!!
Quote:
Originally Posted by
vitorloke
this is my last modify code:
Code :
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class tma2 extends JFrame implements ActionListener
{
private JButton m_btn1;
private int index;
public tma2()
{
initialize();
}
private void initialize()
{
index = 0;
m_btn1 = new JButton("BIG");
m_btn1.addActionListener(this);
setLayout(new BorderLayout());
add(m_btn1, BorderLayout.NORTH);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == 0)
{
setSize(200, 100);
}
else
{
setSize(300, 100);
}
}
public static void main(String[] p)
{
tma2 thisFrame = new tma2();
thisFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisFrame.setSize(200,200);
thisFrame.setVisible(true);
}
}
Do you have a problem with it (error, exception, unexpected behaviour)?
Re: help to checking the error!!
yup!when i run this program the frame size only chenge 1 time only,if i click again the frame size cannot changer to other!can you let me how to modify the statement!
Re: help to checking the error!!
Quote:
Originally Posted by
vitorloke
yup!when i run this program the frame size only chenge 1 time only,if i click again the frame size cannot changer to other!can you let me how to modify the statement!
Firstly, I don't think the last posted code will compile. To be more specific the following line will give you an error.
if(e.getSource() == 0)
e.getSource() should equal the button you have created.
To your question now:
did you try what i suggested in post #4? I tell you that because it is critical to understand why it isn't changing (it actually).
You can use the index variable to the setSize() method and increment or decrement it every time you click the button to get what you want.
Re: help to checking the error!!
how to use the index variable to the setSize() method to change the size!since i just want to change by 2 difference type!so how to create the statement?
Re: help to checking the error!!
Quote:
Originally Posted by
vitorloke
how to use the index variable to the setSize() method to change the size!since i just want to change by 2 difference type!so how to create the statement?
What do you mean by "i just want to change 2 difference type"?
Can you post your current code and elaborate your requirements?
Re: help to checking the error!!
i mean just want to set (200,100) and (300,200)
Re: help to checking the error!!
Quote:
Originally Posted by
vitorloke
i mean just want to set (200,100) and (300,200)
If I'm getting it right you want to set the size (200,100) the first time the button is clicked, (300,200) the second time so on.
So if you want to do this with one button only you can use the index value in the actionPerformed() method you have with the following logic;
if (index is even)*
setSize(200, 100);
else
setSize (300, 100);
increment index by one
*You can either test if the index is odd instead of even.
Hint: an int x is even when x % 2 == 0.