Java 从自定义JDialog输出数据或结果更新JFrame中的JTextArea文本
我在主布局中有一个Java 从自定义JDialog输出数据或结果更新JFrame中的JTextArea文本,java,swing,jpanel,jtextarea,jdialog,Java,Swing,Jpanel,Jtextarea,Jdialog,我在主布局中有一个JTextArea,我想使用自定义JDialog更新它。很难用这样的话来解释…所以请按照下面概述的代码和解释过程进行操作: 我有一个JPanel,其中包含需要更新的JTextArea(即原始JTextArea—“cancellationPolicTA”): public class Panel_Other_Information extends JPanel { private JTextArea cancellationPolicyTA, otherInformationT
JTextArea
,我想使用自定义JDialog
更新它。很难用这样的话来解释…所以请按照下面概述的代码和解释过程进行操作:
public class Panel_Other_Information extends JPanel {
private JTextArea cancellationPolicyTA, otherInformationTA;
public final String cancellationPolicyBorderTXT = " Cancellation Policy ";
public final String additionalInformationBorderTXT = " Other Information ";
public Panel_Other_Information () {
// Create and set up the window.
JPanel thisPanel = new JPanel ();
// [1] Define the Dimensions of the Panel.
Dimension panelSize = getPreferredSize();
panelSize.width = 520;
panelSize.height = 100;
setPreferredSize(panelSize);
setBorder(BorderFactory.createTitledBorder(" 15. Additional Information "));
// [2] Use the 'SpringLayout' to set or define the layout of components within this panel.
SpringLayout layout = new SpringLayout();
setLayout(layout);
setBackground(McGyver.APP_THEME_COLOR);
// [3] Define required Label components/controls.
JLabel cancellationPolicyLabel = new JLabel ("Cancellation Policy:");
JLabel otherInformationLabel = new JLabel ("Other Information:");
String canPolTxt = "No Cancellation Policy";
String othInfTxt = "No Additional Information";
// [4] Define required input (TextField) controls/components.
final int widthCB = 230;
final int heightCB = 48;
cancellationPolicyTA = new JTextArea(canPolTxt);
cancellationPolicyTA.setEditable(false);
cancellationPolicyTA.setWrapStyleWord(true);
cancellationPolicyTA.addMouseListener(new CancelPolicyMouseListener());
cancellationPolicyTA.addFocusListener(new CancelPolicyFocusListener());
otherInformationTA = new JTextArea(othInfTxt);
otherInformationTA.setEditable(false);
otherInformationTA.setWrapStyleWord(true);
otherInformationTA.addMouseListener(new OtherInformationMouseListener());
JScrollPane canPolTAScrollPane = new JScrollPane(cancellationPolicyTA);
canPolTAScrollPane.setPreferredSize(new Dimension(widthCB, heightCB));
JScrollPane otherInfoTAScrollPane = new JScrollPane(otherInformationTA);
otherInfoTAScrollPane.setPreferredSize(new Dimension(widthCB, heightCB));
// [5] Define button controls - if needed.
/* JButton saveDataBTN = new JButton("Save"); */
////////////////////////////////////////////////////////////////////////
// Define the layout of components - component-by-component //
////////////////////////////////////////////////////////////////////////
/* -- Component 1 - Additional Information - Cancellation Policy Label */
layout.putConstraint(SpringLayout.NORTH, cancellationPolicyLabel, 5, SpringLayout.NORTH, thisPanel);
layout.putConstraint(SpringLayout.WEST, cancellationPolicyLabel, 0, SpringLayout.EAST, thisPanel);
/* -- Component 2 - Additional Information - Cancellation Policy Text Area */
layout.putConstraint(SpringLayout.NORTH, canPolTAScrollPane, 5, SpringLayout.SOUTH, cancellationPolicyLabel);
layout.putConstraint(SpringLayout.WEST, canPolTAScrollPane, 0, SpringLayout.WEST, cancellationPolicyLabel);
/* -- Component 1 - Additional Information - Cancellation Policy Label */
layout.putConstraint(SpringLayout.NORTH, otherInformationLabel, 5, SpringLayout.NORTH, thisPanel);
layout.putConstraint(SpringLayout.WEST, otherInformationLabel, 30, SpringLayout.EAST, canPolTAScrollPane);
/* -- Component 2 - Additional Information - Cancellation Policy Text Area */
layout.putConstraint(SpringLayout.NORTH, otherInfoTAScrollPane, 5, SpringLayout.SOUTH, otherInformationLabel);
layout.putConstraint(SpringLayout.WEST, otherInfoTAScrollPane, 0, SpringLayout.WEST, otherInformationLabel);
// [4] Add Swing components to content pane.
add(cancellationPolicyLabel);
add(canPolTAScrollPane);
add(otherInformationLabel);
add(otherInfoTAScrollPane);
}
private class CancelPolicyMouseListener extends MouseAdapter {
public void mouseClicked(MouseEvent e){
if(e.getClickCount() == 1){
String cancelPolicyText = cancellationPolicyTA.getText();
if (cancelPolicyText.length() > 0) {
String dialogTitle = "15. Additional Information";
String borderTitle = cancellationPolicyBorderTXT;
McGyver.popCustomDialogTextAreaObject(dialogTitle, borderTitle, cancelPolicyText);
}
}
}
} }
public class Stan_App extends JFrame {
// [1] Instantiate all SWING components needed for this application.
public static Stan_App stanFrame;
private McGyver mcGyver = new McGyver();
public Panel_Other_Information additionalInformation = new Panel_Other_Information();
public Stan_App (String title) {
super(title);
// [1] Set LayoutManager
SpringLayout layout = new SpringLayout();
setLayout(layout);
setBackground(Color.CYAN);
////////////////////////////////////////////////////////////////////////
// [2] Define the layout of components - component-by-component //
////////////////////////////////////////////////////////////////////////
//-- Component 1 - JPanel - Additional Information --//
layout.putConstraint(SpringLayout.NORTH, additionalInformation, 17, SpringLayout.SOUTH, neighbourhoodRating);
layout.putConstraint(SpringLayout.WEST, additionalInformation, 20, SpringLayout.EAST, inRoomFacilities);
// [3] Add Swing components to content pane.
Container conTain = getContentPane();
conTain.add(additionalInformation);
}
public static void main (String[] args) {
SwingUtilities.invokeLater (new Runnable() {
@Override
public void run() {
createStanAppGUI();
}
});
}
private static void createStanAppGUI () {
// Override the default Font for the application.
McGyver.setUIFont(McGyver.APP_GLOBAL_FONT);
// This code chuck lays out the visual components (GUI) of the app.
stanFrame = new Stan_App (" S.T.A. Namibia - Database Manager " + " | THEME: " + McGyver.currentlySelectedAppThemeColor + " |");
// UIManager.getLookAndFeelDefaults().put("defaultFont", new Font("Noto Sans", Font.BOLD, 42));
stanFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
stanFrame.setSize(1925, 1050);
stanFrame.setResizable(false);
stanFrame.setVisible(true);
themeColorCB.setSelectedItem(McGyver.currentlySelectedAppThemeColor);
}
public void setCancellationTextFromCustomPopupDialog (String suppliedText) {
additionalInformation.setCancellation_Policy_TextArea_Value(suppliedText);
} }
public class McGyver {
public static JDialog custPopupDialog;
public static void popCustomDialogTextAreaObject (String dialogTitle, String borderTitle, String inputString) {
Panel_Custom_Dialog custDial = new Panel_Custom_Dialog(Stan_App.stanFrame, borderTitle, inputString);
final int widthCB = 500;
final int heightCB = 400;
custPopupDialog = new JDialog();
custPopupDialog.setTitle(dialogTitle);
custPopupDialog.add(custDial);
custPopupDialog.setSize(new Dimension(widthCB, heightCB)); /* Size(550, 450); */
custPopupDialog.setModal(true);
custPopupDialog.setLocationRelativeTo(null);
custPopupDialog.setVisible(true);
} }
public class Stan_App extends JFrame {
// [1] Instantiate all SWING components needed for this application.
public static Stan_App stanFrame;
private McGyver mcGyver = new McGyver();
public Panel_Other_Information additionalInformation = new Panel_Other_Information();
public Stan_App (String title) {
super(title);
// [1] Set LayoutManager
SpringLayout layout = new SpringLayout();
setLayout(layout);
setBackground(Color.CYAN);
////////////////////////////////////////////////////////////////////////
// [2] Define the layout of components - component-by-component //
////////////////////////////////////////////////////////////////////////
//-- Component 1 - JPanel - Additional Information --//
layout.putConstraint(SpringLayout.NORTH, additionalInformation, 17, SpringLayout.SOUTH, neighbourhoodRating);
layout.putConstraint(SpringLayout.WEST, additionalInformation, 20, SpringLayout.EAST, inRoomFacilities);
// [3] Add Swing components to content pane.
Container conTain = getContentPane();
conTain.add(additionalInformation);
}
public static void main (String[] args) {
SwingUtilities.invokeLater (new Runnable() {
@Override
public void run() {
createStanAppGUI();
}
});
}
private static void createStanAppGUI () {
// Override the default Font for the application.
McGyver.setUIFont(McGyver.APP_GLOBAL_FONT);
// This code chuck lays out the visual components (GUI) of the app.
stanFrame = new Stan_App (" S.T.A. Namibia - Database Manager " + " | THEME: " + McGyver.currentlySelectedAppThemeColor + " |");
// UIManager.getLookAndFeelDefaults().put("defaultFont", new Font("Noto Sans", Font.BOLD, 42));
stanFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
stanFrame.setSize(1925, 1050);
stanFrame.setResizable(false);
stanFrame.setVisible(true);
themeColorCB.setSelectedItem(McGyver.currentlySelectedAppThemeColor);
}
public void setCancellationTextFromCustomPopupDialog (String suppliedText) {
additionalInformation.setCancellation_Policy_TextArea_Value(suppliedText);
} }
public class McGyver {
public static JDialog custPopupDialog;
public static void popCustomDialogTextAreaObject (String dialogTitle, String borderTitle, String inputString) {
Panel_Custom_Dialog custDial = new Panel_Custom_Dialog(Stan_App.stanFrame, borderTitle, inputString);
final int widthCB = 500;
final int heightCB = 400;
custPopupDialog = new JDialog();
custPopupDialog.setTitle(dialogTitle);
custPopupDialog.add(custDial);
custPopupDialog.setSize(new Dimension(widthCB, heightCB)); /* Size(550, 450); */
custPopupDialog.setModal(true);
custPopupDialog.setLocationRelativeTo(null);
custPopupDialog.setVisible(true);
} }
请参阅使用注释更新的部分代码 首先从Panel\u Custom\u Dialog类中删除stanAPP=new Stan\u App(“”);并传递对它的引用,因为它应该在其他地方实例化
McGyver.popCustomDialogTextAreaObject(dialogTitle, borderTitle, cancelPolicyText);
//* the popCustomDialog is Modal, this code above should block until the user hits ok or cancel,
String updatedText = McGyver.getInputString();
cancellationPolicyTA.setText(updatedText);
在Mcgyver类和PopCustomDialogTextArea对象中,您需要返回更新文本的方法。请参阅下面添加的带注释的方法
public class McGyver {
public static JDialog custPopupDialog;
public static Panel_Custom_Dialog custDial
public static void popCustomDialogTextAreaObject (String dialogTitle, String borderTitle, String inputString) {
custDial = new Panel_Custom_Dialog(borderTitle, inputString);
final int widthCB = 500;
final int heightCB = 400;
custPopupDialog = new JDialog();
custPopupDialog.setTitle(dialogTitle);
custPopupDialog.add(custDial);
custPopupDialog.setSize(new Dimension(widthCB, heightCB)); /* Size(550, 450); */
custPopupDialog.setModal(true);
custPopupDialog.setLocationRelativeTo(null);
custPopupDialog.setVisible(true);
}
//* add a method that will return the updated string.
//* for this to work, Panel_Custom_Dialog should have a method that will return the updated text
public static string getInputString()
{
return custDial.getInputString();
}
}
我在你的McGyver类中没有看到返回更新文本的方法来更新cancellationPolicyTA。custPopupDialog是模态的,所以你应该阻止它,直到用户点击ok和cancel,然后你才能返回更新文本Faljbour,你能用一个代码示例解释一下吗,因为我发现很难理解你说的话。我添加了代码示例,如果仍然不清楚,请告诉我您不需要其他方法来返回更新的文本您可以保留getCust_Dialog_TextArea_值或GetInputStringThank,Faljbour…我意识到-稍后会清理它-只是先将您的代码与我的代码进行比较。但这仍然不能解决问题。有可能吗ps其他一些我做得不对的事情,阻止或否定了原始JTextArea中文本的更新?我尝试了您的建议Faljbour,但它仍然不起作用。单击“面板自定义对话框”中的确认按钮后,有一个最终的处置方法。这不就是消除从“面板自定义”传递的所有数据吗_对话框“?如果是,则调用”custDial.getInputString()'不会产生任何数据,是吗?我对此不太清楚…请提供建议。谢谢这只是一个想法哦,你应该如何设计代码,你需要一个方法从面板的自定义对话框返回修改过的更新文本,该文本应该传递回cancellationPolicyTA。你添加了该方法,你可能想显示面板的代码_自定义对话框。为了回答您的问题,是的,它将生成数据。Faljbour,我现在添加了“Panel\u Custom\u Dialog”代码。根据您添加的其他代码,即使在返回并处理对话框后,它也应该可以正常工作。