gpt4 book ai didi

java - 将 JPanel 置于 Controller 之上

转载 作者:行者123 更新时间:2023-11-29 05:44:16 25 4
gpt4 key购买 nike

我的应用程序运行良好,但我需要通过 SaveFormController 获取 SaveForm。我需要更改 ApplicationMainFormController 中的 ButtonFunction

如何通过 Controller 获取JPanel

应用程序主窗体 Controller :

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

保存表单 Controller :

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;
}
}

最佳答案

..need get SaveForm over SaveFormController.

更改自:

setLocationRelativeTo(null);

对话框

在对话框中:

setLocationRelativeTo(parent);

演示。其中,参见 Center JDialog over parent

框架

在框架中:

setLocationByPlatform(true);

参见 How to best position Swing GUIs?一个演示。第二个。

关于java - 将 JPanel 置于 Controller 之上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16384742/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com