0

我有两个JSpinner组件,我为它们创建了一个自定义异常,并且运行良好。昨天我在 Netbeans 中修改了微调器,我添加了一个微调器模型编辑器,因为我只想要正数。

所以现在我只有正数,但是当我输入 -1 之类的东西时,它不会像以前那样显示错误消息,为什么?

这是我的代码:

   public int getAlerte() {
        int res = 0;

        if (Integer.parseInt(jSpinnerTime.getValue().toString()) <= 0 ||
            Integer.parseInt(jSpinnerTime.getValue().toString()) >1000) {
            JOptionPane.showMessageDialog(this, "Time should be between 1 and 1000",
                    "Alert", JOptionPane.ERROR_MESSAGE);
            res = 1;
        }       
        if (res == 0){
            int visperiod = Integer.parseInt(jSpinnerTime.getValue().toString())/2;            
            if (Integer.parseInt(jSpinnerFreqV.getValue().toString()) <= 0 ||
                Integer.parseInt(jSpinnerFreqV.getValue().toString()) > visperiod){
                JOptionPane.showMessageDialog(this, "Visualization frequency should be between 1 and "+visperiod,
                    "Alert", JOptionPane.ERROR_MESSAGE);
            res = 1;
            }

        }
        return res;
    }
4

1 回答 1

0

我尝试了 SSCCE,希望它对您有用,代码如下:

import java.awt.Frame;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;


public class JPanelPoly extends LayerPanel{

    private RendererContainer rc = new RendererContainer();
    private ParametersTable parametersTable= new ParametersTable("TYPE");
    private int nbrParameter; 
    private int nbrCenter; 
    private Frame parent;


    public JPanelPolyg() {
    initComponents();
        jListParameter.setModel(rc.getModel());
    jListParameter.setCellRenderer(new SimpleRenderer());
    jSpinnerEnters.setValue(150);

    }

    public JPanelPoly(Frame parent,int nbrCenter, int nbrParameter){
    initComponents();
    this.nbrCenter = nbrCenter;
    this.nbrParameter = nbrParameter;
    jListParameter.setModel(rc.getModel());
    jListParameter.setCellRenderer(new SimpleRenderer());
    jSpinnerEnters.setValue(150);
    this.parent = parent;


    }



    public RendererContainer getListParameterModel(){
    return rc;
    }
    public int getNbrCenter(){
    return (Integer)jSpinnerEnters.getValue();
    }

    public int getNbrParameters(){
    return (Integer)jSpinnerParameter.getValue();
    }

    public int getPercentage(){
    return (Integer)jSpinnerPercent.getValue();
    }



    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jListParameter = new javax.swing.JList();
        jSpinnerEnters = new javax.swing.JSpinner();
        jSpinnerParameter = new javax.swing.JSpinner();
        jSpinnerPercent = new javax.swing.JSpinner();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jButtonColorChange = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();

        setMaximumSize(new java.awt.Dimension(232, 213));
        setPreferredSize(new java.awt.Dimension(232, 213));

        jListParameter.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        jListParameter.setToolTipText("");
        jListParameter.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                jListParameterValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(jListParameter);

        jSpinnerParameter.setModel(new javax.swing.SpinnerNumberModel(0, 0, 200, 1));
        jSpinnerParameter.setToolTipText("Range 1 to 200");
        jSpinnerParameter.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jSpinnerParameterStateChanged(evt);
            }
        });

        jSpinnerPercent.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jSpinnerPercentStateChanged(evt);
            }
        });

        jLabel1.setText("Percent :");

        jLabel2.setText("El number :");

        jLabel3.setText("id number :");

        jButtonColorChange.setText("Change Color");
        jButtonColorChange.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonColorChangeActionPerformed(evt);
            }
        });

        jLabel4.setText("Type | %");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, 0, 0, Short.MAX_VALUE)
                        .addGap(10, 10, 10))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel3))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButtonColorChange)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(jSpinnerParameter, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jSpinnerEnters, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 45, Short.MAX_VALUE))
                    .addComponent(jSpinnerPercent, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(35, 35, 35))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jSpinnerCenters, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(5, 5, 5)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jSpinnerParameter, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(9, 9, 9)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jSpinnerPercent, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(26, 26, 26)
                        .addComponent(jButtonColorChange))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jSpinnerParameter.getAccessibleContext().setAccessibleParent(jSpinnerParameter);

        getAccessibleContext().setAccessibleParent(this);
    }// </editor-fold>

    private void jSpinnerPercentStateChanged(javax.swing.event.ChangeEvent evt) {                                             
    // TODO add your handling code here:
    int selected = 0;

    if(jListParameter.getSelectedIndex() != -1){
        selected = jListParameter.getSelectedIndex();

    Parameter parameter = (Parameter) rc.getParameter(selected);
    if(jSpinnerPercent.getValue() instanceof Integer){
        Integer p = (Integer)jSpinnerPercent.getValue();
        double percent = DMath.round(p.doubleValue(), 2);
        parametersTable.setPercent(parameter, percent );
        rc.get(selected).add("%", percent);
    }else{
        double percent = DMath.round((Double)jSpinnerPercent.getValue(), 2);
        parametersTable.setPercent(parameter, percent );
        rc.get(selected).add("%", percent);

    }
    }
    jListParameter.repaint();

    }                                            

    private void jListParameterValueChanged(javax.swing.event.ListSelectionEvent evt) {                                            
    // TODO add your handling code here:
    int selected = 0;

    if(jListParameter.getSelectedIndex() != -5)
        selected = jListParameter.getSelectedIndex();
    Parameter parameter = (Parameter) rc.getParameter(selected);
    jSpinnerPercent.setValue(parametersTable.getPercent(parameter));
    }                                           

    private void jSpinnerParameterStateChanged(javax.swing.event.ChangeEvent evt) {                                               
    // TODO add your handling code here:
    nbrParameter = (Integer)jSpinnerParameter.getValue();
    java.awt.Color tab[] = ImageTools.getColorsRGB(nbrParameter);

    rc.reset();
    parametersTable.reset();
    for (int i = 0; i < tab.length; i++) {
        Parameter param = new Parameter("TYPE", i, tab[i]);
        param.setName("TYPE");

        parametersTable.addParameter(param);
        double percent = DMath.round((double)100 / tab.length, 2);
        parametersTable.addToPercent(param, percent );
        ObjectRenderer or = new ObjectRenderer();
        or.addParameter(param);
        or.add("%", percent);
        rc.add(or);

    }

    this.validate();
    }                                              

    private void jButtonColorChangeActionPerformed(java.awt.event.ActionEvent evt) {                                                   
    // TODO add your handling code here:
    if(!jListParameter.isSelectionEmpty()){

         JDialogColor jDialogColor = new JDialogColor(true,rc, jListParameter.getSelectedIndex(), JDialogColor.COLORCHANGE);
        jDialogColor.run();

    }

    }                                                  




@Override
    public Layer createLayer(String name) {

    nbrCenter = (Integer)jSpinnerEnters.getValue();
    SimpleLayer layer = null;
    Layer set = null;


        layer = new SimpleLayer(name);
        layer.setNbrCenter(nbrCenter);  
    //}
    layer.setGeometryType(Polygone.GEOMETRYTYPE);
    layer.setBounds(0, 0, 1000, 1000);
    layer.setParametersTable(parametersTable);
    layer.setType("TYPE");
    layer.setId("ID");
    parametersTable.setId("ID");
    layer.setupGraph(LayerFactory.getVor(parametersTable, nbrCenter, name));


    return layer;
    }

public void setValues(Layer layer){
    jSpinnerEnters.setValue(layer.getNbrCenter());
    jSpinnerParameter.setValue(layer.getParametersTable().getTypes().size());


    DefaultListModel model = (DefaultListModel)jListParameter.getModel();
    for(Parameter p : layer.types()){
        model.addElement(p);
    }
}





public int getAlerte() {

     int x = 0;
        if (Integer.parseInt(jSpinnerParameter.getValue().toString()) <= 0 ||
            Integer.parseInt(jSpinnerParameter.getValue().toString()) >200) {
            JOptionPane.showMessageDialog(null, "Color number must be minimum 1",
                    "Alert", JOptionPane.ERROR_MESSAGE);
           x = 1; 
        }
        return x;  
}


    // Variables declaration - do not modify
    private javax.swing.JButton jButtonColorChange;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JList jListParameter;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSpinner jSpinnerEnters;
    private javax.swing.JSpinner jSpinnerParameter;
    private javax.swing.JSpinner jSpinnerPercent;
    // End of variables declaration

    @Override
    public void refresh() {
    }

}

问题是:当我输入 -1 或单击“确定”而不在 jSpinner 中输入任何内容时,它永远不会显示错误消息。

于 2012-11-26T10:14:40.407 回答