[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 266
  • Last Modified:

problems with GUI BorderLayout

I am trying to get my GUI program to have a nice layout, but everything I do doesn't work. I have to make an enormous window size to make everything visible in the window work. I also have not figured out how to make my OK button a normal size. It consistently fills the whole bottom of the screen. So my questions are:

1. How do I make my OK button smaller?
2. What do I need to do to get everything in my window visible and compact?
3. How do I make get my window static so I cannot resize it with my mouse?

I tried adjusting my setLocation and setSize, but none of the changes work.
0
monroe6
Asked:
monroe6
  • 3
  • 2
  • 2
  • +2
7 Solutions
 
monroe6Author Commented:
I am sorry, here is my code:


 import java.awt.*;
 import java.awt.event.*;
 import javax.swing.*;
 
 public class Week2Assignment2
 {
 
       private static final int WINDOW_WIDTH = 400; // pixels
      private static final int WINDOW_HEIGHT = 400;
      private static final int FIELD_WIDTH = 15;
      private static final int INSTRUCTION_WIDTH = 30;
      
 // variable data fields
 
       private String str1;
      private OkButtonHandler okHandler;

      
 // GUI fields
 
      private JFrame window;
      private String usage = "Enter a string of letters (spaces ok).";
      private JTextArea instructions;
      private JTextField str1Field;
      private JLabel str1Label;
      private JButton okB;
      private JRadioButton list1, list2;
            
// create method

 public Week2Assignment2()
 {
 
 // set up new window to contain all components
      
        window = new JFrame();
        window.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      
            
 // set up Instructions
 
        instructions = new JTextArea(usage);
        instructions.setEditable(false); // can't edit instructions
        instructions.setBackground(window.getBackground());
        instructions.setLineWrap(false);
        instructions.setWrapStyleWord(false);

 // set up fields for data entry/display
 
        str1Label = new JLabel("Enter any string of letters");
      str1Field = new JTextField(FIELD_WIDTH);
      
// set up center panel
      
        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new FlowLayout());
      
// set up panel for user input

        JPanel str1Panel = new JPanel();
        str1Panel.setLayout(new FlowLayout());
        str1Panel.add(str1Label);
       str1Panel.add(str1Field);
      
      
// add components to main panel

        mainPanel.add(str1Panel);
        
// obtain content pane for this window

        Container c = window.getContentPane();

// set layouts
      
        c.setLayout(new BorderLayout());
      
// create list1 radio button

      list1 = new JRadioButton("1. Determine length of string");
      list1.setSize(100, 70);
      list1.setLocation(500, 100);
      
// create list2 radio button

      list2 = new JRadioButton("2. Determine how many consonants or vowels are in the string.");
      list2.setSize(100, 70);
      list2.setLocation(600, 150);

             
// create Ok button
      
      okB = new JButton("Ok");
      okHandler = new OkButtonHandler();
      okB.addActionListener(okHandler);
        
// add all fields to the content pane
      
        c.add(instructions, BorderLayout.NORTH);
      c.add(mainPanel, BorderLayout.CENTER);
      c.add(okB, BorderLayout.SOUTH);
      c.add(list1, BorderLayout.WEST);
      c.add(list2, BorderLayout.EAST);

// make the window visible
  window.setSize(1000,400);
  window.setVisible(true);
      
}      

private class OkButtonHandler implements ActionListener
{
      public void actionPerformed(ActionEvent e)
      {
            
            String str2;
            String str3;
            
            str2 = "You entered " + str1Field.getText()
                         + ". Choose one of the following.\n";      
            str3 = "1. Determine length of string \n" +
                         "2. Determine how many consonants or vowels are in the string.\n";
                         
                         System.out.print (str2);
                         System.out.print (str3);
      }
}

// create main
            
      public static void main(String[] args)
      {

    Week2Assignment2 gui = new Week2Assignment2();
   }
}
0
 
Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
>>How do I make my OK button smaller?

As you have added  only Ok utton to the south directly using c.add(okB, BorderLayout.SOUTH); It occupies the complete with of your layout. try this,

JPanel panel = new JPanel();
panel.add(okB);
 c.add(panel, BorderLayout.SOUTH);

This will fix the first one.

3rd one that is to fix the size of the window

window = new JFrame();
        window.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

>> Add this line here window.setResizable(false);
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
monroe6Author Commented:
I almost got it, but now everything runs on the same line. How do I fix that?

import java.awt.*;
 import java.awt.event.*;
 import javax.swing.*;
 
 public class Week2Assignment2
 {
 
       private static final int WINDOW_WIDTH = 400; // pixels
      private static final int WINDOW_HEIGHT = 400;
      private static final int FIELD_WIDTH = 15;
      private static final int INSTRUCTION_WIDTH = 30;
      
 // variable data fields
 
       private String str1;
      private OkButtonHandler okHandler;

      
 // GUI fields
 
      private JFrame window;
      private String usage = "Enter a string of letters (spaces ok).";
      private JTextArea instructions;
      private JTextField str1Field;
      private JLabel str1Label;
      private JButton okB;
      private JRadioButton list1, list2;
            
// create method

 public Week2Assignment2()
 {
 
 // set up new window to contain all components
      
        window = new JFrame();
        window.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      
            
 // set up Instructions
 
        instructions = new JTextArea(usage);
        instructions.setEditable(false); // can't edit instructions
        instructions.setBackground(window.getBackground());
        instructions.setLineWrap(false);
        instructions.setWrapStyleWord(false);

 // set up fields for data entry/display
 
        str1Label = new JLabel("Enter any string of letters");
      str1Field = new JTextField(FIELD_WIDTH);
      
// set up center panel
      
        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new FlowLayout());
      
// set up panel for user input

        JPanel str1Panel = new JPanel();
        str1Panel.setLayout(new FlowLayout());
        str1Panel.add(str1Label);
       str1Panel.add(str1Field);
      
      
// add components to main panel

        mainPanel.add(str1Panel);
        
// obtain content pane for this window

        Container c = window.getContentPane();

// set layouts
      
        c.setLayout(new BorderLayout());
      
// create list1 radio button

      list1 = new JRadioButton("1. Determine length of string");
      list1.setSize(100, 70);
      list1.setLocation(500, 100);
      
      JPanel panel2 = new JPanel();
      panel2.add(list1);
      
// create list2 radio button

      list2 = new JRadioButton("2. Determine how many consonants or vowels are in the string.");
      list2.setSize(100, 70);
      list2.setLocation(600, 150);
      
      JPanel panel3 = new JPanel();
      panel3.add(list2);

             
// create Ok button
      
      okB = new JButton("Ok");
      okHandler = new OkButtonHandler();
      okB.addActionListener(okHandler);
      
      JPanel panel = new JPanel();
      panel.add(okB);
        
// add all fields to the content pane
      
        c.add(instructions, BorderLayout.NORTH);
      c.add(mainPanel, BorderLayout.WEST);
      c.add(panel, BorderLayout.SOUTH);
      c.add(panel2, BorderLayout.CENTER);
      c.add(panel3, BorderLayout.EAST);

// make the window visible
  window.setSize(950,150);
  window.setVisible(true);
  window.setResizable(false);
      
}      

private class OkButtonHandler implements ActionListener
{
      public void actionPerformed(ActionEvent e)
      {
            
            String str2;
            String str3;
            
            str2 = "You entered " + str1Field.getText()
                         + ". Choose one of the following.\n";      
            str3 = "1. Determine length of string \n" +
                         "2. Determine how many consonants or vowels are in the string.\n";
                         
                         System.out.print (str2);
                         System.out.print (str3);
      }
}

// create main
            
      public static void main(String[] args)
      {

    Week2Assignment2 gui = new Week2Assignment2();
   }
}
0
 
Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
This seems to be a assignment and hence as per the rules I think we are only allowed to direct you towards solution.
 Any way My above suggestions do work. And now things you need to do are you need to group the radio buttons

see the example here

http://www.java2s.com/Code/Java/Swing-JFC/UsingJRadioButtons.htm
0
 
contactkarthiCommented:
0
 
Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
@contactkarthi:

GridBagLayout is the toughest layout in swing and I don't think such a small app need it.
0
 
contactkarthiCommented:
you need to visualise how the components will be place based on the values you specify..

initially there will be a couple of trial and error.. but once you get the point it is the best flexible way for placing your components..
0
 
objectsCommented:
try a boxlayout

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

public class Week2Assignment2
{

      private static final int WINDOW_WIDTH = 400; // pixels
      private static final int WINDOW_HEIGHT = 400;
      private static final int FIELD_WIDTH = 15;
      private static final int INSTRUCTION_WIDTH = 30;

      // variable data fields

      private String str1;
      private OkButtonHandler okHandler;

      // GUI fields

      private JFrame window;
      private String usage = "Enter a string of letters (spaces ok).";
      private JTextArea instructions;
      private JTextField str1Field;
      private JLabel str1Label;
      private JButton okB;
      private JRadioButton list1, list2;

      // create method

      public Week2Assignment2()
      {

            // set up new window to contain all components

            window = new JFrame();
            window.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
            window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            // set up Instructions

            instructions = new JTextArea(usage);
            instructions.setEditable(false); // can't edit instructions
            instructions.setBackground(window.getBackground());
            instructions.setLineWrap(false);
            instructions.setWrapStyleWord(false);

            // set up fields for data entry/display

            str1Label = new JLabel("Enter any string of letters");
            str1Field = new JTextField(FIELD_WIDTH);

            // set up center panel

            JPanel mainPanel = new JPanel();
            mainPanel.setLayout(new FlowLayout());

            // set up panel for user input

            JPanel str1Panel = new JPanel();
            str1Panel.setLayout(new FlowLayout());
            str1Panel.add(str1Label);
            str1Panel.add(str1Field);

            // add components to main panel

            mainPanel.add(str1Panel);

            // obtain content pane for this window

            Container c = window.getContentPane();

            // set layouts

            c.setLayout(new BorderLayout());

            // create list1 radio button

            JPanel form = new JPanel();
            form.setLayout(new BoxLayout(form, BoxLayout.Y_AXIS));

            form.add(mainPanel);
            list1 = new JRadioButton("1. Determine length of string");

            form.add(list1);

            // create list2 radio button

            list2 = new JRadioButton(
                    "2. Determine how many consonants or vowels are in the string.");

            form.add(list2);

            // create Ok button

            okB = new JButton("Ok");
            okHandler = new OkButtonHandler();
            okB.addActionListener(okHandler);

            JPanel panel = new JPanel();
            panel.add(okB);

            // add all fields to the content pane

            c.add(instructions, BorderLayout.NORTH);
            c.add(panel, BorderLayout.SOUTH);
            c.add(form, BorderLayout.CENTER);

            // make the window visible
            window.pack();
            window.setVisible(true);
            window.setResizable(false);

      }

      private class OkButtonHandler implements ActionListener
      {
            public void actionPerformed(ActionEvent e)
            {

                  String str2;
                  String str3;

                  str2 = "You entered " + str1Field.getText()
                          + ". Choose one of the following.\n";
                  str3 = "1. Determine length of string \n"
                          + "2. Determine how many consonants or vowels are in the string.\n";

                  System.out.print(str2);
                  System.out.print(str3);
            }
      }

      // create main

      public static void main(String[] args)
      {

            Week2Assignment2 gui = new Week2Assignment2();
      }
}
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now