This is my first time using a JFrame. I can’t get the window to display the text areas I’ve nested inside the JFrame. I am trying to get the text field with my name in it to display above the tabulated results, which I have omitted the formatting for until I can get the JFrame to work.
public void printResults(String[] names, int[] temp, int[][] scores, float[] averages, char[] letters){
JTextArea outarea= new JTextArea(5,20);
JTextArea name = new JTextArea(5,20);
Font font = new Font("Tahoma", Font.BOLD, 48);
name.setFont(font);
name.setText("Made By Durka Durka");
JFrame window = new JFrame();
window.getContentPane().add(name);
window.getContentPane().add(outarea);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.pack();
window.getContentPane().setVisible(true)
String out = "foo";
outarea.setText(out);
//JOptionPane.showMessageDialog(null,window);
}
The probable reason why the
JFrameis not appearing is because of this line:The above line is setting the visibility of the
Containerto which theJTextAreas have been added, but does not control the visibility of theJFrameitself — therefore, theJFrameitself is not being displayed.(To be more precise, the
JFrame.getContentPanemethod returns aContainer, so the above code is actually calling theContainter‘ssetVisiblemethod.)Try the following instead:
This will set the visibility of the
JFrameitself to be visible.Also, as the other answers have suggested, try using Layout Managers to control the locations of where the components should be displayed. The following are links to using two of the several Layout Managers which could be used to layout components.