Results 1 to 4 of 4
  1. #1
    swedishfished is offline Member
    Join Date
    Sep 2012
    Posts
    24
    Rep Power
    0

    Default Problem with JButton--Takes up entire screen

    Hi. Here is the code: It is a calculator. When I run it, the JButton (submit) takes up the entire screen.
    import java.awt.FlowLayout;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JTextField;
    import javax.swing.JOptionPane;
    import javax.swing.JComboBox;

    public class aosd extends JFrame {

    String[] operations = {
    "divided by", "multiplied by", "added to", "minus"
    };
    private JButton submit;
    private JTextField fnum;
    private JTextField snum;
    private JComboBox op;
    int fnum1;
    int snum1;
    int cow;
    public aosd(){
    submit = new JButton("Calculate");
    fnum = new JTextField(10);
    snum = new JTextField(10);
    op = new JComboBox(operations);
    submit.setSize(1,11);
    add(fnum);
    add(snum);
    add(op);
    add(submit);
    handler handObj = new handler();
    submit.addActionListener(handObj);
    }
    private class handler implements ActionListener{
    public void actionPerformed(ActionEvent event){
    int sum;
    if(event.getSource()==submit){
    getOp();
    doOp();
    }
    JOptionPane.showMessageDialog(null, fnum1);
    }
    }
    public void getOp(){
    Object operation1 = op.getSelectedItem();
    String operation2 = operation1.toString();
    for(int i=0; i<4; i++){
    if(operation2==operations[i]){
    cow = i;
    }
    }
    }

    public void doOp(){
    String text = fnum.getText();
    fnum1 = Integer.parseInt(text);
    String text2 = fnum.getText();
    snum1 = Integer.parseInt(text);
    fnum1 = fnum1/snum1;
    switch(cow){
    case 0:
    fnum1 = fnum1/snum1;
    case 1:
    fnum1 = fnum1*snum1;
    case 2:
    fnum1 = fnum1+snum1;
    case 3:
    fnum1 = fnum1-snum1;
    }
    }







    }

    Second file:
    import javax.swing.JFrame;
    class calc {
    public static void main(String[] args){
    aosd object = new aosd();
    object.setDefaultCloseOperation(JFrame.EXIT_ON_CLO SE);
    object.setSize(2000,2000);
    object.setVisible(true);
    }
    }

  2. #2
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: Problem with JButton--Takes up entire screen

    What you want to do is instead of adding them straight to the frame add them to a jpanel:

    Java Code:
    //other code in your class
    
    JPanel panel = new JPanel((new GridLayout(1,4)); // this means the panel will have 1 row, and 4 columns, so your 4 buttons will fit into it nicely
         panel.add( submit );
    
         //add other buttons
    
        JFrame frame = ... 
    
           frame.add( panel , BorderLayout.NORTH); // puts it at the top of the frame
    Try this and it should work, i tend to never paint or add straight to the frame i always use components like JPanels.

  3. #3
    swedishfished is offline Member
    Join Date
    Sep 2012
    Posts
    24
    Rep Power
    0

    Default Re: Problem with JButton--Takes up entire screen

    when I do this nothing shows up

  4. #4
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: Problem with JButton--Takes up entire screen

    try setting its visibility

    panel.setVisible(true);

Similar Threads

  1. Problem about Black Screen
    By lawlawlaw in forum New To Java
    Replies: 2
    Last Post: 01-26-2012, 04:18 PM
  2. Replies: 2
    Last Post: 01-19-2012, 04:53 PM
  3. Problem with splash screen
    By frintocf in forum AWT / Swing
    Replies: 1
    Last Post: 09-06-2011, 11:42 PM
  4. Runnable JButton takes too much memory <-- Alternative?
    By Reskaillev in forum New To Java
    Replies: 7
    Last Post: 08-24-2011, 08:12 PM
  5. Do I have what it takes.
    By phil128 in forum Jobs Discussion
    Replies: 0
    Last Post: 03-04-2011, 12:32 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •