Note that there are some explanatory texts on larger screens.

plurals
  1. POneed assistance with GUIs and java swing
    text
    copied!<p>This is my program below and I am trying to figure out where my main method should be. I have seen a few examples of it being implemented at the very end of the program but there main is different from mine. </p> <p>Main method (to be implemented):</p> <pre><code> public class JFrame { public static void main(String[] args) { JFrame frame = new JFrame(); frame.setTitle("Components File"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } </code></pre> <p>My Program</p> <pre><code> import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class Lab_10 extends JFrame { private final double EARTHQUAKE_RATE= 8.0; private final int FRAME_WIDTH= 300; private final int FRAME_HEIGHT= 200; private JLabel rLabel; private JTextField eField; private JButton button; private JLabel earthLabel; public Lab_10() { JLabel earthLabel = new JLabel("Most structures fall"); makeTextField(); makeButton(); makePanel(); setSize(FRAME_WIDTH, FRAME_HEIGHT); } private void makeTextField() { JLabel rLabel = new JLabel("Richter"); final int FIELD_WIDTH = 10; eField = new JTextField(FIELD_WIDTH); eField.setText("" + EARTHQUAKE_RATE); } class AddLabelListener implements ActionListener { public void actionPerformed(ActionEvent event) { earthLabel.setText("Most structures fall"); } } private void makeButton() { JButton button = new JButton("Enter"); ActionListener listener = new AddLabelListener(); button.addActionListener(listener); } private void makePanel() { JPanel panel = new JPanel(); panel.add(rLabel); panel.add(eField); panel.add(button); panel.add(earthLabel); add(panel); } } </code></pre> <p>Updated Code (which is compiling but and running but with logic errors because it implements an empty frame [guess as much from the main method I have]):</p> <pre><code> import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class Lab_10 extends JFrame { private final double EARTHQUAKE_RATE= 8.0; private final int FRAME_WIDTH= 300; private final int FRAME_HEIGHT= 200; private JLabel rLabel; private JTextField eField; private JButton button; private JLabel earthLabel; public Lab_10() { JLabel earthLabel = new JLabel("Most structures fall"); makeTextField(); makeButton(); makePanel(); setSize(FRAME_WIDTH, FRAME_HEIGHT); } private void makeTextField() { JLabel rLabel = new JLabel("Richter"); final int FIELD_WIDTH = 10; eField = new JTextField(FIELD_WIDTH); eField.setText("" + EARTHQUAKE_RATE); } class AddLabelListener implements ActionListener { public void actionPerformed(ActionEvent event) { earthLabel.setText("Most structures fall"); } } private void makeButton() { JButton button = new JButton("Enter"); ActionListener listener = new AddLabelListener(); button.addActionListener(listener); } private void makePanel() { JPanel panel = new JPanel(); panel.add(rLabel); panel.add(eField); panel.add(button); panel.add(earthLabel); add(panel); } public static void main(String[] args){ javax.swing.SwingUtilities.invokeLater(new Runnable(){ @Override public void run() { JFrame frame = new JFrame(); frame.setTitle("Components File"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload