根据用户输入计算星期几

时间:2014-01-31 01:34:31

标签: java swing calendar

我无法弄清楚我的程序如何根据月,日,年的用户输入计算星期几。如下图。我不知道如何使它与JTextField和JComboBoxes连接。我非常感谢任何人对此的帮助。我知道我应该使用:

Calendar xmas = new GregorianCalendar(1998,Calendar.DECEMBER,25); int dayOfWeek = xmas.get(Calendar.DAY_OF_WEEK); // 6 =星期五

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.GregorianCalendar;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JTextField;
import javax.swing.JTextArea;



public class DayOfWeek extends JFrame 
{

    private JPanel contentPane;
    private JTextField yearField;
    private JLabel dayOfW;

    /**
     * Launch the application.
     */
    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                try
                {
                    DayOfWeek frame = new DayOfWeek();
                    frame.setVisible(true);
                } catch (Exception e)
                {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public DayOfWeek()
    {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 230);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel monthL = new JLabel("Month:");
        monthL.setBounds(10, 11, 46, 14);
        contentPane.add(monthL);

        JComboBox monthBox = new JComboBox();
        monthBox.setModel(new DefaultComboBoxModel(new String[] {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}));
        monthBox.setBounds(53, 8, 109, 20);
        contentPane.add(monthBox);

        JLabel dayL = new JLabel("Day: ");
        dayL.setBounds(172, 11, 31, 14);
        contentPane.add(dayL);

        JComboBox dayBox = new JComboBox();
        dayBox.setModel(new DefaultComboBoxModel(new String[] {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31"}));
        dayBox.setBounds(203, 8, 53, 20);
        contentPane.add(dayBox);

        JLabel yearL = new JLabel("Year: ");
        yearL.setBounds(266, 11, 37, 14);
        contentPane.add(yearL);

        yearField = new JTextField();
        yearField.setBounds(301, 8, 86, 20);
        contentPane.add(yearField);
        yearField.setColumns(10);



        JLabel dayOfW;
        dayOfW = new JLabel("The day of the week is");
        dayOfW.setBounds(10, 153, 126, 14);
        contentPane.add(dayOfW);

        JTextArea textArea = new JTextArea();
        textArea.setBounds(172, 148, 160, 19);
        contentPane.add(textArea);

        Calendar xmas = new GregorianCalendar(1998, Calendar.DECEMBER, 25);
        int dayOfWeek = xmas.get(Calendar.DAY_OF_WEEK); // 6=Friday




    yearField.addActionListener(new ActionListener() 
    {

        @Override
        public void actionPerformed(ActionEvent e)
        {

        }
    });
}
}

非常感谢

3 个答案:

答案 0 :(得分:2)

您有多种选择......

你可以

在每个字段上使用FocusListener,并在调用focusLost时验证每个字段的状态,以确定您是否有足够的信息来执行所需的计算......但这不需要考虑如果用户只是在最后一个字段上按 Enter 会发生什么......

你可以

在每个字段上使用ActionListener来检测用户何时按 Enter (或处理各个字段)并验证每个字段的状态以确定您是否有足够的信息执行所需的计算,但这并未考虑用户未在字段上按 Enter 的情况

你可以

同时使用FocusListenerActionListener,只需调用验证每个字段状态的方法,以确定您是否有足够的信息来执行所需的计算

你可以

要求用户按下JButton才能执行计算。激活后,它将验证每个字段的状态,以确定您是否有足够的信息来执行所需的计算

看看:

您甚至可以使用InputVerifier,有关详细信息,请参阅Validating Input

现在,说了这么多,你的主要问题是参考问题。

所有字段都在构造函数的本地声明。虽然您可以将它们声明为final并且您可以从ActionListener的内部类中访问它们,但我个人会将它们声明为实例变量,以便您可以从班级本身的任何地方。这为您提供了更大的机会来根据需要实现逻辑。

答案 1 :(得分:1)

不要在swing组件上使用awt侦听器。使用DocumentListener调出swing,它将监听字段文本更改。

yearField.getDocument().addDocumentListener(new DocumentListener() {
public void changedUpdate(DocumentEvent e) {
  calc();
}
public void removeUpdate(DocumentEvent e) {
  calc();
}
public void insertUpdate(DocumentEvent e) {
  calc();
}

public void calc() {
   //get values from fields
   //pass them to gregorian calendar constructor
   //get day of week from calendar
   //print day to label
}
});

答案 2 :(得分:1)

试试这个:

yearField.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent arg0) {

        //check the value of the other input fields
        //do the calculations

        textArea.setText("the result here");    
    }

});