import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test extends JApplet {
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
JPanel panel4 = new JPanel();
JButton btn1=new JButton("BTN1");
JButton btn2=new JButton("BTN2");
JButton btn3=new JButton("BTN3");
JButton btn4=new JButton("BTN4");
JButton btn5=new JButton("BTN5");
JButton btn6=new JButton("BTN6");
JButton btn7=new JButton("BTN7");
JButton btn8=new JButton("BTN8");
JButton btn9=new JButton("BTN9");
public Test() {
panel1.add(btn1);
add(panel1, BorderLayout.WEST);
add(panel2,BorderLayout.EAST);
add(panel3, BorderLayout.SOUTH);
add(panel4, BorderLayout.NORTH);
btn1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
panel2.add(btn2);
panel2.add(btn3);
panel2.add(btn4);
panel2.add(btn5);
panel2.add(btn6);
panel2.add(btn7);
panel2.add(btn8);
panel2.add(btn9);
repaint();
}
});
}
public static void main(String[] args){
JFrame frame= new JFrame();
Test applet=new Test();
frame.add(applet,BorderLayout.CENTER);
frame.setSize(300,300);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
applet.init();
}
}
autoresize JFrame or JApplet
Page 1 of 110 Replies - 202 Views - Last Post: 14 February 2013 - 03:19 PM
#1
autoresize JFrame or JApplet
Posted 13 February 2013 - 03:39 PM
Replies To: autoresize JFrame or JApplet
#2
Re: autoresize JFrame or JApplet
Posted 13 February 2013 - 04:53 PM
public static JFrame frame;
so that you can reference it in the event:
panel2.add(btn8);
panel2.add(btn9);
frame.pack();
There may be a way to find frame through the object hierarchy, avoiding the need for the static reference, but I couldn't locate it using getParent() or getTopLevelAncestor().
This post has been edited by andrewsw: 13 February 2013 - 04:54 PM
#3
Re: autoresize JFrame or JApplet
Posted 13 February 2013 - 04:57 PM
#4
Re: autoresize JFrame or JApplet
Posted 13 February 2013 - 05:04 PM
public class MainFrame extends JFrame {
private Timer timer;
private int count = 1;
public MainFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
add(new JButton("button " + count));
pack();
setLocationRelativeTo(null);
timer = new Timer(3000, adder);
timer.start();
}
private ActionListener adder = new ActionListener() {
public void actionPerformed(ActionEvent e) {
add(new JButton("Button " + String.valueOf(++count)));
pack();
}
};
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new MainFrame().setVisible(true);
}
});
}
}//end awesomeness
This post has been edited by farrell2k: 13 February 2013 - 05:05 PM
#5
Re: autoresize JFrame or JApplet
Posted 13 February 2013 - 05:14 PM
package comp351assign1;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test extends JApplet {
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
JPanel panel4 = new JPanel();
JButton btn1=new JButton("BTN1");
JButton btn2=new JButton("BTN2");
JButton btn3=new JButton("BTN3");
JButton btn4=new JButton("BTN4");
JButton btn5=new JButton("BTN5");
JButton btn6=new JButton("BTN6");
JButton btn7=new JButton("BTN7");
JButton btn8=new JButton("BTN8");
JButton btn9=new JButton("BTN9");
public Test() {
panel1.add(btn1);
add(panel1, BorderLayout.WEST);
add(panel2,BorderLayout.EAST);
add(panel3, BorderLayout.SOUTH);
add(panel4, BorderLayout.NORTH);
btn1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
panel2.add(btn2);
panel2.add(btn3);
panel2.add(btn4);
panel2.add(btn5);
panel2.add(btn6);
panel2.add(btn7);
panel2.add(btn8);
panel2.add(btn9);
repaint();
}
});
}
public static void main(String[] args){
public static JFrame frame= new JFrame();
frame.setLayout(new BorderLayout());
Test applet=new Test();
frame.add(applet,BorderLayout.CENTER);
frame.setSize(300,300);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
applet.init();
}
}
Thanks for the replies, but neither of them worked for this code. I tried both of your suggestions (see code below) and wasn't able to get to resize. With the static Jframe object, I got an error saying this wan an illegal start for an expression.
package comp351assign1;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test extends JApplet {
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
JPanel panel4 = new JPanel();
JButton btn1=new JButton("BTN1");
JButton btn2=new JButton("BTN2");
JButton btn3=new JButton("BTN3");
JButton btn4=new JButton("BTN4");
JButton btn5=new JButton("BTN5");
JButton btn6=new JButton("BTN6");
JButton btn7=new JButton("BTN7");
JButton btn8=new JButton("BTN8");
JButton btn9=new JButton("BTN9");
public Test() {
panel1.add(btn1);
add(panel1, BorderLayout.WEST);
add(panel2,BorderLayout.EAST);
add(panel3, BorderLayout.SOUTH);
add(panel4, BorderLayout.NORTH);
btn1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
panel2.add(btn2);
panel2.add(btn3);
panel2.add(btn4);
panel2.add(btn5);
panel2.add(btn6);
panel2.add(btn7);
panel2.add(btn8);
panel2.add(btn9);
repaint();
}
});
}
public static void main(String[] args){
public static JFrame frame= new JFrame();
frame.setLayout(new BorderLayout());
Test applet=new Test();
frame.add(applet,BorderLayout.CENTER);
frame.setSize(300,300);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
applet.init();
}
}
#6
Re: autoresize JFrame or JApplet
Posted 13 February 2013 - 05:18 PM
You also need to make your JFrame an instance variable, instead of a local on in main(). No need for static.
#7
Re: autoresize JFrame or JApplet
Posted 13 February 2013 - 05:22 PM
#8
Re: autoresize JFrame or JApplet
Posted 13 February 2013 - 05:31 PM
#9
Re: autoresize JFrame or JApplet
Posted 14 February 2013 - 04:48 AM
A JFrame can resize itself but you have to call validate() after adding component(s).
P.S.
not a good idea at all to add JComponent(s) to a container on the fly, this can screw up many layout manager. Better to add all JComponet at construction time, make them setVisible(false) if required and toggle their visibility later on
#10
Re: autoresize JFrame or JApplet
Posted 14 February 2013 - 02:01 PM
#11
Re: autoresize JFrame or JApplet
Posted 14 February 2013 - 03:19 PM
An Applet size is defined in its .html file
A JFrame will display new added component if validate() is called
and yes you have to call pack() if it was called before or setSize() if the old size is not large enough to display the new added component
|
|

New Topic/Question
Reply




MultiQuote





|