0

I would like to add some space between the JTextField and the JButtons, i am using a flow layout, and help would be appreciated

The text field and the buttons are to close, any input would help, i am a newb

I am making a calculator

CODE

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
 import java.awt.Font;

import javax.swing.JButton;
import javax.swing.JFrame;
 import javax.swing.JTextField;



public class Ken {

static JTextField text = new JTextField("0",9);



public static void frame(){
    JFrame frame = new JFrame("MyCalc");
    frame.setSize(480, 600);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    frame.setResizable(false);
    FlowLayout fl = new FlowLayout(0, 30, 20);
    setLayout(fl);

    frame.getContentPane().setLayout(fl);
    Container con = frame.getContentPane();
    con.setBackground(Color.LIGHT_GRAY);
    Font f = new Font("MS UI Gothic", Font.BOLD, 40);
    Font f2 = new Font("Engravers MT", Font.PLAIN, 40);
    int bHeight = 80;
    int bWidth = 70;
    text.setBackground(Color.GREEN);
    text.setFont(f2);
    text.setPreferredSize(new Dimension(bHeight, bWidth));




    frame.add(text, BorderLayout.NORTH);

    JButton oneButton = new JButton("1");
    oneButton.setPreferredSize(new Dimension(bHeight, bWidth));
    oneButton.setBackground(Color.DARK_GRAY);
    oneButton.setForeground(Color.GREEN);
    oneButton.setFont(f);
    frame.add(oneButton);

    JButton twoButton = new JButton("2");
    twoButton.setPreferredSize(new Dimension(bHeight, bWidth));
    twoButton.setBackground(Color.DARK_GRAY);
    twoButton.setForeground(Color.GREEN);
    twoButton.setFont(f);
    frame.add(twoButton);

    JButton threeButton = new JButton("3");
    threeButton.setPreferredSize(new Dimension(bHeight, bWidth));
    threeButton.setBackground(Color.DARK_GRAY);
    threeButton.setForeground(Color.GREEN);
    threeButton.setFont(f);
    frame.add(threeButton);

    JButton plusButton = new JButton("+");
    plusButton.setPreferredSize(new Dimension(bHeight, bWidth));
    plusButton.setBackground(Color.DARK_GRAY);
    plusButton.setForeground(Color.GREEN);
    plusButton.setFont(f);
    frame.add(plusButton);

    JButton fourButton = new JButton("4");
    fourButton.setPreferredSize(new Dimension(bHeight, bWidth));
    fourButton.setBackground(Color.DARK_GRAY);
    fourButton.setForeground(Color.GREEN);
    fourButton.setFont(f);
    frame.add(fourButton);

    JButton fiveButton = new JButton("5");
    fiveButton.setPreferredSize(new Dimension(bHeight, bWidth));
    fiveButton.setBackground(Color.DARK_GRAY);
    fiveButton.setForeground(Color.GREEN);
    fiveButton.setFont(f);
    frame.add(fiveButton);

    JButton sixButton = new JButton("6");
    sixButton.setPreferredSize(new Dimension(bHeight, bWidth));
    sixButton.setBackground(Color.DARK_GRAY);
    sixButton.setForeground(Color.GREEN);
    sixButton.setFont(f);
    frame.add(sixButton);

    JButton minusButton = new JButton("-");
    minusButton.setPreferredSize(new Dimension(bHeight, bWidth));
    minusButton.setBackground(Color.DARK_GRAY);
    minusButton.setForeground(Color.GREEN);
    minusButton.setFont(f);
    frame.add(minusButton);

    JButton sevenButton = new JButton("7");
    sevenButton.setPreferredSize(new Dimension(bHeight, bWidth));
    sevenButton.setBackground(Color.DARK_GRAY);
    sevenButton.setForeground(Color.GREEN);
    sevenButton.setFont(f);
    frame.add(sevenButton);

    JButton eightButton = new JButton("8");
    eightButton.setPreferredSize(new Dimension(bHeight, bWidth));
    eightButton.setBackground(Color.DARK_GRAY);
    eightButton.setForeground(Color.GREEN);
    eightButton.setFont(f);
    frame.add(eightButton);

    JButton nineButton = new JButton("9");
    nineButton.setPreferredSize(new Dimension(bHeight,bWidth));
    nineButton.setBackground(Color.DARK_GRAY);
    nineButton.setForeground(Color.GREEN);
    nineButton.setFont(f);
    frame.add(nineButton);

    JButton timesButton = new JButton("*");
    timesButton.setPreferredSize(new Dimension(bHeight, bWidth));
    timesButton.setBackground(Color.DARK_GRAY);
    timesButton.setForeground(Color.GREEN);
    timesButton.setFont(f);
    frame.add(timesButton);

    JButton zeroButton = new JButton("0");
    zeroButton.setPreferredSize(new Dimension(bHeight, bWidth));
    zeroButton.setBackground(Color.DARK_GRAY);
    zeroButton.setForeground(Color.GREEN);
    zeroButton.setFont(f);
    frame.add(zeroButton);

    JButton enterButton = new JButton("=");
    enterButton.setPreferredSize(new Dimension(190, bWidth));
    enterButton.setBackground(Color.DARK_GRAY);
    enterButton.setForeground(Color.GREEN);
    enterButton.setFont(f);
    frame.add(enterButton);

    JButton dividButton = new JButton("/");
    dividButton.setPreferredSize(new Dimension(bHeight, bWidth));
    dividButton.setBackground(Color.DARK_GRAY);
    dividButton.setForeground(Color.GREEN);
    dividButton.setFont(f);
    frame.add(dividButton);

    frame.setComponentOrientation(
            ComponentOrientation.LEFT_TO_RIGHT);
}



private static Dimension Dimension(int bHeight, int bWidth) {
    // TODO Auto-generated method stub
    return null;
}



private static void setLayout(FlowLayout fl) {
    // TODO Auto-generated method stub

}



public static void main(String[] args){
    frame();
}
 }
ndfkj asdkfh
  • 316
  • 1
  • 5
  • 15
  • Why does your code still have the `setPreferredSize()` statement along with the other statements to set the width/heigt variables??? – camickr Jun 24 '13 at 00:39
  • @camickr i fixed my issue by changing the parameters of static JTextField text = new JTextField("0",9); – ndfkj asdkfh Jun 24 '13 at 00:41
  • That is my point. The other code does nothing and is not necessary ... get rid of it!!! – camickr Jun 24 '13 at 00:42
  • New user name? [why-are-my-buttons-taking-up-the-whole-jframe](http://stackoverflow.com/questions/17261699/why-are-my-buttons-taking-up-the-whole-jframe). You still haven't accepted any answers from previous questions. And it's not my answer that concerns me. It's all your other questions. Please fix this. – Hovercraft Full Of Eels Jun 24 '13 at 01:02
  • @HovercraftFullOfEels sorry, i did not know you could accept answers, that account got, like, banned – ndfkj asdkfh Jun 24 '13 at 01:06
  • And again, you're much better off not using FlowLayout as I explained in my answer [here](http://stackoverflow.com/a/17261724/522444). – Hovercraft Full Of Eels Jun 24 '13 at 01:15

1 Answers1

2

FlowLayout fl = new FlowLayout(0, 30, 20);

Not related to this question, but don't use magic numbers. I have no idea what the 0 means. Read the FlowLaout API and use the proper variable name provide by the API.

By default a the content pane of the frame uses a BorderLayout. Check our the BorderLayout API. You can specify the gap size between components. So you can create a new BorderLayout with the desired gap and then set the layout for the content pane to this BorderLayout.

Or, you can add an EmptyBorder to the top panel. Read the Swing tutorial on How to Use Borders for more information and examples.

camickr
  • 321,443
  • 19
  • 166
  • 288