2014-10-06 21 views
-1

我的當前程序從某些JTextField中獲取文本並將其添加到文件以跟蹤藥物。我想要做的是在程序的底部添加另一個按鈕,該按鈕將打開第二個jframe以顯示已經記錄的藥物,但是所有嘗試都是慘遭失敗。以下是我目前正在使用的代碼。如何從當前打開第二個jframe?

非常感謝。

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
import java.io.File; 
import java.io.FileWriter; 
import java.io.IOException; 
import java.io.PrintWriter; 


@SuppressWarnings({ "serial" }) 
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; 

    public MedGUITest(){ 
     setLayout(new FlowLayout()); 

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

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

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

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

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

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

     submitButton = new JButton("Click Here to Add"); 
     event e = new event(); 
     submitButton.addActionListener(e); 
     add(submitButton); 

     meds = new JButton("Click Here to see meds"); 
     event2 r = new event2(); 
     meds.addActionListener(r); 
     add(meds); 

     finished = new JLabel(""); 
     add(finished); 
    } 


    public class event implements ActionListener { 

     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); 
       finished.setText("Your Med has been added"); 
      } catch (IOException e1) { 
       e1.printStackTrace(); 
      } 

     } 
    } 

    public class event2 implements ActionListener{ 

     public void actionPerformed(ActionEvent e){ 

     } 

    } 

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

    int winWidth = 300; 
    int winLength = 300; 

    MedGUITest gui = new MedGUITest(); 
    gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    gui.setSize(winWidth, winLength); 
    gui.setVisible(true); 
    gui.setTitle("Standard GUI"); 


    } 

} 
+1

請參閱[使用多個JFrames,好/壞實踐?](http://stackoverflow.com/q/9554636/418556) – 2014-10-06 00:10:08

+0

*「..所有的嘗試都是慘遭失敗。」*該代碼僅顯示任何對單個幀的引用。向我們展示您的一個嘗試,而不是爲其他人發佈「模板」來完成。 – 2014-10-06 00:11:42

+0

看看[如何製作對話框](http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html) – MadProgrammer 2014-10-06 00:18:22

回答

3

您可能想要使用JDialog而不是其他JFrame。您只需按照設置其他JFrame或JDialog的方式進行設置即可。你創建框架/對話框,添加組件,然後調用pack()然後setvisible(true)。您只想確保在關閉第二個JFrame時不要將JFrame.setDefaultCloseOperation()設置爲關閉。

我發現將我的類設置爲子類JPanel而不是JFrame更容易,因此更容易在其他位置重用它。

相關問題