#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    Frame won't open?


    I am new and very uneducated as far as Java goes, but I wanted to try it. My teacher gave me the option to program my project, and I accepted. I typed up this code to make a frame full of buttons that when clicked on would give information. However, now whenever I run it, there are no errors, but it terminates immediately.

    What's happening?

    Here is the code:



    import java.awt.ComponentOrientation;
    import java.awt.Dimension;
    import java.awt.GridLayout;
    import java.awt.Window;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.*;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;

    public abstract class Frame extends JFrame implements ActionListener {

    public void frame(){

    final JFrame frame = new JFrame();

    frame.setSize(1000,700);

    frame.setVisible(true);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.setLocationRelativeTo(null);

    frame.setTitle("Eukariotic Cell Information");




    JLabel label = new JLabel("Click on what you would like to learn about.");



    final JPanel panel = new JPanel();

    panel.setLayout(new GridLayout(3,4,50,50));




    frame.add(panel);

    panel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);


    JButton cellmembrane = new JButton("Cell Membrane");

    cellmembrane.addActionListener(this);

    new ActionListener() {
    public void actionPerformed(ActionEvent e) {

    frame.remove(panel);
    JPanel panel2 = new JPanel();

    JLabel cellmembraneinfo = new JLabel("Info on Cell membrane");

    panel2.add(cellmembraneinfo);

    frame.add(panel2);

    }
    };





    JButton mitochondrion = new JButton("Mitochondrion");

    mitochondrion.addActionListener(this);

    new ActionListener() {
    public void actionPerformed(ActionEvent e) {

    frame.remove(panel);
    JPanel panel3 = new JPanel();

    JLabel mitochondrioninfo = new JLabel("Info on Mitochondrion");

    panel3.add(mitochondrioninfo);

    frame.add(panel3);

    }
    };




    JButton ribosomes = new JButton("Ribosomes");

    ribosomes.addActionListener(this);


    new ActionListener() {
    public void actionPerformed(ActionEvent e) {

    frame.remove(panel);
    JPanel panel4 = new JPanel();

    JLabel ribosomesinfo = new JLabel("Info on Ribosomes");

    panel4.add(ribosomesinfo);

    frame.add(panel4);

    }
    };



    JButton roughER = new JButton("Rough ER");

    roughER.addActionListener(this);

    new ActionListener() {
    public void actionPerformed(ActionEvent e) {

    frame.remove(panel);
    JPanel panel5 = new JPanel();

    JLabel roughERinfo = new JLabel("Info on Rough ER");

    panel5.add(roughERinfo);

    frame.add(panel5);

    }
    };

    JButton smoothER = new JButton("Smooth ER");

    smoothER.addActionListener(this);

    new ActionListener() {
    public void actionPerformed(ActionEvent e) {

    frame.remove(panel);
    JPanel panel6 = new JPanel();

    JLabel smoothERinfo = new JLabel("Info on Smooth ER");

    panel6.add(smoothERinfo);

    frame.add(panel6);

    }
    };



    JButton golgiapparatus = new JButton("Golgi Apparatus");

    golgiapparatus.addActionListener(this);

    new ActionListener() {
    public void actionPerformed(ActionEvent e) {

    frame.remove(panel);
    JPanel panel7 = new JPanel();

    JLabel golgiapparatusinfo = new JLabel("Info on Golgi Apparatus");

    panel7.add(golgiapparatusinfo);

    frame.add(panel7);

    }
    };

    JButton lysosomes = new JButton("Lysosomes");

    lysosomes.addActionListener(this);

    new ActionListener() {
    public void actionPerformed(ActionEvent e) {

    frame.remove(panel);
    JPanel panel8 = new JPanel();

    JLabel lysosomesinfo = new JLabel("Info on Lysosomes");

    panel8.add(lysosomesinfo);

    frame.add(panel8);

    }
    };


    JButton cytoskeleton = new JButton("Cytoskeleton");

    cytoskeleton.addActionListener(this);

    new ActionListener() {
    public void actionPerformed(ActionEvent e) {

    frame.remove(panel);
    JPanel panel9 = new JPanel();

    JLabel cytoskeletoninfo = new JLabel("Info on Cytoskeleton");

    panel9.add(cytoskeletoninfo);

    frame.add(panel9);

    }
    };


    JButton nucleus = new JButton("Nucleus");

    nucleus.addActionListener(this);


    new ActionListener() {
    public void actionPerformed(ActionEvent e) {

    frame.remove(panel);
    JPanel panel10 = new JPanel();

    JLabel nucleusinfo = new JLabel("Info on Nucleus");

    panel10.add(nucleusinfo);

    frame.add(panel10);

    }
    };

    JButton cellwall = new JButton("Cell Wall");

    cellwall.addActionListener(this);

    new ActionListener() {
    public void actionPerformed(ActionEvent e) {

    frame.remove(panel);
    JPanel panel11 = new JPanel();

    JLabel cellwallinfo = new JLabel("Info on Cell Wall");

    panel11.add(cellwallinfo);

    frame.add(panel11);

    }
    };


    JButton vacuole = new JButton("Vacuole");

    vacuole.addActionListener(this);

    new ActionListener() {
    public void actionPerformed(ActionEvent e) {

    frame.remove(panel);
    JPanel panel12 = new JPanel();

    JLabel vacuoleinfo = new JLabel("Info on Vacuole");

    panel12.add(vacuoleinfo);

    frame.add(panel12);

    }
    };


    JButton plastids = new JButton("Plastids");

    plastids.addActionListener(this);

    new ActionListener() {
    public void actionPerformed(ActionEvent e) {

    frame.remove(panel);
    JPanel panel13 = new JPanel();

    JLabel plastidsinfo = new JLabel("Info on Plastids");

    panel13.add(plastidsinfo);

    frame.add(panel13);

    }
    };

    JButton chloroplasts = new JButton("Chloroplasts");

    chloroplasts.addActionListener(this);

    new ActionListener() {
    public void actionPerformed(ActionEvent e) {

    frame.remove(panel);
    JPanel panel14 = new JPanel();

    JLabel chloroplastsinfo = new JLabel("Info on Chloroplasts");

    panel14.add(chloroplastsinfo);

    frame.add(panel14);

    }
    };

    JButton nucleolus = new JButton("Nucleolus");

    nucleolus.addActionListener(this);

    new ActionListener() {
    public void actionPerformed(ActionEvent e) {

    frame.remove(panel);
    JPanel panel15 = new JPanel();

    JLabel nucleolusinfo = new JLabel("Info on Nucleolus");

    panel15.add(nucleolusinfo);

    frame.add(panel15);

    }
    };

    JButton nuclearmembrane = new JButton("Nuclear Membrane");

    nuclearmembrane.addActionListener(this);

    new ActionListener() {
    public void actionPerformed(ActionEvent e) {

    frame.remove(panel);
    JPanel panel16 = new JPanel();

    JLabel nuclearmembraneinfo = new JLabel("Info on Nuclear Membrane");

    panel16.add(nuclearmembraneinfo);

    frame.add(panel16);

    }
    };


    panel.add(label);

    panel.add(cellmembrane);

    panel.add(mitochondrion);

    panel.add(ribosomes);

    panel.add(roughER);

    panel.add(smoothER);

    panel.add(golgiapparatus);

    panel.add(lysosomes);

    panel.add(cytoskeleton);

    panel.add(nucleus);

    panel.add(cellwall);

    panel.add(vacuole);

    panel.add(plastids);

    panel.add(chloroplasts);




    }






    public static void main(String[]args){









    }










    }
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2010
    Posts
    93
    Rep Power
    25
    I'm no GUI expert, but one problem I can see right away is that your main method is empty. When you tell Java to run, its going to start by looking at your main method and execute what is inside. I would imagine that if you want the frame to show up you would need to instantiate it and setVisible to true.

    I think that googling a few simple AWT examples / sample code would get you what you need.

    Edit:
    Also, in the future please consider using CODE tags (described in the sticky here: http://forums.devshed.com/java-help-9/all-users--how-to-post-a-question-147056.html). They will make your code a lot more readable and that makes it easier for others to help you.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0
    Originally Posted by jasondj
    I'm no GUI expert, but one problem I can see right away is that your main method is empty. When you tell Java to run, its going to start by looking at your main method and execute what is inside. I would imagine that if you want the frame to show up you would need to instantiate it and setVisible to true.

    I think that googling a few simple AWT examples / sample code would get you what you need.

    Edit:
    Also, in the future please consider using CODE tags (described in the sticky here: . They will make your code a lot more readable and that makes it easier for others to help you.


    Thank you for responding. So how would I go about instantiating it in my main method? It is static, so the (this) in the action listeners won't function if I just drag the whole thing in. Should I just re-declare it? Sorry if these are really stupid questions, but this is the first program I have ever really made in Java, so I don't quite know how to make things work yet.

    Thanks.

IMN logo majestic logo threadwatch logo seochat tools logo