-1

My GUI Frame

In the image above, this is what my GUI looks like, i have a problem fixing up the textfield as it takes up the most percentage of space in my frame, and i want to correct the error I have. I know layout managers is a pain in the ass, but does anyone know how i can make the text field smaller so that my frame looks nicer.

I am using jpanels to hold most of my components

Here is the necessary part of my code

panel1 = new JPanel();
    panel1.setLayout(new GridLayout());
    frame.getContentPane().add(panel1, BorderLayout.NORTH);

        label2 = new JLabel("Score: " + score);
        label2.setFont(new Font("SimSun", Font.BOLD, 22));
        label2.setLocation(0,0);
        label2.setSize(117,37);
        panel1.add(label2);

        label3 = new JLabel("Lives: " + lives);
        label3.setFont(new Font("SimSun", Font.BOLD, 22));
        label3.setLocation(0,70);
        label3.setSize(105,49);
        panel1.add(label3);

    panel2 = new JPanel();
    panel2.setLayout(new BorderLayout());
    frame.getContentPane().add(panel2, BorderLayout.CENTER);

        label1 = new JLabel(a +" + " +b);
        label1.setFont(new Font("SimSun", Font.BOLD, 22));
        label1.setHorizontalAlignment(SwingConstants.CENTER);
        Border paddingBorder = BorderFactory.createEmptyBorder(10,10,10,10);
        label1.setBorder(BorderFactory.createCompoundBorder(null,paddingBorder));
        label1.setLocation(249,105);
        label1.setSize(183,60);
        panel2.add(label1, BorderLayout.NORTH);

        box1 = new JTextField();
        box1.setPreferredSize(new Dimension(50, 50));
        box1.setLocation(249,176);
        box1.setSize(96,25);
        panel2.add(box1, BorderLayout.CENTER);

    panel3 = new JPanel();
    panel3.setLayout(new GridLayout());
    frame.getContentPane().add(panel3, BorderLayout.SOUTH);

        button1 = new JButton("Answer");
        panel3.add(button1);
        button1.addActionListener(this);
        button1.setLocation(187,236);
        button1.setSize(105,50);

        button2 = new JButton("Reset");
        panel3.add(button2);
        button2.addActionListener(this);
        button2.setVisible(false);
        button2.setLocation(302,236);
        button2.setSize(105,50);
Kebab Programmer
  • 1,213
  • 2
  • 21
  • 36
  • 2
    1) See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) 2) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). 3) As an emergency measure, call `frame.pack()` after all components have been added.. – Andrew Thompson Nov 23 '14 at 18:22
  • .. 4) `BorderFactory.createCompoundBorder(null,paddingBorder)` The 'short' version of that is `paddingBorder`.. – Andrew Thompson Nov 23 '14 at 18:25

1 Answers1

3

The reason for this is you are adding your JTextField to BorderLayout.CENTER, which will stretch it to fill all the space available to this particular layout manager. Although I am not exactly sure how you want your GUI to look, perhaps a simple FlowLayout (the default layout) or a BoxLayout coupled with setAlignmentX(Component.CENTER_ALIGNMENT); on the JTextField will solve your problem.

You can find more information on layouts here: https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

Additionally, note that you shouldn't be using things like setLocation() as the point of the layout manager is to allow it to choose how components should be laid out on the screen, and as such the layout manager may ignore this as well as methods like setSize.

user3424612
  • 183
  • 1
  • 1
  • 9
  • Also note Andrew Thompson's comments on the question regarding my last point, he provides an excellent link with more information. – user3424612 Nov 23 '14 at 18:32