-2

My JFrame with a null layout wont display anything.... I tried adding the components to a JPanel and then set that as the frame's content pane. More recently (as seen in the code below) I tried adding directly to the frame. I am using Java 8.

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Electog extends JFrame implements MouseListener,      MouseMotionListener {
     BlankArea blankArea;
     JTextArea textArea;
static JFrame frame;

public static void main(String[] args) {
    JFrame frame = new Electog();
    frame.setTitle("Electog"); 
    frame.setSize(Toolkit.getDefaultToolkit().getScreenSize());
    frame.setVisible(true);
}

public Electog() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(null);

    JRadioButton selectButton = new JRadioButton("Select");
    selectButton.addKeyListener(new KeyListener() {
        @Override
        public void keyReleased(KeyEvent e) {
            handleAction(e);
        }

        @Override
        public void keyTyped(KeyEvent e) {

        }

        @Override 
        public void keyPressed(KeyEvent e) {
            handleAction(e);
        }
    });
    selectButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            handleAction(e);
        }

        public void actionEvent(ActionEvent e) {
            handleAction(e);
        }
    });
    selectButton.setSelected(true);

    JRadioButton removeButton = new JRadioButton("Remove");
    removeButton.addKeyListener(new KeyListener() {
        @Override
        public void keyReleased(KeyEvent e) {
            handleAction(e);
        }

        @Override
        public void keyTyped(KeyEvent e) {

        }

        @Override 
        public void keyPressed(KeyEvent e) {
            handleAction(e);
        }
    });
    removeButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            handleAction(e);
        }

        public void actionEvent(ActionEvent e) {
            handleAction(e);
        }
    });

    JRadioButton openButton = new JRadioButton("Open");
    openButton.addKeyListener(new KeyListener() {
        @Override
        public void keyTyped(KeyEvent e) {

        }

        @Override
        public void keyReleased(KeyEvent e) {
            handleAction(e);
        }

        @Override 
        public void keyPressed(KeyEvent e) {
            handleAction(e);
        }
    });
    openButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            handleAction(e);
        }

        public void actionEvent(ActionEvent e) {
            handleAction(e);
        }
    });


    JRadioButton saveButton = new JRadioButton("Save");
    saveButton.addKeyListener(new KeyListener() {
        @Override
        public void keyReleased(KeyEvent e) {
            handleAction(e);
        }

        @Override
        public void keyTyped(KeyEvent e) {

        }

        @Override 
        public void keyPressed(KeyEvent e) {
            handleAction(e);
        }
    });
    saveButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            handleAction(e);
        }

        public void actionEvent(ActionEvent e) {
            handleAction(e);
        }
    });

    JRadioButton wireButton = new JRadioButton("Wiring");
    wireButton.setMnemonic(KeyEvent.VK_W);
    wireButton.addKeyListener(new KeyListener() {
        @Override
        public void keyReleased(KeyEvent e) {
            handleAction(e);
        }

        @Override
        public void keyTyped(KeyEvent e) {

        }

        @Override 
        public void keyPressed(KeyEvent e) {
            handleAction(e);
        }
    });
    wireButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            handleAction(e);
        }

        public void actionEvent(ActionEvent e) {
            handleAction(e);
        }
    });

    JRadioButton subcircuitButton = new JRadioButton("Subcircuit");
    subcircuitButton.setMnemonic(KeyEvent.VK_C);
    subcircuitButton.addKeyListener(new KeyListener() {
        @Override
        public void keyReleased(KeyEvent e) {
            handleAction(e);
        }

        @Override
        public void keyTyped(KeyEvent e) {

        }

        @Override 
        public void keyPressed(KeyEvent e) {
            handleAction(e);
        }
    });
    subcircuitButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            handleAction(e);
        }

        public void actionEvent(ActionEvent e) {
            handleAction(e);
        }
    });

    JRadioButton ANDButton = new JRadioButton("AND Gate");
    ANDButton.setMnemonic(KeyEvent.VK_A);
    ANDButton.addKeyListener(new KeyListener() {
        @Override
        public void keyReleased(KeyEvent e) {
            handleAction(e);
        }

        @Override
        public void keyTyped(KeyEvent e) {

        }

        @Override 
        public void keyPressed(KeyEvent e) {
            handleAction(e);
        }
    });
    ANDButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            handleAction(e);
        }

        public void actionEvent(ActionEvent e) {
            handleAction(e);
        }
    });

    JRadioButton NANDButton = new JRadioButton("NAND Gate");
    NANDButton.setMnemonic(KeyEvent.VK_N);
    NANDButton.addKeyListener(new KeyListener() {
        @Override
        public void keyReleased(KeyEvent e) {
            handleAction(e);
        }

        @Override
        public void keyTyped(KeyEvent e) {

        }

        @Override 
        public void keyPressed(KeyEvent e) {
            handleAction(e);
        }
    });
    NANDButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            handleAction(e);
        }

        public void actionEvent(ActionEvent e) {
            handleAction(e);
        }
    });

    JRadioButton ORButton = new JRadioButton("OR Gate");
    ORButton.addKeyListener(new KeyListener() {
        @Override
        public void keyReleased(KeyEvent e) {
            handleAction(e);
        }

        @Override
        public void keyTyped(KeyEvent e) {

        }

        @Override 
        public void keyPressed(KeyEvent e) {
            handleAction(e);
        }
    });
    ORButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            handleAction(e);
        }

        public void actionEvent(ActionEvent e) {
            handleAction(e);
        }
    });

    JRadioButton groundButton = new JRadioButton("Ground");
    groundButton.addKeyListener(new KeyListener() {
        @Override
        public void keyReleased(KeyEvent e) {
            handleAction(e);
        }

        @Override
        public void keyTyped(KeyEvent e) {

        }

        @Override 
        public void keyPressed(KeyEvent e) {
            handleAction(e);
        }
    });
    groundButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            handleAction(e);
        }

        public void actionEvent(ActionEvent e) {
            handleAction(e);
        }
    });

    JRadioButton powerButton = new JRadioButton("Power");
    powerButton.addKeyListener(new KeyListener() {
        @Override
        public void keyReleased(KeyEvent e) {
            handleAction(e);
        }

        @Override
        public void keyTyped(KeyEvent e) {

        }

        @Override 
        public void keyPressed(KeyEvent e) {
            handleAction(e);
        }
    });
    powerButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            handleAction(e);
        }

        public void actionEvent(ActionEvent e) {
            handleAction(e);
        }
    });

    ButtonGroup group = new ButtonGroup();
    group.add(selectButton);
    group.add(removeButton);
    group.add(openButton);
    group.add(saveButton);
    group.add(wireButton);
    group.add(subcircuitButton);
    group.add(ANDButton);
    group.add(NANDButton);
    group.add(ORButton);
    group.add(groundButton);
    group.add(powerButton);

    //menu buttons, still need more features and zoom/move simulation screen
    add(selectButton);
    add(removeButton);
    add(openButton);
    add(saveButton);
    add(wireButton);
    add(subcircuitButton);
    add(ANDButton);
    add(NANDButton);
    add(ORButton);
    add(groundButton);
    add(powerButton);

    selectButton.setBounds((int)(Toolkit.getDefaultToolkit().getScreenSize().getWidth() * (1.0/11.0)), 50, 0, 0); //11 is number of buttons as far as now
    removeButton.setBounds((int)(Toolkit.getDefaultToolkit().getScreenSize().getWidth() * (1.0/11.0)), 50, (int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth() * (1.0/11.0)), 0);
    openButton.setBounds((int)(Toolkit.getDefaultToolkit().getScreenSize().getWidth() * (1.0/11.0)), 50, (int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth() * (2.0/11.0)), 0);
    saveButton.setBounds((int)(Toolkit.getDefaultToolkit().getScreenSize().getWidth() * (1.0/11.0)), 50, (int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth() * (3.0/11.0)), 0);
    wireButton.setBounds((int)(Toolkit.getDefaultToolkit().getScreenSize().getWidth() * (1.0/11.0)), 50, (int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth() * (4.0/11.0)), 0);
    subcircuitButton.setBounds((int)(Toolkit.getDefaultToolkit().getScreenSize().getWidth() * (1.0/11.0)), 50, (int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth() * (5.0/11.0)), 0);
    ANDButton.setBounds((int)(Toolkit.getDefaultToolkit().getScreenSize().getWidth() * (1/11)), 50, (int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth() * (6.0/11.0)), 0);
    NANDButton.setBounds((int)(Toolkit.getDefaultToolkit().getScreenSize().getWidth() * (1.0/11.0)), 50, (int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth() * (7.0/11.0)), 0);
    ORButton.setBounds((int)(Toolkit.getDefaultToolkit().getScreenSize().getWidth() * (1.0/11.0)), 50, (int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth() * (8.0/11.0)), 0);
    groundButton.setBounds((int)(Toolkit.getDefaultToolkit().getScreenSize().getWidth() * (1.0/11.0)), 50, (int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth() * (9.0/11.0)), 0);
    powerButton.setBounds((int)(Toolkit.getDefaultToolkit().getScreenSize().getWidth() * (1.0/11.0)), 50, (int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth() * (10.0/11.0)), 0);

    //subcircuit buttons
    JScrollPane ScrollPane = new JScrollPane();
    JPanel buttonPanel = new JPanel();
    buttonPanel.setLayout(null);
    buttonPanel.setSize(new Dimension((int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth() * (1/5)), (int) (Toolkit.getDefaultToolkit().getScreenSize().getHeight() - 50)));
    ScrollPane.setViewportView(buttonPanel);
    add(ScrollPane);
    ScrollPane.setBounds((int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth() * (1/5)), (int) (Toolkit.getDefaultToolkit().getScreenSize().getHeight() - 50), 0, 50);
    ScrollPane.updateUI();
    blankArea = new BlankArea(Color.WHITE);
    add(blankArea);
    blankArea.addMouseListener(this);
    blankArea.addMouseMotionListener(this);
    blankArea.setBounds((int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth() * (4/5)), (int) (Toolkit.getDefaultToolkit().getScreenSize().getHeight() - 50), (int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth() * (1/5)), 50);
    addMouseListener(this);
    addMouseMotionListener(this);
    setPreferredSize(new Dimension((int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth() * (4/5)),(int) (Toolkit.getDefaultToolkit().getScreenSize().getHeight() - 50)));
}
public void handleAction(KeyEvent e) {
    if ((e.getModifiers() & KeyEvent.CTRL_MASK) != 0 && (e.getKeyChar() == 'S' || e.getKeyChar() == 's')) {
        try {
            //saveCurrentFile();
        } catch(Exception ex) {
            JOptionPane.showMessageDialog(frame, ex.getStackTrace());
        }
    } else if ((e.getModifiers() & KeyEvent.CTRL_MASK) != 0 && (e.getKeyChar() == 'O' || e.getKeyChar() == 'o')) {
        try {
            //openFile();
        } catch(Exception ex) {
            JOptionPane.showMessageDialog(frame, ex.getStackTrace());
        }
    } else if ((e.getModifiers() & KeyEvent.CTRL_MASK) == 0 && (e.getKeyChar() == 'A' || e.getKeyChar() == 'a')) {
        //selectAll();
    } else if ((e.getModifiers() & KeyEvent.CTRL_MASK) == 0 && (e.getKeyChar() == 'A' || e.getKeyChar() == 'a')) {  

    } else if ((e.getModifiers() & KeyEvent.CTRL_MASK) == 0 && (e.getKeyChar() == 'N' || e.getKeyChar() == 'n')) {  

    } else if ((e.getModifiers() & KeyEvent.CTRL_MASK) == 0 && (e.getKeyChar() == 'O' || e.getKeyChar() == 'o')) {  

    }

}

public void handleAction(ActionEvent e) {
    if (e.getActionCommand() == "Save") {
        try {
            //saveCurrentFile();
        } catch(Exception ex) {
            JOptionPane.showMessageDialog(frame, ex.getStackTrace());
        }
    } else if (e.getActionCommand() == "Open") {
        try {
            //openFile();
        } catch(Exception ex) {
            JOptionPane.showMessageDialog(frame, ex.getStackTrace());
        }
    } else if (e.getActionCommand() == "AND Gate") {    

    } else if (e.getActionCommand() == "NAND Gate") {   

    } else if (e.getActionCommand() == "OR Gate") { 

    } else if (e.getActionCommand() == "Select") {  

    } else if (e.getActionCommand() == "Ground") {  

    } else if (e.getActionCommand() == "Power") {   

    } else if (e.getActionCommand() == "Remove") {  

    } else if (e.getActionCommand() == "Subcircuit") {  

    } else if (e.getActionCommand() == "Wiring") {  

    }
}

public void mousePressed(MouseEvent e) {

}

public void mouseReleased(MouseEvent e) {

}

public void mouseEntered(MouseEvent e) {

}

public void mouseExited(MouseEvent e) {

}

public void mouseClicked(MouseEvent e) {

}

public void mouseMoved(MouseEvent e) {

}

public void mouseDragged(MouseEvent e) {

}
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 8
    Step 1: **DO NOT** use a null layout. – gla3dr Sep 17 '15 at 16:47
  • 2
    You're setting the height for every button to 0 and the width of the scrollpane and also the `selectButton` to 0. – Lukas Rotter Sep 17 '15 at 16:50
  • possible duplicate of [Using a JPanel with a null layout](http://stackoverflow.com/questions/14982781/using-a-jpanel-with-a-null-layout) – Eric Leibenguth Sep 17 '15 at 16:52
  • Thanks LuxxMiner... that worked, didn't realize it was x,y,width,height (thought it was width, height, x, y) – Electog Sep 17 '15 at 16:56
  • 4
    I agree with @gla3dr, that you're fooling yourself by thinking that a `null` layout is the easiest way to create complex GUI's. The problem with them though is that their use leads to rigid GUI's that are very difficult to enhance or debug, GUI's that look bad on most all platforms except maybe one. The better long-term solution is to study and use the layout managers, learning to nest JPanels, each using its own layout managers. – Hovercraft Full Of Eels Sep 17 '15 at 17:04
  • I dont think it's easiest.... I think its simplest for now. The BlankArea (which extends JPanel) and ScrollPane still dont show up.I fixed the (x,y), (width, height) switch – Electog Sep 17 '15 at 17:07
  • 1
    @Electog I really don't think a bunch of coordinates are simpler (to understand) than layouts (also certainly not better)... Could you please post a pic of the intended look (as an edit) so we can give you suggestions on which layout managers you could use? – Lukas Rotter Sep 17 '15 at 17:11
  • 3
    `"I think its simplest for now"` -- but this will delay your learning to use the layout managers, something you shouldn't do. Be brave -- take the plunge -- learn the layouts. – Hovercraft Full Of Eels Sep 17 '15 at 17:18
  • Will yall just criticize me or will you help me with my null Layout? – Electog Sep 17 '15 at 21:42
  • 3
    *"Will yall just criticize me"* People are not criticizing you, just the code approach. Don't confuse yourself with your code. *"or will you help me with my null Layout?"* If by 'help' you mean guide you to a solution that won't result in 3-4 more *'now how do I fix...?'* style questions, yes. But to do that we'd use layouts and you seem to be resisting even giving us enough information to help you there. – Andrew Thompson Sep 18 '15 at 00:08

1 Answers1

0

Each java.​awt.​Component has a setBounds(int x, int y, int width, int height) method used to determine its location in its container. It only works with null-layout container.

gudok
  • 4,029
  • 2
  • 20
  • 30
anas
  • 1