简体   繁体   English

约会系统的摆动GUI

[英]Swing GUI for appointment system

could anybody advise me on how to connect my GUI to my base classes I have wrote the gui and designed it, but now need the GUI to interact with another class I made. 有人可以建议我如何将GUI连接到我编写的gui并对其进行设计的基类上,但是现在需要GUI与我制作的另一个类进行交互。

edit: added updated code to my thread, added new methods to print out text to the TextFields when buttons are pressed. 编辑:将更新的代码添加到我的线程中,添加了新的方法来在按下按钮时将文本打印到TextFields中。 Ie Show Appointments, displays all of the appointments in the textfield next to the button. 即显示约会,在按钮旁边的文本字段中显示所有约会。 Also, need to try and add an option for a user to enter their own appointment using the Gregorian calendar format 另外,需要尝试为用户添加一个选项,以使用公历日历格式输入自己的约会

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

    }

}

You have most of the code already. 您已经拥有大多数代码。 You have a instance of your AppointmentBook class in your ControllerGUI 您的ControllerGUI中有一个AppointmentBook类的实例

AppointmentBook appBook = new AppointmentBook();

You can use that appBook object in your event handler. 您可以在事件处理程序中使用该appBook对象。

public  class EventHandler implements ActionListener{

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

I don't see the code for the Appointment class there, but you can call whatever the constructor is for your Appointment class in the appBook.add call. 我在那里没有看到Appointment类的代码,但是您可以在appBook.add调用中为Appointment类调用任何构造函数。

Eg 例如

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

If you had a constructor that takes in 2 strings for an appointment 如果您的构造函数需要2个字符串进行约会

Edit: 编辑:

After seeing your additional code I see you have 2 main() methods. 看完您的其他代码后,我看到您有2个main()方法。 So these are truly 2 separate programs. 因此,这些确实是2个单独的程序。

You can try to combine the 2 main methods. 您可以尝试结合两种主要方法。

Instead of making a bunch of appointments in the main method. 而不是在main方法中进行一堆约会。 You should test out adding appointments by clicking one of the buttons. 您应该通过单击按钮之一来测试添加约会。

public class EventHandler implements ActionListener{ 公共类EventHandler实现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"));
        }
    }
}

You can also make another button check to call your appBook.ShowAppointments() method. 您还可以进行另一个按钮检查,以调用appBook.ShowAppointments()方法。

Adding a harcoded appointment like that isn't ideal though. 但是添加这样的经过编码的约会并不理想。 So test it out a bit and then add some methods that allow you to pass in the values. 因此,对其进行一点测试,然后添加一些允许您传递值的方法。

You won't need the other main method at all to get this to work, just the one with the CreateandShowGUI call. 您根本不需要其他主要方法来使它起作用,只需使用CreateandShowGUI调用即可。

Edit2: You have a toString method in your Appointment class already. Edit2:您的约会类中已经有一个toString方法。 Add a getAppointment method to your AppointmentBook class that allows you to get any appointment by an index, taking that index as a parameter. 将一个getAppointment方法添加到AppointmentBook类中,该方法使您可以通过索引获取任何约会,并将该索引作为参数。 Something that would return appointmentList.get(index); 会返回appointmentList.get(index);

So in your eventHandler you can use that to set your text field. 因此,您可以在eventHandler中使用它来设置文本字段。

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

This assumes that you have at least one appointment in your appBook object. 假设您的appBook对象中至少有一个约会。 So you'll have to add some code to check that the appBook isn't empty before trying to set the text. 因此,在尝试设置文本之前,您必须添加一些代码以检查appBook是否为空。

Edit3: 编辑3:

You aren't actually using your EventHandler. 您实际上并没有使用EventHandler。 This is what your ControllerGUI file should look like: 这是您的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();
            }
        });
    }
}

And the method in your AppointmentBook class should look like this: 并且AppointmentBook类中的方法应如下所示:

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

I would really recommend that you revise a lot of the basics before continuing though. 我真的建议您在继续之前先修改很多基础知识。 You need to have a better grasp of methods (passing them parameters and returning values). 您需要更好地掌握方法(将参数传递给它们并返回值)。 You'll need to get all of that before trying a GUI program of this level. 在尝试此级别的GUI程序之前,您需要获取所有这些信息。

In the above class I made the EventHandler class static, then I made an instance of it in the CreateandShowGUI class. 在上面的类中,我将EventHandler类设为静态,然后在CreateandShowGUI类中将其实例化。 Then I added the buttons to the EventHandler (actionlistener). 然后我将按钮添加到EventHandler(actionlistener)。 This was just done rejigging your code. 刚刚完成了您的代码重新编排。 It would be better to have a class that handled all of this in a separate file, that wasn't a static class. 最好有一个类在一个单独的文件中处理所有这些问题,这不是静态类。 So you could instantiate it and make any and all calls to methods you want without them being static. 因此,您可以实例化它,并在不使其成为静态的情况下对所需方法进行任何和所有调用。

That's all the help I can give for now. 这就是我现在可以提供的所有帮助。

You should use the MVC method of development, 您应该使用MVC开发方法,

Your Business Logic is at the bottom accessed from Services that the Gui can call, then you can put a website/swing/any other view system on the top more or less accessing an API into your business logic 您可以从Gui可以调用的服务访问您的业务逻辑的底部,然后您可以在顶部放置一个网站/ Swing /任何其他视图系统,或多或少地将API接入您的业务逻辑

AppointmentService.addAppointment(Appointment appointment); AppointmentService.addAppointment(预约约会); AppointmentService.getAppointments(); AppointmentService.getAppointments();

etc 等等

Start with a simple command line interface (CLI) class. 从简单的命令行界面(CLI)类开始。

Just code a class with a main() method that exercises AppointmentBook. 只需使用练习AppointmentBook的main()方法编写一个类即可。 Once you understand how AppointmentBook works, return to your GUI. 了解了AppointmentBook的工作原理后,请返回到GUI。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM