1) You create 2 JFrame instances for the first of the JFrame extensions for the class, and the second of the JFrame window=.. You, instead of saying setVisible(..) on window and in your main part you are trying to call setVisible(...) on dogYears .
Decision
You must create only one JFrame for each Swing GUI. Get extends JFrame (and the code that comes with it) as its not a good practice to extend JFrame in Swing. This way, of course, you cannot call setVisible(true) in your constructor, which calls it great after creating the GUI in the constructor, or creates a method like setVisible in a GUI class that will be a wrapper for your JFrame setVisble(boolean b) .
Other offers
Always create Swing components on the Event Dispatch Thread , which you must do with the SwingUtilitites.invokeLater(Runnable r) block. Read Concurrency in Swing .
Remember to make the package visible before installing the JFrame and after adding the components.
setLocationRelativeTo(..) should appear after pack() .
It is better to use JFrame.DISPOSE_ON_CLOSE if you do not use timers.
Also, there is no need for setContentPane just call add(..) on the JFrame instance.
Here is your code with the above fixes:

import java.awt.FlowLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.SwingUtilities; public class GUI { private JTextField humanYears_TextField = new JTextField(3); private JTextField dogYears_TextField = new JTextField(3); private JButton convert_Button = new JButton("Convert"); private JLabel greeting = new JLabel("Dog years to Human years!"); private JFrame window = new JFrame(); private JPanel content = new JPanel(); public GUI() { content.setLayout(new FlowLayout()); content.add(this.greeting); content.add(new JLabel("Dog Years: ")); content.add(this.dogYears_TextField); content.add(this.convert_Button); content.add(new JLabel("Human Years: ")); content.add(this.humanYears_TextField); window.add(content); window.setTitle("Dog Year Converter"); window.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); window.pack(); window.setLocationRelativeTo(null);
source share