0

In my program I have my elements organized to be stacked on top of each other, the only problem here is that they stretch horizontally to touch all edges of the JFrame, and I do not want this and cannot for the life of me figure out how to stop this from happening.

Below is my code, much thanks.

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;


@SuppressWarnings({ "serial", "unused" })
public class MedGUITest extends JFrame {

        private JLabel medName;
        private JLabel medTime;
        private JLabel medDose;
        private JLabel finished;

        private JTextField textFieldMed;
        private JTextField textFieldTime;
        private JTextField textFieldDose;

        private JButton submitButton;
        private JButton meds;
        static int winWidth = 300;
        static int winLength = 300;

        JTextArea ta = new JTextArea(winWidth,winLength);
        JPanel panel = new JPanel();

        public MedGUITest() throws FileNotFoundException, IOException{
            panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
            add(panel);

                setLayout(new FlowLayout());

                medName = new JLabel("Type Medication Here:");
                panel.add(medName);

                textFieldMed = new JTextField("",15);
                panel.add(textFieldMed);

                medDose = new JLabel("Type Medication Dose:");
                panel.add(medDose);

                textFieldDose = new JTextField("",15);
                panel.add(textFieldDose);

                medTime = new JLabel("Type Medication Time:");
                panel.add(medTime);

                textFieldTime = new JTextField("",15);
                panel.add(textFieldTime);

                submitButton = new JButton("Click Here to Add");
                submitButton.addActionListener(new ActionListener(){

                        @Override
                        public void actionPerformed(ActionEvent e){
                                String medName = textFieldMed.getText();
                                String medDose = textFieldDose.getText();
                                String medTime = textFieldTime.getText();
                                File med = new File("med.txt");

                                try(PrintWriter out= new PrintWriter(new FileWriter(med,true))) {
                                        out.println("Medication: " + medName + " " +"Dosage: "+ medDose + " Mg"+ " " +"Time of day: "+ medTime);
                                        String lastMed = ("Medication: " + medName + " " +"Dosage: "+ medDose + " Mg"+ " " +"Time of day: "+ medTime+ "\n");
                                        finished.setText("Your Med has been added");
                                        Timer t = new Timer(5000, new ActionListener() {

                                            @Override
                                            public void actionPerformed(ActionEvent e) {
                                                finished.setText(null);
                                            }
                                        });
                                        t.setRepeats(false);
                                        t.start();
                                        ta.append(lastMed);
                                } catch (IOException e1) {
                                        e1.printStackTrace();
                                }

                        }

                });
                panel.add(submitButton);

                final File med = new File("med.txt");

                if(med.exists()){

                }else{
                meds = new JButton("Click Here to see meds");
                meds.addActionListener(new ActionListener(){
                        public void actionPerformed(ActionEvent e) {

                                if(med.exists()){
                                try {
                                        ta.read(new FileReader("med.txt"),null);

                                } catch (IOException e1) {
                                        e1.printStackTrace();
                                }
                                panel.add(ta);}

                        }
                }); 
                panel.add(meds);
                }


                finished = new JLabel("");
                panel.add(finished);


                if(med.exists()){
                ta.read(new FileReader("med.txt"),null);
                panel.add(ta);}
        }



        public static void main(String args[]) throws FileNotFoundException, IOException{



        MedGUITest gui = new MedGUITest();
        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gui.pack();
        gui.setVisible(true);
        gui.setTitle("Standard GUI");

        }

}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
sirnomnomz
  • 623
  • 1
  • 7
  • 20

1 Answers1

2

Try adding an EmptyBorder to the content pane...

getContentPane().setBorder(new EmptyBorder(10, 10, 10, 10));

You could also consider using a layout manager which provides "padding", personally, I'd prefer GridBagLayout, but you could also look at FlowLayout(int align, int hgap, int vgap), but this will effect the spacing between ALL components, not just with the frame

Have a look at How to Use Borders and Laying Out Components Within a Container for some more ideas

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366