有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

预约系统的javaswinggui

有人能告诉我如何将GUI连接到基类吗 我已经编写并设计了gui,但现在需要gui与我创建的另一个类交互

编辑:在我的线程中添加了更新的代码,添加了新的方法,以便在按下按钮时将文本打印到文本字段中。即显示约会,在按钮旁边的文本字段中显示所有约会。 此外,还需要尝试添加一个选项,以便用户使用公历格式输入自己的约会

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

    }

}

共 (3) 个答案

  1. # 1 楼答案

    从一个简单的命令行界面(CLI)类开始

    只需使用main()方法编写一个类,该方法可以练习AppointBook。 一旦您了解了AppointBook的工作原理,请返回GUI

  2. # 2 楼答案

    你应该使用MVC开发方法

    您的业务逻辑位于底部,通过Gui可以调用的服务访问,然后您可以在顶部放置一个网站/swing/任何其他视图系统 或多或少地将API访问到您的业务逻辑中

    任命服务。预约; 预约服务。获取约会()

    等等

  3. # 3 楼答案

    你已经有了大部分代码。在ControllerGUI中有一个AppointmentBook类的实例

    AppointmentBook appBook = new AppointmentBook();
    

    可以在事件处理程序中使用该appBook对象

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

    我看不到Appointment类的代码,但是你可以在appBook中调用Appointment类的构造函数。添加通话

    例如

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

    如果你有一个构造函数,它为约会接受两个字符串

    编辑:

    在看到附加代码后,我看到有两个main()方法。这是两个独立的项目

    你可以尝试结合这两种主要方法

    而不是在主方法中进行一系列预约。您应该通过单击其中一个按钮来测试添加约会

    公共类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"));
            }
        }
    }
    

    你也可以通过另一个按钮来调用appBook。showAppoints()方法

    不过,增加这样的预约并不理想。所以,先测试一下,然后添加一些方法,允许您传入值

    您根本不需要其他主要方法来实现这一点,只需要使用CreateandShowGUI调用的方法

    编辑2: 约会类中已经有了toString方法。 向AppointmentBook类添加一个getAppointment方法,该方法允许您通过索引获取任何约会,并将该索引作为参数。返回的东西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类设置为静态,然后在CreateandShowGUI类中创建了它的一个实例。然后我将按钮添加到EventHandler(actionlistener)中。这是刚刚完成的重新调整您的代码。最好让一个类在一个单独的文件中处理所有这些,而不是一个静态类。因此,您可以实例化它,并对您想要的方法进行任何和所有调用,而不需要它们是静态的

    这就是我目前能提供的全部帮助