Here is a code I made to practice how to make JFrame and add components to it.
But the problem is:
1. On line 8, shouldn't the frame turn invisible?
2. On line 7, shouldn't the frame be in the center of the screen?
Could anyone help?
P.S : I did it in Ready to Program (if thats the source of any problem)
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class JButtonDemo2 { public static void main(String[] args) { mkay app = new mkay(); app.setLocationRelativeTo(null); app.setVisible(false); } } class mkay extends javax.swing.JFrame{ public mkay() { init(); } JFrame jtfMainFrame; JButton jbnButton1, jbnButton2; JTextField jtfInput; JPanel jplPanel; public void init() { jtfMainFrame = new JFrame("Which Button Demo"); jtfMainFrame.setSize(50, 50); jbnButton1 = new JButton("Button 1"); jbnButton2 = new JButton("Button 2"); jtfInput = new JTextField(20); jplPanel = new JPanel(); jbnButton1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { jtfInput.setText("Button 1!"); mkay app2 = new mkay(); app2.setLocationRelativeTo(null); } }); jbnButton2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { jtfInput.setText("Button 2!"); } }); jplPanel.setLayout(new FlowLayout()); jplPanel.add(jtfInput); jplPanel.add(jbnButton1); jplPanel.add(jbnButton2); jtfMainFrame.getContentPane().add(jplPanel, BorderLayout.CENTER); jtfMainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jtfMainFrame.pack(); jtfMainFrame.setVisible(true); } }