-1

因此,如果用户不按下任何按钮,则不会触发动作侦听器,并且最终会出现异常。因此,我想在我的 FrameClass 中放置一个默认字符串,并在单击按钮时更改该字符串,而不是在我的主类中,我执行一个循环,一直循环直到更改默认字符串,所以我认为这是一个无限循环。这样做可以吗?

package gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRootPane;

/**
 * 
 * @author E-TECH
 */
public class ButtonsFrame extends JFrame {

    private JButton ScPerF, weekSc, both,cancel;
    private SchedulePerWeek week;
    private CoursesPerWeek course;
    private JPanel panel;
    private String choice;
    private File file;

    public ButtonsFrame() {
        ScPerF = new JButton("Generate schedule/faculty");
        weekSc = new JButton("Generate weekly class schedule");
        both = new JButton("Generate Both");
        cancel = new JButton("Cancel");
        choice="nothing";

        ScPerF.addActionListener(new ButtonListener());
        weekSc.addActionListener(new ButtonListener());
        both.addActionListener(new ButtonListener());
        cancel.addActionListener(new ButtonListener());
        setResizable(false);
        setUndecorated(true);
        getRootPane().setWindowDecorationStyle(JRootPane.NONE);

        panel = new JPanel();
        panel.add(ScPerF);
        panel.add(weekSc);
        panel.add(both);
        panel.add(cancel);
        getContentPane().add(panel);
        setVisible(true);
        pack();
        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    }

    private class ButtonListener implements ActionListener {

        public void actionPerformed(ActionEvent event) {
            if (event.getSource() == ScPerF) {
                dispose();

                choice = "faculty";

            }
            if (event.getSource() == weekSc) {
                dispose();

                choice = "course";
            }
            if (event.getSource() == both) {
                dispose();
                choice = "both";
            }
            if (event.getSource()==cancel){
                dispose();
                choice="cancel";
            }


        }

    }

    public boolean Activated() {
        return ScPerF.isSelected() || weekSc.isSelected();
    }

    public String getChoice() {
        return choice;
    }

    public File getFile() {
        return file;
    }

}

public class SchedulePerWeek {
    HSSFSheet weekSh,courseSh;
    int instructor_count;
    HSSFWorkbook wb;

    public SchedulePerWeek() {


        ExcelReader reader = new ExcelReader();

        HSSFSheet sh = reader.getSortedSheet();
        String choice=reader.getChoice();

        if(choice.equals("cancel")||choice.equals("nothing")){///i fixed the exception with this condition by closing the program instead of continuing,but i want to wait for the user instead of just exiting the program
            System.exit(1);
}
        wb = new HSSFWorkbook();
/////
///more code
4

1 回答 1

1

我在几次编辑之前运行了您的代码,它在我的 Windows 8 工作站 Java 7 上运行良好。

在进一步进行 GUI 设计之前,请阅读对多个 JFrame 的使用,好/坏做法的答案?

我修改了您的代码以使用 JFrame,而不是扩展一个。只应在覆盖组件方法之一时扩展 Swing 组件。

您只需要定义一次 Button 侦听器。您在按钮上设置监听器。

我将 JFrame 默认关闭操作更改为关闭时退出。

我添加了一个主要方法,以便我可以运行您的代码。

这是更改的代码。

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.SwingUtilities;

/**
 * 
 * @author E-TECH
 */
public class ButtonsFrame{

    private JButton scPerf, weekSc, both, cancel;
    // private SchedulePerWeek week;
    // private CoursesPerWeek course;
    private JFrame  frame;
    private JPanel  panel;
    private String  choice;
    private File    file;

    public ButtonsFrame() {
        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        scPerf = new JButton("Generate schedule/faculty");
        weekSc = new JButton("Generate weekly class schedule");
        both = new JButton("Generate Both");
        cancel = new JButton("Cancel");
        choice = "nothing";

        ButtonListener listener = new ButtonListener();
        scPerf.addActionListener(listener);
        weekSc.addActionListener(listener);
        both.addActionListener(listener);
        cancel.addActionListener(listener);

        frame.setResizable(false);
        frame.setUndecorated(true);
        frame.getRootPane().setWindowDecorationStyle(JRootPane.NONE);

        panel = new JPanel();
        panel.add(scPerf);
        panel.add(weekSc);
        panel.add(both);
        panel.add(cancel);

        frame.getContentPane().add(panel);
        frame.pack();
        frame.setVisible(true);
    }

    private class ButtonListener implements ActionListener {

        public void actionPerformed(ActionEvent event) {
            if (event.getSource() == scPerf) {
                frame.dispose();
                choice = "faculty";
            }
            if (event.getSource() == weekSc) {
                frame.dispose();
                choice = "course";
            }
            if (event.getSource() == both) {
                frame.dispose();
                choice = "both";
            }
            if (event.getSource() == cancel) {
                frame.dispose();
                choice = "cancel";
            }
        }

    }

    public boolean Activated() {
        return scPerf.isSelected() || weekSc.isSelected();
    }

    public String getChoice() {
        return choice;
    }

    public File getFile() {
        return file;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new ButtonsFrame();
            }
        });
    }

}
于 2013-05-25T11:25:54.773 回答