Results 1 to 6 of 6
- 07-25-2011, 03:37 PM #1
Member
- Join Date
- Jul 2011
- Posts
- 76
- Rep Power
- 0
Components not displaying on JFrame
my menu bar displays but my jpanel and the components in it are not displaying when i first compile the program. I have to compile it mutiple times to get it to display. i am using the setBounds method
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class IncomeTax extends JFrame {
//Instance Variable Declaration
private JMenuItem exit;
private JMenuItem help;
private JMenuItem about;
private JTextField name;
private JTextField salary;
private JTextField numOfChildren;
private JComboBox comBox;
private JButton submit;
//Constructor that initializes JFrame
public IncomeTax ()
{
setTitle ("Income Tax Application");
setSize (400,300);
setVisible (true);
setDefaultCloseOperation (EXIT_ON_CLOSE);
setResizable (false);
setLocation(300,200);
setMenuBar ();
setContent ();
}//end constructor
//Sets MenuBar on JFrame
public void setMenuBar ()
{
JMenuBar mBar = new JMenuBar ();
JMenu file = new JMenu ("File ");
JMenu option = new JMenu ("Options");
setJMenuBar(mBar);
mBar.add(file);
mBar.add(option);
file.add(exit = new JMenuItem ("Exit"));
option.add(help = new JMenuItem ("Help"));
option.add(about = new JMenuItem ("About"));
help.setFont(new Font("Segoe UI",Font.PLAIN,12));
about.setFont(new Font("Segoe UI",Font.PLAIN,12));
exit.setFont (new Font("Segoe UI",Font.PLAIN,12));
file.setFont(new Font("Segoe UI",Font.PLAIN,12));
option.setFont(new Font("Segoe UI",Font.PLAIN,12));
EventHandler handlerObj = new EventHandler ();
help.addActionListener (handlerObj);
about.addActionListener (handlerObj);
exit.addActionListener (handlerObj);
}//end setMenuBar
public void setContent ()
{
String[] stat ={"Married","Single"};
name = new JTextField (12);
salary = new JTextField (7);
numOfChildren = new JTextField (2);
submit = new JButton ("Submit");
JLabel label1 = new JLabel ("Name: ");
JLabel label2 = new JLabel ("Gross Salary: ");
JLabel label3 = new JLabel ("# of Children: ");
label1.setFont(new Font("Segoe UI",Font.PLAIN,12));
label2.setFont(new Font("Segoe UI",Font.PLAIN,12));
label3.setFont(new Font("Segoe UI",Font.PLAIN,12));
Dimension label1Size = label1.getPreferredSize();
Dimension label2Size = label2.getPreferredSize();
Dimension label3Size = label3.getPreferredSize ();
Dimension nameSize = name.getPreferredSize ();
Dimension salarySize = salary.getPreferredSize ();
Dimension childSize = numOfChildren.getPreferredSize ();
Dimension btSize = submit.getPreferredSize ();
label1.setBounds (20,20,label1Size.width,label1Size.height);
label2.setBounds (20,45,label2Size.width,label2Size.height);
label3.setBounds (20,70,label3Size.width,label3Size.height);
name.setBounds(70,20,nameSize.width,nameSize.heigh t);
salary.setBounds(90,45,salarySize.width,salarySize .height);
numOfChildren.setBounds (100,70,childSize.width,childSize.height);
submit.setBounds (20,110,btSize.width,btSize.height);
JPanel panel = new JPanel ();
panel.setBounds(50,20,270,170);
panel.setBorder(BorderFactory.createTitledBorder(" Info"));
comBox = new JComboBox (stat);
comBox.setBounds (160,70,75,30);
comBox.setEditable(false);
comBox.setSelectedItem("Single");
setLayout(null);
add(panel);
panel.setLayout(null);
panel.add(label1);
panel.add(name);
panel.add(label2);
panel.add(salary);
panel.add(label3);
panel.add(numOfChildren);
panel.add(comBox);
panel.add(submit);
EventHandler handlerObj = new EventHandler ();
submit.addActionListener(handlerObj);
}//end setContent
public static void main (String [] args)
{
IncomeTax window = new IncomeTax ();
}//end main
private class EventHandler implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
if (e.getSource () == help || e.getSource () == about || e.getSource () == exit || e.getSource() == submit )
{
JOptionPane.showMessageDialog (null,"Not Coded");
}
}//end actionPerformed
}//end EventHandler
}//end IncomeTax class
- 07-25-2011, 04:06 PM #2
I'm pretty sure you have to make it visible at the very end, after adding all of the components.
- Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
- +Rep people for helpful posts.
- 07-25-2011, 04:45 PM #3
There shouldn't be any difference between the output from the first compile and the output from the last compile, UNLESS you change the source between each compile. A compiler reads the source file and creates a .class file.I have to compile it mutiple times to get it to display.
You do not get any displays in that process, unless you have errors in the source.
- 07-27-2011, 05:18 PM #4
Put setVisible(true) at the very end of the code body you used to write the class that returns the frame. So a rough pseudo-code would be:
Your compiler by the way shouldn't change anything unless you made a change.Java Code:pulblic SomeFrame() { JFrame jf = new JFrame(); JPanel jp = new Jpanel(); JLabel someLabel = new JLabel("Label"); jp.add(someLabel); jf.add(jp); //this would be the last line of the code; there would be additional code for you to fill in above. jf.setVisible(true); }
- 07-27-2011, 07:10 PM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Even better, move the code that makes the gui visible right out of the constructor and call it on the event dispatch thread. There are numerous examples throughout Oracle's Tutorial.
- 07-27-2011, 07:48 PM #6
I suggested making it visible at the end of everything. No response still...
pbrockway2 is right though, if the OP ever returns to this thread the OP should indeed listen to this advice.- Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
- +Rep people for helpful posts.
Similar Threads
-
Access Child Components of JFrame?
By PrinceSendai in forum New To JavaReplies: 5Last Post: 06-22-2011, 03:18 PM -
Get more components from JFrame Form.
By ZeCute in forum AWT / SwingReplies: 0Last Post: 05-09-2011, 10:46 AM -
JLayeredPane not displaying components
By abc in forum AWT / SwingReplies: 3Last Post: 01-06-2011, 03:40 PM -
Getting and Displaying Swing Components From DataBase
By vivekkumar in forum AWT / SwingReplies: 1Last Post: 07-27-2010, 03:32 PM -
Displaying Chinese characters on Swing components
By vaskarbasak in forum AWT / SwingReplies: 3Last Post: 06-26-2008, 08:27 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks