Link to home
Start Free TrialLog in
Avatar of monroe6
monroe6

asked on

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.
Avatar of monroe6
monroe6

ASKER

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();
   }
}
ASKER CERTIFIED SOLUTION
Avatar of Siva Prasanna Kumar
Siva Prasanna Kumar
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of monroe6

ASKER

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();
   }
}
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial