Results 1 to 7 of 7
Thread: frames and panels
- 03-21-2011, 11:15 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 1
- Rep Power
- 0
frames and panels
Can anybody figure out what is wrong with the following code. I don't see the labels and textfields I expected to see. Thank you.
import java.awt.*;
import javax.swing.*;
public class PracticeApp {
public static void main(String[] args)
{
JFrame SalesF = new JFrame(); //SalesFrame();
SalesF.setVisible(true);
}
}
class SalesFrame extends JFrame
{
public SalesFrame()
{
setTitle("Sales");
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.add(new SalesPanel());
this.pack();
centerWindow(this);
}
private void centerWindow(Window w)
{
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension d = tk.getScreenSize();
setLocation((d.width-w.getWidth())/2, (d.height-w.getHeight())/2);
}
}
class SalesPanel extends JPanel
{
JPanel sPanel = new JPanel();
private JLabel prompt1Label, prompt2Label; //prompt3Label, prompt4Label;
public JTextField prompt1TextField, prompt2TextField, prompt3TextField;
public SalesPanel()
{
// add panels
setLayout(new GridBagLayout());
prompt1Label = new JLabel("Enter ISBN");
add(prompt1Label, getConstraints(0,0,1,1, GridBagConstraints.EAST));
prompt1TextField = new JTextField(10);
prompt1TextField.setEditable(true);
prompt1TextField.setFocusable(false);
add(prompt1TextField, getConstraints(1,0,1,1, GridBagConstraints.WEST));
prompt2Label = new JLabel("Enter Title");
add(prompt2Label, getConstraints(0,1,1,1, GridBagConstraints.EAST));
prompt1TextField = new JTextField(10);
prompt1TextField.setEditable(false);
prompt1TextField.setFocusable(false);
add(prompt2TextField, getConstraints(1,1,1,1, GridBagConstraints.WEST));
prompt3TextField = new JTextField(10);
prompt3TextField.setEditable(false);
prompt3TextField.setFocusable(false);
add(prompt3TextField, getConstraints(1,2,2,4, GridBagConstraints.WEST));
}
private GridBagConstraints getConstraints(int gridx, int gridy,
int gridwidth, int gridheight, int anchor)
{
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(5, 5, 5, 5);
c.ipadx = 0;
c.ipady = 0;
c.gridx = gridx;
c.gridy = gridy;
c.gridwidth = gridwidth;
c.gridheight = gridheight;
c.anchor = anchor;
return c;
}
}
- 03-21-2011, 01:36 PM #2
Member
- Join Date
- Mar 2011
- Posts
- 5
- Rep Power
- 0
I'd appreciate if someone with some knowledge of JFrames could take a look at this, as I am also curious. I think a panel should be added to the frame in main, but I couldn't get it to work right. Also the sizing doesn't seem to be staying as specified. From my experience the frame usually wraps around content and makes itself as small as possible, but it seems like with specified size it should stay that way.
- 03-21-2011, 01:52 PM #3
Member
- Join Date
- Mar 2011
- Posts
- 94
- Rep Power
- 0
You never instantiated a SalesFrame object. This line simply creates the standard built-in JFrame:
You need to do this instead:Java Code:public static void main(String[] args) { [B]JFrame SalesF = new JFrame(); //SalesFrame();[/B] SalesF.setVisible(true); } }
Also comment out this line (it had an error that you need to fix):Java Code:SalesFrame SalesF = new SalesFrame(); SalesF.setVisible(true);
With those changes, I was able to run your project and see the dialog box with label, etc.Java Code:prompt1TextField = new JTextField(10); prompt1TextField.setEditable(false); prompt1TextField.setFocusable(false); [B]//add(prompt2TextField, getConstraints(1,1,1,1, GridBagConstraints.WEST));[/B] prompt3TextField = new JTextField(10); prompt3TextField.setEditable(false); prompt3TextField.setFocusable(false); add(prompt3TextField, getConstraints(1,2,2,4, GridBagConstraints.WEST));
- 03-21-2011, 01:58 PM #4
Use the code tags to maintain the readability of your code. And go through this link
SSCCE (Short, Self Contained, Compilable and Executable)
It will help you to get rid of extraneous code that has nothing at all to do with the stated problem (like setEditable and setFocusable calls).
db
- 03-21-2011, 02:06 PM #5
Member
- Join Date
- Mar 2011
- Posts
- 5
- Rep Power
- 0
FlipPoker, how were you able to find that the line:
"add(prompt2TextField, getConstraints(1,1,1,1, GridBagConstraints.WEST));"
was not needed?
I'm not sure what the line even does, but I have the code in Eclipse and it doesn't make a reference to an error in that class at all.
- 03-21-2011, 02:10 PM #6
Member
- Join Date
- Mar 2011
- Posts
- 94
- Rep Power
- 0
I'm not saying it's not needed. I just commented it out because it threw an exception. I was just trying to get your dialog to show up.
But now I see the problem. You never instantiated prompt2TextField. So that add() method threw a null pointer exception. I think you meant to do this (which does work now):
Java Code:[B]prompt2TextField [/B]= new JTextField(10); [B]prompt2TextField[/B].setEditable(false); [B]prompt2TextField[/B].setFocusable(false); add(prompt2TextField, getConstraints(1,1,1,1, GridBagConstraints.WEST));
- 03-26-2011, 04:01 AM #7
Member
- Join Date
- Mar 2011
- Posts
- 23
- Rep Power
- 0
Similar Threads
-
Need some help with panels inside panels
By kakefjes in forum AWT / SwingReplies: 0Last Post: 03-17-2011, 11:36 AM -
Java Frames
By Java Unknown in forum New To JavaReplies: 2Last Post: 02-22-2011, 05:45 AM -
jsp frames
By vasug in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 04-16-2010, 05:00 PM -
Switching Frames
By jonnytabpni in forum New To JavaReplies: 1Last Post: 11-08-2009, 10:12 PM -
Help regarding Frames
By ramesh.8189 in forum AWT / SwingReplies: 14Last Post: 02-15-2009, 08:12 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks