试图将JComboBox实现到程序中 - Java

时间:2015-04-28 21:49:51

标签: java swing

这是我的第一篇文章!

我试图将TutCombo程序中的组合框实现到ExamGradesGUI + ExamGrades中。正如你在TutCombo程序中看到的那样,有一个' String subjectUnitTxt'。理想情况下,我希望这可以取代' subjectUnitTxt'在ExamGradesGUI程序中,但具有组合框的功能,并能够与firstName,lastName和examMark一起保存到文件中。如果有人能告诉我如何做到这一点,那就太好了。很抱歉,如果我添加了太多代码。感谢

1 个答案:

答案 0 :(得分:1)

我通过对代码进行一些小的更改来实现这一点(请参阅附带的代码)。搜索“unitCombo”。

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class ExamGradesGUI {
    public static void main(String[] args) {
        new ExamGradesGUI();
    }

    String[] firstName = new String[20];
    String[] lastName = new String[20];
    String[] subjectUnit = new String[20];
    double[] examMark = new double[20];
    private JLabel firstNameLbl, lastNameLbl, unitLbl, markLbl;
    private JTextField firstNameTxt, lastNameTxt, subjectUnitTxt, examMarkTxt;
    private JComboBox<String> unitCombo;
    private JButton btnClear, btnSave, btnOpen, btnExit;
    private JPanel panel;
    private JFrame frame;

    public ExamGradesGUI(){
        buildFrame();
        buildFields();
        buildButtons();
        frame.setVisible(true);
        frame.add(panel);
    }

    public void buildFrame(){
        frame = new JFrame("GradeEnter");
        frame.setSize(650,450);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        panel = new JPanel();
        panel.setLayout(null);
        panel.setBackground(Color.white);
    }

    public void buildFields(){

        // Labels, User Input + Location

        firstNameTxt = new JTextField(10);
        firstNameTxt.setBounds(180, 80, 150, 20);
        panel.add(firstNameTxt);

        String str = firstNameTxt.getText(); 
        if(str.matches("[-a-zA-Z]*"))
        {
        }
        else
        {
            JOptionPane.showMessageDialog(null, "Please enter amount donating");
        }

        lastNameTxt = new JTextField(10);
        lastNameTxt.setBounds(180, 110, 150, 20);
        panel.add(lastNameTxt);

        subjectUnitTxt = new JTextField(10);
        String[] courses = {"Computing","Forensic","Business"};
        unitCombo = new JComboBox<String>(courses);
        //subjectUnitTxt.setBounds(180, 140, 150, 20);
        //panel.add(subjectUnitTxt);
        unitCombo.setBounds(180, 140, 150, 20);
        panel.add(unitCombo);

        // IF HAVE TIME: Turn Combo Box into GUI - Refer to testgui.java

        examMarkTxt = new JTextField(10);
        examMarkTxt.setBounds(180, 170, 150, 20);
        panel.add(examMarkTxt);

        firstNameLbl = new JLabel("First Name:");
        firstNameLbl.setBounds(70, 80, 100, 20);
        panel.add (firstNameLbl);

        lastNameLbl = new JLabel("Last Name:");
        lastNameLbl.setBounds(70, 110, 100, 20);
        panel.add (lastNameLbl);

        unitLbl = new JLabel("Unit:");
        unitLbl.setBounds(70, 140, 100, 20);
        panel.add (unitLbl);

        markLbl = new JLabel("Mark:");
        markLbl.setBounds(70, 170, 100, 20);
        panel.add (markLbl);

    }

    public void buildButtons() {
        btnClear = new JButton ("Reset Fields");
        btnClear.setBounds(55, 220, 110, 20);
        btnClear.addActionListener(new ClearButtonListener());
        panel.add (btnClear);

        btnSave = new JButton ("Save");
        btnSave.setBounds(155, 220, 70, 20);
        btnSave.addActionListener(new SaveButton());
        panel.add (btnSave);

        btnOpen = new JButton ("Open 'GradeEnter.txt' ");
        btnOpen.setBounds(90, 250, 200, 20);
        btnOpen.addActionListener(new OpenButton());
        panel.add (btnOpen);


        btnExit = new JButton ("Exit");
        btnExit.setBounds(255, 220, 70, 20);
        btnExit.addActionListener(new ExitButton());
        panel.add (btnExit);

    }
    public void setText() {
        firstNameTxt.setText("");
        lastNameTxt.setText("");
        subjectUnitTxt.setText("");
        examMarkTxt.setText("");
    }

    public void getText() {
        int i = 0;
        i++;
        firstName[i] = firstNameTxt.getText();
        lastName[i] = lastNameTxt.getText();
        subjectUnit[i] = unitCombo.getItemAt(unitCombo.getSelectedIndex());
        examMark[i] = Double.parseDouble(examMarkTxt.getText());
    }
    private class ClearButtonListener implements ActionListener {

        public void actionPerformed (ActionEvent e) {
            setText();
        }
    }

    private class SaveButton implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            getText();
            setText();

            ExamGrades save = new ExamGrades();
            save.fileOpen();
            save.addRecords(firstName, lastName, subjectUnit, examMark);
            JOptionPane.showMessageDialog(null, "Entry Saved!");
            save.fileClose();

        }
    }
    private class OpenButton implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            try { 
                JOptionPane.showMessageDialog(null, "'GradeEnter.txt' opening in Java!"); 
                Thread.sleep(2); // Adds a 2 second delay so user can read dialog message
                Runtime.getRuntime().exec("eclipse GradeEnter.txt" );


            } catch (Exception NoFileFound) { 
                System.out.println("Couldn't open or find the file.");
            } 
        }
    }

    class ExitButton implements ActionListener{
        public void actionPerformed(ActionEvent e) {
            int n = JOptionPane.showConfirmDialog(frame, 
                    "Are you sure you want to exit?", 
                    "Exit?", 
                    JOptionPane.YES_NO_OPTION);
            if(n == JOptionPane.YES_OPTION){
                System.exit(0);
            }
        }
    }

}