2014-10-06 55 views
0

在我的程序中,我的元素被組織成堆疊在一起,唯一的問題是它們水平伸展以觸及JFrame的所有邊緣,而我不想這樣,不能爲我的生活找出如何阻止這種情況的發生。沒有所有的元素接觸JFrame的邊框

下面是我的代碼,非常感謝。

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"); 

     } 

} 

回答