Java Swing BorderLayout未正确显示

时间:2017-12-06 09:56:34

标签: java swing jpanel layout-manager border-layout

当我尝试编译源代码并运行程序时,有时一切似乎都很好,有时按钮没有正确显示。

基本上,存在扩展问题。问题的事实是我不明白为什么它工作得很好,有时却没有。您可以在下面看到源代码和图像。

我认为问题出在JpaneBorderLayout上,但无法理解原因。

Working GUI

Not correctly displayed one

class PropertyPanel extends PartPanel {

  private Action actionLoadProp, actionSimProp;
  private JButton buttonLoadProp, buttonSimProp;
  private JTextField fieldProp;
  private PropertiesFile m_propertiesFile;
  private PropertiesList listProp;
  private PropConstPanel mdPp;
  private boolean selectedProp;

  PropertyPanel(Config config) {
    super(config);
    selectedProp = false;
    initActions();
    initGUIComp();

  }

  void initGUIComp() {
    buttonLoadProp = new JButton(actionLoadProp);
    buttonSimProp = new JButton(actionSimProp);
    fieldProp = new JTextField(35);

    JPanel panelMainProp = new JPanel(new BorderLayout(5, 5));
    listProp = new PropertiesList();

    JScrollPane scrollPane = new JScrollPane(listProp);
    panelMainProp.setBorder(BorderFactory.createTitledBorder("Properties"));

    JPanel panelProp = new JPanel(new FlowLayout(FlowLayout.LEADING));
    panelProp.add(fieldProp);
    panelProp.add(buttonLoadProp);
    panelProp.add(buttonSimProp);
    panelMainProp.add(scrollPane, BorderLayout.CENTER);
    panelMainProp.add(panelProp, BorderLayout.SOUTH);

    PropertyPanel.this.add(panelMainProp, BorderLayout.NORTH);
    mdPp = new PropConstPanel();
    PropertyPanel.this.add(mdPp, BorderLayout.CENTER);
  }

  String getProperty(int index) {
    if (m_propertiesFile != null)
      return ((Expression) m_propertiesFile.getProperty(index))
        .toString();

    return "";
  }

  private void initActions() {
    actionLoadProp = new AbstractAction("Load Properties") {

      public void actionPerformed(ActionEvent e) {
        loadProp();

      }

    };

    actionSimProp = new AbstractAction("Simulate") {

      @Override
      public void actionPerformed(ActionEvent arg0) {

        m_config.propId = -1;
        UndefinedConstants consts = new UndefinedConstants(m_config.getModel(), null);

        ConstTableModel modelTable = ExperimentSetup.getInstance()
          .getMf_constModel();
        int size = modelTable.getRowCount();
        try {
          String constName, constValue;
          int type;
          for (int i = 0; i < size; i++) {
            constName = modelTable.getConstant(i).name;
            constValue = "" + modelTable.getConstant(i).value;
            type = consts.getMFUndefinedType(i);
            consts.defineConstant(constName,
              "" + mdPp.parseValue(type, constValue));

          }
          consts.checkAllDefined();
          consts.initialiseIterators();
          m_config.getModel().setUndefinedConstants(
            consts.getMFConstantValues());
        } catch (PrismException e) {
          JOptionPane.showMessageDialog(null, e.getMessage(), "Constant error", JOptionPane.CANCEL_OPTION);
          return;
        }
        consts.initialiseIterators();
        ExperimentSetup.getInstance().simulateCX();
      }
    };

  }
  class PropConstPanel extends ConstPanel {

    private UndefinedConstants unDefConst;

    public PropConstPanel() {
      super("Property Constants");
    }

    public void fillTables() {
      ExperimentSetup setup = ExperimentSetup.getInstance();

      unDefConst = new UndefinedConstants(m_config.getModel(),
        m_propertiesFile);
      for (int i = 0; i < unDefConst.getPFNumUndefined(); i++) {
        modelTableConst.addConstant(unDefConst.getPFUndefinedName(i),
          unDefConst.getPFUndefinedType(i), "");
      }
      setup.enableNext();
    }

    public boolean checkValidity() {
      // return (unDefConst.getPFNumUndefined() > 0) ? false : true;
      return !modelTableConst.isUnDefConstInTable();
    }

    public void parseConstants() {
      ConstTableModel propTableConst = getModelTable();
      ConstTableModel modelTable = ExperimentSetup.getInstance()
        .getMf_constModel();
      int size = modelTable.getRowCount();
      try {
        String constName, constValue;
        int type;
        for (int i = 0; i < size; i++) {
          constName = modelTable.getConstant(i).name;
          constValue = "" + modelTable.getConstant(i).value;
          type = unDefConst.getMFUndefinedType(i);
          unDefConst.defineConstant(constName,
            "" + parseValue(type, constValue));

        }
        size = propTableConst.getRowCount();
        for (int i = 0; i < size; i++) {
          constName = propTableConst.getConstant(i).name;
          constValue = "" + propTableConst.getConstant(i).value;
          type = unDefConst.getPFUndefinedType(i);
          unDefConst.defineConstant(constName,
            "" + parseValue(type, constValue));

        }

        unDefConst.checkAllDefined();
        unDefConst.initialiseIterators();

        m_propertiesFile.setUndefinedConstants(unDefConst
          .getPFConstantValues());
        m_config.getModel().setUndefinedConstants(
          unDefConst.getMFConstantValues());
        m_config.setProp(m_propertiesFile);
      } catch (PrismException e) {
        Registry.getMain().handleWarning(e.getMessage());
      }

    }

  }

  package dipro.run.wizard;

  import java.awt.BorderLayout;

  import javax.swing.JPanel;

  import parser.ast.ModulesFile;
  import parser.ast.PropertiesFile;

  import dipro.run.Config;

  public abstract class PartPanel extends JPanel {

    Config m_config;
    ModulesFile m_modulesFiles;

    public PartPanel(Config config) {
      m_config = config;
      setLayout(new BorderLayout(5, 5));

    }

    abstract boolean isValidConf();

    abstract void parseConstants();


    public Config getConfig() {
      return m_config;
    }
  }

0 个答案:

没有答案