如何使用switch语句重新排列JComboBoxes?

时间:2015-10-20 07:54:49

标签: java swing calendar jcombobox

我正在创建一个日历程序,允许某人从Darian(火星)日历转换为标准Julian日历和反之亦然。我想要做的是能够切换JComboboxes的顺序取决于您想要转换日历的方式。感谢您提供的任何帮助。我对java和编程一般都很陌生。

package com.cw.dc;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Main extends JFrame implements ActionListener {

    private static final long serialVersionUID = 1L;
    String[] calendarSelector = { "Darian(Martian Standard) to Julian", "Julian(Euro/Amero Standard) to Darian" };
    JComboBox<String> csList = new JComboBox<>(calendarSelector);
    JLabel csText = new JLabel();
    static String[] darianMonths = { "Sagittarius", "Dhanus", "Carpicornus", "Makara", "Aquarius", "Kumbha", "Pisces", "Mina",
            "Aries", "Mesha", "Taurus", "Rishabha", "Gemini", "Mithuna", "Cancer", "Karka", "Leo", "Simha", "Virgo",
            "Kanya", "Libra", "Tula", "Scorpius", "Vrishika" };
    static JComboBox<?> darianList = new JComboBox<Object>(darianMonths);
    static JLabel darianText = new JLabel();
    static String[] julianMonths = { "January", "February", "March", "April", "May", "June", "July", "August", "September",
            "October", "November", "December" };
    static JComboBox<?> julianList = new JComboBox<Object>(julianMonths);
    static JLabel julianText = new JLabel();

    public static void main(String[] args) {
        Main fr = new Main();
        fr.setVisible(true);
    }

    public Main() {
        setLayout(new FlowLayout());
        setSize(400, 300);
        setTitle("Darian/Julian Calendar");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Calendar Selector
        csList.setSelectedIndex(1);
        csList.addActionListener(this);
        add(csList);
        add(csText);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == csList) {
            JComboBox<?> cb = (JComboBox<?>) e.getSource();
            String msg = (String) cb.getSelectedItem();
            switch (msg) {
            case "Julian(Euro/Amero Standard) to Darian":
                //csText.setText("1");
                    JtoD();
                break;
            case "Darian(Martian Standard) to Julian":
                //csText.setText("2");
                    DtoJ();
                break;
            default:
                csText.setText("Error");
            }
        }
    }

    private static void JtoD() {
        // Julian List
        julianList.setSelectedIndex(0);
        julianList.addActionListener(julianList);
        (julianList).setSelectedItem(julianList);
        (julianText).setLabelFor(julianText);

        // Darian List
        darianList.setSelectedIndex(0);
        darianList.addActionListener(darianList);
        (darianList).setSelectedItem(darianList);
        (darianText).setLabelFor(darianText);

        return;
    }

    private static void DtoJ() {
        // Darian List
        darianList.setSelectedIndex(0);
        darianList.addActionListener(darianList);
        (darianList).setSelectedItem(darianList);
        (darianText).setLabelFor(darianText);

        // Julian List
        julianList.setSelectedIndex(1);
        julianList.addActionListener(julianList);
        (julianList).setSelectedItem(julianList);
        (julianText).setLabelFor(julianText);

        return;
    }
}

0 个答案:

没有答案