1

我的应用程序运行良好,但我需要SaveForm克服SaveFormController。我需要改变ButtonFunctionApplicationMainFormController

如何克服JPanel控制器?

应用程序主窗体控制器:

...
    applicationMainForm.getjButtonSaveResult().addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent arg0) {
                    SaveForm saveForm = new SaveForm(applicationMainForm, rootPaneCheckingEnabled);     
                    saveForm.setVisible(true);
                }
            });
...

保存表单控制器:

package GUI;

public class SaveFormController {

    private SaveForm saveForm;

    public SaveFormController() {

    }

    public SaveForm getSaveForm(){
        return saveForm;
    }
}

保存表格:

package GUI;

import javax.swing.plaf.DimensionUIResource;

public class SaveForm extends javax.swing.JDialog{

    //ApplicationMainForm applicationMainForm;

    public SaveForm(javax.swing.JFrame parent, boolean modal) {
        super(parent, modal);
        setTitle(LocalizationUtils.getLocalizedString(LocalizationUtils.JDIALOG_SAVE_RESULT_KEY));
        setDefaultCloseOperation(this.DISPOSE_ON_CLOSE);
        setSize(new DimensionUIResource(111, 111));
        setLocationRelativeTo(null);
       // applicationMainForm = (ApplicationMainForm) parent;
    }
}

应用程序主窗体:

package GUI;

import java.awt.Dimension;
import java.awt.FlowLayout;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;


import GUI.LocalizationUtils;;

public class ApplicationMainForm extends JFrame{

    //private JFrame jFrame;
    private JPanel jPanel;
    private JButton jButton;
    private JButton jButtonSaveResult;
    private JLabel jLabelImage;
    private JTextArea jTextArea;
    private Box box;
    private ImageIcon imageIcon;

    public ApplicationMainForm() {

        jPanel = new JPanel();
        box = new Box(BoxLayout.Y_AXIS);
        jButton = new JButton(LocalizationUtils.getLocalizedString(LocalizationUtils.JBUTTON_RANDOM_KEY));
        jButtonSaveResult = new JButton(LocalizationUtils.getLocalizedString
                (LocalizationUtils.JBUTTON_SAVE_RESULT_KEY));
        jLabelImage = new JLabel("f");
        jTextArea = new JTextArea(18,22);


        setTitle(LocalizationUtils.getLocalizedString(LocalizationUtils.JFRAME_KEY));
        setLocationRelativeTo(null);
        setDefaultCloseOperation(this.EXIT_ON_CLOSE);
        setMinimumSize(new Dimension(250,500));
        setLayout(new FlowLayout());
    //  jPanel.setLayout(new BoxLayout(jPanel, BoxLayout.Y_AXIS));
    //  jPanel.add(jLabelImage);


        add(jPanel);        
        jPanel.add(box);            

        jLabelImage.setAlignmentX(JComponent.CENTER_ALIGNMENT);  
        jButton.setAlignmentX(JComponent.CENTER_ALIGNMENT);
        jTextArea.setAlignmentX(JComponent.CENTER_ALIGNMENT);
        jButtonSaveResult.setAlignmentX(JComponent.CENTER_ALIGNMENT);

        box.add(Box.createVerticalGlue());                          
        box.add(jLabelImage);                       
        box.add(Box.createVerticalGlue());  
        box.add(Box.createRigidArea(new Dimension(0,5)));    //space
        box.add(jButton);                           
        box.add(Box.createVerticalGlue());  
        box.add(Box.createRigidArea(new Dimension(0,5)));    //space
        box.add(jTextArea);
        box.add(Box.createVerticalGlue());      
        box.add(Box.createRigidArea(new Dimension(0,5)));    //space
        box.add(jButtonSaveResult);
        box.add(Box.createVerticalGlue());

        pack();
        setVisible(true);
    }

    public void setImage(String path){
        imageIcon = new ImageIcon(path);
        jLabelImage.setText("");
        jLabelImage.setIcon(imageIcon);
    }

    public void setTextOnjTextArea(String numberOfRandom, String numberOfRandomChaos, 
            String numberOfRandomSoul, String numberOfRandomBless, String numberOfRandomZen,
            String sumZen, String numberOfRandomEmpty, String membershipPercentOfChaos,
            String membershipPercentOfSoul, String membershipPercentOfBless, String membershipPercentOfZen,
            String membershipPercentOfEmpty){
                jTextArea.setText("Chance to random Chaos: " + membershipPercentOfChaos + "%" + "\n" +
                "Chance to random Soul: " + membershipPercentOfSoul + "%" + "\n" +
                "Chance to random Bless: " + membershipPercentOfBless + "%" + "\n" +
                "Chance to random Zen: " + membershipPercentOfZen + "%" + "\n" +
                "Chance to random Empty: " + membershipPercentOfEmpty + "%" + "\n" + "\n" +
                "Number of random: " + numberOfRandom + "\n" +
                "Number of random Chaos: " + numberOfRandomChaos + "\n" +
                "Number of random Soul: " + numberOfRandomSoul + "\n" +
                "Number of random Bless: " + numberOfRandomBless + "\n" +
                "Number of random zen: " + numberOfRandomZen + "\n" +
                "Sum zen: " + sumZen + "\n" +
                "Number of random empty: " + numberOfRandomEmpty + "\n");
    }

    public JButton getjButton(){
        return jButton;
    }
    public JButton getjButtonSaveResult(){
        return jButtonSaveResult;
    }

    public JTextArea getjTextArea() {
        return jTextArea;
    }
}
4

1 回答 1

3

..需要克服SaveFormSaveFormController

更改自:

setLocationRelativeTo(null);

对话

在对话框中:

setLocationRelativeTo(parent);

对于演示。其中,请参阅Center JDialog over parent

框架

在框架中:

setLocationByPlatform(true);

请参阅如何以最佳方式定位 Swing GUI?演示。第二个。

于 2013-05-05T13:00:02.360 回答