Question

So, this is my problem: I used the oracle documents as an example for my project, but when i run the program all it shows up as is a box with "weekly pay:" in the middle. What am I doing wrong?

JFrame window = new JFrame();
window.setTitle("Weekly Pay");
window.setSize(300, 150);
window.setResizable(false);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Color lGray = new Color(209, 209, 209);
GroupLayout layout = new GroupLayout(window);

JPanel panel = new JPanel();
panel.setBackground(lGray);
panel.setLayout(layout);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);

JLabel id = new JLabel("ID Number");
JLabel hw = new JLabel("Hourly Wage");
JLabel rh = new JLabel("Regular Hours");
JLabel oh = new JLabel("Overtime Hours");
JButton calc = new JButton("Calculate");
JTextField idEntry = new JTextField(); //where the user imputs their ID
JTextField hwEntry = new JTextField(); //where the user imputs their hourly wage
JTextField rhEntry = new JTextField(); //where the user imputs their regular hours
JTextField ohEntry = new JTextField(); //where the user imputs their overtime hours
JLabel wp = new JLabel("Weekly Pay:");    

GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();    
hGroup.addGroup(layout.createParallelGroup().
  addComponent(id).addComponent(hw).addComponent(rh).addComponent(oh).addComponent(calc));
hGroup.addGroup(layout.createParallelGroup().
  addComponent(idEntry).addComponent(hwEntry).addComponent(rhEntry).addComponent(ohEntry).addComponent(wp));
layout.setHorizontalGroup(hGroup);

GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();    
vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
    addComponent(id).addComponent(idEntry));
vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
    addComponent(hw).addComponent(hwEntry));
vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
    addComponent(rh).addComponent(rhEntry));
vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
    addComponent(oh).addComponent(ohEntry));
vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
    addComponent(calc).addComponent(wp));
layout.setVerticalGroup(vGroup);

window.setVisible(true);
Was it helpful?

Solution

There are two main problems: you set the group layout manager on the frame, not the panel, and you don't actually put the panel onto the frame/window (since there is something in java UI programming called a window, I question the naming of the variable holding the frame to be 'window').

Instead of "GroupLayout layout = new GroupLayout(window);", use "GroupLayout layout = new GroupLayout(panel); // after panel is constructed, of course".

Then use "window.add(panel);" to put the panel in the frame/window; I would put this and "window.pack();" just before "window.setVisible(true);"

rc

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top