2016-01-21 26 views
0

可能有人告訴我如何我的GUI連接到我的基類 我已經寫了圖形用戶界面和設計,但現在需要的GUI與其他類我做交互。聘任制度Swing GUI的

編輯:添加更新的代碼,以我的線程,增加了新的方法,按下按鈕時,以文本打印出來給的TextField。即顯示約會,顯示按鈕旁邊的文本字段中的所有約會。 此外,需要嘗試,並添加了一個選項,用戶使用公曆格式

package com.appointmentsys; 

import javax.swing.JFrame; 
import java.util.GregorianCalendar; 
import javax.swing.BorderFactory; 
import javax.swing.JButton; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 
import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 


/** 
* 
* @author Daniel Burke 
* 
*/ 
public class ControllerGUI extends JPanel implements ActionListener{ 

    static JButton button1; 
    static JButton button2; 
    static JButton button3; 
    static JButton button4; 
    static JTextField ta; 
    static JTextField ta1; 
    static JTextField ta2; 
    static JTextField ta3; 


    AppointmentBook appBook = new AppointmentBook(); 

    public void MainForm(){ 

     button1.addActionListener(this); 
     button2.addActionListener(this); 
     button3.addActionListener(this); 
     button4.addActionListener(this); 
     ta.addActionListener(this); 
     ta1.addActionListener(this); 
     ta2.addActionListener(this); 
     ta3.addActionListener(this); 

         } 

    public static void CreateandShowGUI(){ 

     JFrame frame = new JFrame(); 
     JPanel panel = new JPanel(); 
     panel.setLayout(new GridLayout(10,10)); 

     button1 = new JButton("Add appointment"); 
     ta = new JTextField(); 
     /* 
     TextAreaOutputStream taos = new TextAreaOutputStream(ta, 60); 
     PrintStream ps = new PrintStream(taos); 
     System.setOut(ps); 
     System.setErr(ps); 
*/ 
     button2 = new JButton("Remove appointment"); 
     ta1 = new JTextField(); 
     button3 = new JButton("Show appointment"); 
     ta2 = new JTextField(); 
     button4 = new JButton("Search appointments"); 
     ta3 = new JTextField(); 

     frame.setContentPane(panel); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.pack(); 
     frame.setVisible(true); 


    panel.add(new JLabel("   ")); 
    panel.add(new JLabel("  Please select an option: ")); 
    panel.add(new JLabel("   ")); 
    panel.add(new JLabel("   ")); 
    panel.add(button1); 
    panel.add(ta); 
     panel.add(button2); 
     panel.add(ta1); 
     panel.add(button3); 
     panel.add(ta2); 
     panel.add(button4); 
     panel.add(ta3); 
     panel.setBorder(BorderFactory.createTitledBorder("Appointment System")); 

} 
    public class EventHandler implements ActionListener{ 


     public void actionPerformed(ActionEvent e){ 
      if(e.getSource() == button1){ 
      appBook.add(new Appointment(new GregorianCalendar(2015, 8+1, 1, 11, 30), new GregorianCalendar(2015, 10, 14, 11, 30), "Dyland")); 
      } 
      else if(e.getSource() == button3){ 
       String appointmentInfo = appBook.getAppointment(0).toString(); 
       ta2.setText(appointmentInfo); 



      } 

     } 
    } 

    public static void main(String[] args) { 

     javax.swing.SwingUtilities.invokeLater(new Runnable(){ 
    public void run(){ 
     CreateandShowGUI(); 
     } 
      }); 
     } 


    @Override 
    public void actionPerformed(ActionEvent arg0) { 
     // TODO Auto-generated method stub 

    } 




    } 

package com.appointmentsys; 

import java.util.ArrayList; 
import java.util.GregorianCalendar; 

/** 
* 
* Controller class will test Appointment/AppointmentBook 
* @author Daniel Burke 
* 
*/ 
public class Controller { 

    public static void main(String[] args) { 

     Appointment a1 = new Appointment(new GregorianCalendar(2015, 8+1, 14, 10, 30), new GregorianCalendar(2015, 10, 14, 11, 30), "Danny"); 
     Appointment a2 = new Appointment(new GregorianCalendar(2015, 8+1, 20, 9, 00), new GregorianCalendar(2015, 10, 20, 10, 10), "JOhn"); 
     Appointment a3 = new Appointment(new GregorianCalendar(2015, 8+1, 21, 14, 00), new GregorianCalendar(2015, 10, 21, 16, 00), "Steve");    
     Appointment a4 = new Appointment(new GregorianCalendar(2015, 8+1, 21, 14, 00), new GregorianCalendar(2015, 10, 21, 16, 00), "Patrick"); 

     AppointmentBook appBook = new AppointmentBook(); 
     appBook.add(a1); 
     appBook.add(a2); 
     appBook.add(a3); 
     appBook.add(a4); 

     System.out.println("Appointment is in book: " + appBook.isInBook(a1)); 
     System.out.println("Appointment is in book: " + appBook.isInBook(a4)); 
     //appBook.remove(a1); 
     appBook.ShowAppointments(); 


    } 
} 

    package com.appointmentsys; 

import java.text.SimpleDateFormat; 
import java.util.Calendar; 
import java.util.GregorianCalendar; 


public class Appointment { 

    //Appointments have start/end times & dates. 
    //Every appointment has a title. 

    private GregorianCalendar startDateTime; 
    private GregorianCalendar endDateTime; 
    private String eventTitle; 

    //default constructor 
    public Appointment(){ 
     this.startDateTime = null; 
     this.endDateTime = null; 
     this.eventTitle = ""; 
    } 

    //constructor 

    public Appointment(GregorianCalendar startDate, GregorianCalendar endDate, String eventTitle){ 
     this.startDateTime = startDate; 
     this.endDateTime = endDate; 
     this.eventTitle = eventTitle; 
    } 


    public GregorianCalendar getStartDateTime() { 
     return startDateTime; 
    } 
    public void setStartDateTime(GregorianCalendar startDateTime) { 
     this.startDateTime = startDateTime; 
    } 
    public GregorianCalendar getEndDateTime() { 
     return endDateTime; 
    } 
    public void setEndDateTime(GregorianCalendar endDateTime) { 
     this.endDateTime = endDateTime; 
    } 
    public String getEventTitle() { 
     return eventTitle; 
    } 
    public void setEventTitle(String eventTitle) { 
     this.eventTitle = eventTitle; 
    } 

    //toString() method to represent an appointment object 
    public String toString(){ 

     String strdate = null; 
     int hours = 0; 
     String hrs = null; 
     int mins = 0; 
     String min = null; 

     SimpleDateFormat sdf = new SimpleDateFormat("MM/DD/YYYY"); 

     if (getStartDateTime() != null){ 
      strdate = sdf.format(getStartDateTime().getTime()); 
      hours = getStartDateTime().get(Calendar.HOUR); 
      hrs = Integer.toString(hours); 
      mins = getStartDateTime().get(Calendar.MINUTE); 
      min = Integer.toString(mins); 
     } 
     String s = getEventTitle()+" "+ strdate+" "+ hrs +": "+min; 
     return "Appointment: \n" + s; 



    } 


} 

    package com.appointmentsys; 

import java.util.ArrayList; 

public class AppointmentBook { 

    private static final int NOTFOUND = -1; //NOTFOUND int constant 

    //We can use an ArrayList to store appointments (you could use a database) 

    private ArrayList<Appointment> appointmentList = new ArrayList<Appointment>(); 

    //add method to appointmentbook 

    /** 
    * Adds appointments to the appointmentList 
    * @param a 
    */ 

    public void add(Appointment a){ 

    appointmentList.add(a); 
    } 
    /** 
    * create a new arrayList for all appoints then return all 
    * @return 
    */ 

    public ArrayList<Appointment> getAllAppointments(){ 

     ArrayList<Appointment> all = new ArrayList<Appointment>(appointmentList); 
     return all; 
    } 
    /** 
    * Prints out the list of all the appointsment made 
    * 
    */ 
    public void ShowAppointments() 
    { 
     ArrayList<Appointment> all = new ArrayList<Appointment>(appointmentList); 
     System.out.println(); 
     System.out.print("All appointments: \n"); 


     for(Appointment a: all) 
     { 
      System.out.println(a); 
      System.out.println(); 
     } 
    } 
    /** 
    * returns -1 if no appointment is found 
    * @param tofind 
    * @return 
    */ 
    private int find(Appointment tofind) 
    { 
     int i = 0; 

     for(Appointment a: appointmentList) 
     { 
      if(a.equals(tofind)) return i; 
      i++; 
     } 
       return NOTFOUND; 
    } 
    /** 
    * removes an appointment from the appointmentList 
    * @param toRemove 
    */ 
    public void remove(Appointment toRemove){ 

     int location = find(toRemove); 
     if(location != NOTFOUND) appointmentList.remove(location); 
     else 
      throw new IllegalArgumentException("Appointment not found"); 
    } 
    /** 
    * 
    * @param a 
    * @return 
    */ 
    public boolean isInBook(Appointment a){ 
     return find(a) != NOTFOUND; 

    } 

    public String getAppointment(int i) { 

     return appointmentList.get(i).toString(); 

    } 

} 
+0

如果你不知道如何「連接」兩個類,你應該學習基礎知識,而不是編寫UI代碼。 – Stultuske

回答

0

你應該用發展的MVC方法進入自己的任命,

你的業務邏輯是在底部訪問Gui可以調用的服務,然後您可以在頂部 上放置一個網站/擺動/任何其他查看系統或多或少地訪問您的業務邏輯的API

AppointmentService.addAppointment(Appointment appointment); AppointmentService.getAppointments();

etc

1

您已經有大部分的代碼。你有你的AppointmentBook類的實例在ControllerGUI

AppointmentBook appBook = new AppointmentBook(); 

您可以使用在事件處理appBook對象。

public class EventHandler implements ActionListener{ 

    public void actionPerformed(ActionEvent e){ 
     if(e.getSource() == button1) { 
      appBook.add(new Appointment()); 
     } 
    } 
} 

我沒有看到的約會類那裏的代碼,但你可以調用任何的構造是在appBook.add呼叫你的約會類。

E.g.

appBook.add(new Appointment("21-01-2016", "Meeting")); 

如果你有一個構造函數在2串預約

編輯:

看到你額外的代碼我看到之後,你有2個主要的()方法。所以這些是真正的2個獨立的程序。

您可以嘗試到2種主要的方法結合起來。

而不是使一堆約會中的主要方法。您應該通過單擊其中一個按鈕來測試添加約會。

公共類事件處理程序實現的ActionListener {

public void actionPerformed(ActionEvent e){ 
     if(e.getSource() == button1) { 
      appBook.add(new Appointment(new GregorianCalendar(2015, 8+1, 14, 10, 30), new GregorianCalendar(2015, 10, 14, 11, 30), "Danny")); 
     } 
    } 
} 

您也可以將另一個按鈕檢查打電話給你appBook.ShowAppointments()方法。

添加harcoded約會一樣,是不理想的,但。所以測試一下,然後添加一些方法,允許您傳入值。

您不需要其他主要方法都來得到這個工作,只是一個與CreateandShowGUI通話。

編輯2: 您的Appointment類中已經有了toString方法。 將一個getAppointment方法添加到AppointmentBook類中,該類允許您通過索引獲取任何約會,並將該索引作爲參數。會返回的東西appointmentList.get(index);

所以在你的eventHandler中,你可以使用它來設置你的文本字段。

public void actionPerformed(ActionEvent e){ 
     if(e.getSource() == button3) { 
      String appointmentInfo = appBook.getAppointment(0).toString(); 
      ta.setText(appointmentInfo); 
     } 
    } 

這假定您的appBook對象中至少有一個約會。因此,在嘗試設置文本之前,您必須添加一些代碼來檢查appBook是否爲空。

編輯3:

你實際上並沒有使用你的EventHandler。這是你的ControllerGUI文件應該是什麼樣子:

public class ControllerGUI extends JPanel { 

    static JButton button1; 
    static JButton button2; 
    static JButton button3; 
    static JButton button4; 
    static JTextField ta; 

    static AppointmentBook appBook = new AppointmentBook(); 
    static EventHandler eventHandler; 

    public static void CreateandShowGUI() { 

     JFrame frame = new JFrame(); 
     JPanel panel = new JPanel(); 
     panel.setLayout(new GridLayout(10, 10)); 

     button1 = new JButton("Add appointment"); 
     button2 = new JButton("Remove appointment"); 
     button3 = new JButton("Show appointment"); 
     ta = new JTextField(); 
     button4 = new JButton("Search appointments"); 

     eventHandler = new EventHandler(); 
     button1.addActionListener(eventHandler); 
     button2.addActionListener(eventHandler); 
     button3.addActionListener(eventHandler); 
     button4.addActionListener(eventHandler); 

     frame.setContentPane(panel); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.pack(); 
     frame.setVisible(true); 

     panel.add(new JLabel("   ")); 
     panel.add(new JLabel("Please select an option: ")); 
     panel.add(new JLabel("   ")); 
     panel.add(button1); 
     panel.add(button2); 
     panel.add(button3); 
     panel.add(button4); 
     panel.add(ta); 
     panel.setBorder(BorderFactory.createTitledBorder("Appointment System")); 

    } 

    public static class EventHandler implements ActionListener { 

     public void actionPerformed(ActionEvent e) { 
      if (e.getSource() == button1) { 
       appBook.add(new Appointment(new GregorianCalendar(2015, 8 + 1, 14, 10, 30), new GregorianCalendar(2015, 10, 14, 11, 30), 
         "Danny")); 
      } 

      if (e.getSource() == button3) { 
       ta.setText(appBook.getAppointment(0).toString()); 
      } 
     } 
    } 

    public static void main(String[] args) { 

     javax.swing.SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       CreateandShowGUI(); 
      } 
     }); 
    } 
} 

而在你AppointmentBook類中的方法應該是這樣的:

public Appointment getAppointment(int index) { 
    return appointmentList.get(0); 
} 

我真的建議你繼續之前修改了很多基礎知識雖然。您需要更好地掌握方法(傳遞參數並返回值)。在嘗試這個級別的GUI程序之前,您需要完成所有這些工作。

在上面的類中,我將EventHandler類設爲static,然後在CreateandShowGUI類中創建了它的一個實例。然後我將按鈕添加到EventHandler(actionlistener)。這只是完成了你的代碼。最好有一個類在單獨的文件中處理所有這些,而不是靜態類。所以你可以實例化它,並且對你想要的方法進行任何和所有的調用,而不是靜態的。

這就是我現在可以給予的所有幫助。

+0

謝謝,我剛剛在線程中添加了Appointment類,忘記添加它原來 – Gdohfg

+0

謝謝!我還將ShowAppointments()方法添加到我的事件處理程序中,您是否知道如何將結果打印到JTextField中? – Gdohfg

+0

您可以向AppointmentBook類添加方法以返回約會的字符串或所有約會。見上面的下一個編輯。 –

0

從一個簡單的命令行界面(CLI)類開始。

只需使用練習AppointmentBook的main()方法編碼類即可。 一旦您瞭解了AppointmentBook的工作方式,就返回到您的GUI。