我的应用程序运行良好,但是我需要通过SaveForm
来获得SaveFormController
。我需要在ButtonFunction
中更改ApplicationMainFormController
。
如何通过控制器获取JPanel
?
ApplicationMainFormController:
...
applicationMainForm.getjButtonSaveResult().addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
SaveForm saveForm = new SaveForm(applicationMainForm, rootPaneCheckingEnabled);
saveForm.setVisible(true);
}
});
...
SaveFormController:
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;
}
}
ApplicationMainForm:
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;
}
}
最佳答案
..需要通过SaveForm
获得SaveFormController
。
更改自:
setLocationRelativeTo(null);
对话
在对话框中:
setLocationRelativeTo(parent);
对于演示。其中,请参见Center JDialog over parent
帧
在框架中:
setLocationByPlatform(true);
有关演示,请参见How to best position Swing GUIs?。第二。