James Hancock
asked on
Why do JFrames make java components indiscernible?
Hi
Awt Java frames seemed seamless.
Create component, set bounds, add it, create listener, done,
but JFrames always seem to have errors in design and construction and look and feel.
In my attached code, I try and make a normal frame and add a text area on a blue window in certain bounds.
When I set it to visible, I can't see the Text area. I can click on the frame and locate the text area, but the text is writable only off in one corner, not within its bounds? It explodes to the whole window? Why don't JFrame components behave as expected? I want the text area to be framed and discernable from the start, and have text writable within that area only.
Code...
import java.awt.Color;
import javax.swing.*;
public class controlFrame extends JFrame {
JTextArea textarea;
public controlFrame() {
this.setSize(900, 600);
this.setBackground(Color.P INK);
this.setVisible(true);
textarea = new JTextArea();
textarea.setSize(500, 100);
textarea.setLocation(100, 100);
textarea.setText("Text Area");
textarea.setVisible(true);
add(textarea);
}
public static void main(String[] args) {
new controlFrame();
}
}
I want it to look like a normal window with components that would appear in any professional application. Are JFrames only intended for heavily graphical applications?
Must I go back to AWT ?
Thanks
Awt Java frames seemed seamless.
Create component, set bounds, add it, create listener, done,
but JFrames always seem to have errors in design and construction and look and feel.
In my attached code, I try and make a normal frame and add a text area on a blue window in certain bounds.
When I set it to visible, I can't see the Text area. I can click on the frame and locate the text area, but the text is writable only off in one corner, not within its bounds? It explodes to the whole window? Why don't JFrame components behave as expected? I want the text area to be framed and discernable from the start, and have text writable within that area only.
Code...
import java.awt.Color;
import javax.swing.*;
public class controlFrame extends JFrame {
JTextArea textarea;
public controlFrame() {
this.setSize(900, 600);
this.setBackground(Color.P
this.setVisible(true);
textarea = new JTextArea();
textarea.setSize(500, 100);
textarea.setLocation(100, 100);
textarea.setText("Text Area");
textarea.setVisible(true);
add(textarea);
}
public static void main(String[] args) {
new controlFrame();
}
}
I want it to look like a normal window with components that would appear in any professional application. Are JFrames only intended for heavily graphical applications?
Must I go back to AWT ?
Thanks
public controlFrame() {
this.setSize(900, 600);
this.setBackground(Color.PINK);
this.setVisible(true);
textarea = new JTextArea();
textarea.setSize(500, 100);
textarea.setLocation(100, 100);
textarea.setText("Text Area");
textarea.setVisible(true);
add(textarea);
this.pack();
}
In general, the problems you are facing are because Swing uses Layout Managers to position components within their parent containers.Yes, but of course AWT is no different in that respect. Layout manages are AWT
ASKER
Now, it seems that once I have instantiated the frame, it vanishes. I added Sleeps around to help lengthen the time of the program before it ends. The frame still vanishes, at once!
Is the layout manager system simple?
It creates a textArea out of the frame, over in the top left corner in its own window, not added inside the frame! ! ?
import java.awt.Color;
import javax.swing.*;
public class controlFrame extends JFrame {
JTextArea textarea;
public controlFrame() {
this.setSize(900, 600);
this.setBackground(Color.P INK);
this.setVisible(true);
textarea = new JTextArea();
textarea.setSize(200, 80);
textarea.setLocation(100, 100);
textarea.setText("Text Area");
textarea.setVisible(true);
add(textarea);
this.pack();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
JFrame f = new controlFrame();
System.out.println("After new controlframe");
try {
Thread.sleep(15000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Is the layout manager system simple?
It creates a textArea out of the frame, over in the top left corner in its own window, not added inside the frame! ! ?
import java.awt.Color;
import javax.swing.*;
public class controlFrame extends JFrame {
JTextArea textarea;
public controlFrame() {
this.setSize(900, 600);
this.setBackground(Color.P
this.setVisible(true);
textarea = new JTextArea();
textarea.setSize(200, 80);
textarea.setLocation(100, 100);
textarea.setText("Text Area");
textarea.setVisible(true);
add(textarea);
this.pack();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
JFrame f = new controlFrame();
System.out.println("After new controlframe");
try {
Thread.sleep(15000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Your ctor isn't making a lot of sense at the moment. This makes a bit more:
public controlFrame() {
setSize(900, 600);
setBackground(Color.PINK);
textarea = new JTextArea();
textarea.setText("Text Area");
add(textarea);
setVisible(true);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Though i'm not sure what the sleeping is about
ASKER
The sleep is to try and make the JVM stay alive long enough, so that it doesn't let the frame disappear.
Does the frame die because it has no listeners?
Thx
Does the frame die because it has no listeners?
Thx
The sleeps() don't do anything, except delay your disappointment.
A JFrame will stay resident until you either stop the JVM or close the window by calling the static JFrame dispose.
A JFrame will stay resident until you either stop the JVM or close the window by calling the static JFrame dispose.
ASKER
Good, thanks
THis works. The window stays alive, but the pink background is all covered by the blue textArea. The textArea cant seem to be resized. It explodes, no matter what. Also, pack() seemed to be responsible for shrinking the window. If I take it out, the window is permanent and the right size, but all blue.
?
public controlFrame() {
this.setSize(900, 600);
this.setBackground(Color.P INK);
textarea= new JTextArea();
textarea.setBackground(Col or.BLUE);
textarea.setSize(200, 140);
add(textarea);
textarea.setText("setText" );
this.setVisible(true);
}
THis works. The window stays alive, but the pink background is all covered by the blue textArea. The textArea cant seem to be resized. It explodes, no matter what. Also, pack() seemed to be responsible for shrinking the window. If I take it out, the window is permanent and the right size, but all blue.
?
public controlFrame() {
this.setSize(900, 600);
this.setBackground(Color.P
textarea= new JTextArea();
textarea.setBackground(Col
textarea.setSize(200, 140);
add(textarea);
textarea.setText("setText"
this.setVisible(true);
}
Also, pack() seemed to be responsible for shrinking the window.
That's because your complaint was the fact that nothing fitted.
Back to the main point :
import java.awt.Color;
import javax.swing.*;
import java.awt.BorderLayout;
public class controlFrame extends JFrame {
JTextArea textarea;
public controlFrame() {
this.setLayout(new BorderLayout());
this.setSize(700, 400);
// this.setBackground(Color.PINK);
this.getContentPane().setBackground(Color.PINK);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setVisible(true);
textarea = new JTextArea();
this.getContentPane().add(textarea,BorderLayout.NORTH);
textarea.setSize(500, 100);
// textarea.setLocation(100, 100);
textarea.setText("Text Area");
this.setVisible(true);
//textarea.setVisible(true);
}
public static void main(String[] args) {
controlFrame f = new controlFrame();
}
}
THis works. The window stays alive, but the pink background is all covered by the blue textArea. The textArea cant seem to be resized. It explodes, no matter what.Because the frame uses a BorderLayout by default. If there is one component added, it's added BorderLayout.CENTER. That sizes the component to the whole available window area. Calling setSize on the added component is ignored.
Also, pack() seemed to be responsible for shrinking the window.It sizes a window just large enough to accommodate the preferred size of its children, and no larger.
If I take it out, the window is permanent and the right size, but all blue.See first paragraph above
ASKER
So, if I'd like specific sizes, positioning for my textFields and labels for aesthetic needs, I can't place them at x,y, with xSize , ySize ?
I'm sure I could once design a frame precisely. Was that only AWT
?
Thanks
I'm sure I could once design a frame precisely. Was that only AWT
?
Thanks
You can use a GridBagLayout to obtain the finest control.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
It looks like (null) was the correct answer!
Thanks
Thanks
:)
http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html