get set Text from one JFrame to another pop up JFrame with a textArea

柔情痞子 提交于 2019-12-14 04:23:07

问题


I need to take all the textField, comboBox, jlst, and jslider input from the first frame and make it into a summary in the second frame after the Submit button is picked. I can't figure out the best way to do this. My assignment requires a popup second frame with a textArea and exit button that brings you back to the first frame.

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
import java.util.Hashtable;

public class Ch10Asg extends JFrame
{
private String[] flightNames = {"342", "4324", "939", "104", "222"};
private String[] cardTypes ={"Mastercard", "Visa", "American Express", "Discover"};
private String[] Dates ={"8/1/2013", "8/2/2013", "8/3/2013", "8/4/2013", "8/5/2013"};
private JTextField jtfFirst = new JTextField(10); 
private JTextField jtfLast = new JTextField(10);
private JTextField jtfAddress = new JTextField(15);
private JTextField jtfCity = new JTextField(15); 
private JTextField jtfState = new JTextField(2);
private JTextField jtfZip = new JTextField(5);
private JTextField jtfCard = new JTextField(16);
private JComboBox jcbo = new JComboBox(flightNames);
private JComboBox jcbcctype = new JComboBox(cardTypes);
private JList jlst = new JList(Dates);
private JSlider jsldHort = new JSlider(JSlider.HORIZONTAL,0,4,0);
private JButton jbtExit = new JButton ("EXIT");
private JButton jbtClear = new JButton ("CLEAR");
private JButton jbtSubmit = new JButton ("SUBMIT");
private JTextField jtfStart = new JTextField();
private JTextField jtfEnd = new JTextField();

public Ch10Asg() 
{
    JPanel p1 = new JPanel(new GridLayout(3,1));
    p1.add(new JLabel("First Name")); 
    p1.add(jtfFirst); 
    p1.add(new JLabel("Last Name")); 
    p1.add(jtfLast); 
    p1.add(new JLabel("Address")); 
    p1.add(jtfAddress); 
    p1.add(new JLabel("City")); 
    p1.add(jtfCity); 
    p1.add(new JLabel("State"));
    p1.add(jtfState);
    p1.add(new JLabel("ZIP")); 
    p1.add(jtfZip);

    JPanel p2 = new JPanel(new GridLayout(3,1));
    p2.add(new JLabel("Select Flight Number"));
    p2.add(jcbo);
    p2.add(new JLabel("Select Date"));
    p2.add(jlst);
    p2.add(new JLabel("Select Time"));

    jsldHort.setMajorTickSpacing(1);
    jsldHort.setPaintTicks(true);

    Hashtable<Integer, JLabel> labels = new Hashtable<Integer, JLabel>();
    labels.put(0, new JLabel("4:00am"));
    labels.put(1, new JLabel("5:00am"));
    labels.put(2, new JLabel("8:00am"));
    labels.put(3, new JLabel("11:00am"));
    labels.put(4, new JLabel("5:00pm"));
    jsldHort.setLabelTable(labels);
    jsldHort.setPaintLabels(true);
    p2.add(jsldHort);

    JPanel p4 = new JPanel(new GridLayout(3,1));
    p4.add(new JLabel("Starting City"));
    p4.add(jtfStart);
    p4.add(new JLabel("Ending City"));
    p4.add(jtfEnd);
    p4.add(new JLabel("Select Card Type"));
    p4.add(jcbcctype);
    p4.add(new JLabel("Enter Number"));
    p4.add(jtfCard);
    p4.add(jbtExit);
    p4.add(jbtClear);
    p4.add(jbtSubmit);



    add(p1, BorderLayout.NORTH);
    add(p2, BorderLayout.CENTER);
    add(p4, BorderLayout.SOUTH);




    jbtExit.addActionListener(new ActionListener()
    {@Override
        public void actionPerformed(ActionEvent arg0) {
            // TODO Auto-generated method stub
            System.exit(0);

        }});

    jbtClear.addActionListener(new ActionListener()
    {@Override
        public void actionPerformed(ActionEvent arg0) {
            // TODO Auto-generated method stub
        jtfFirst.setText("");
        jtfLast.setText(""); 
        jtfAddress.setText("");
        jtfCity.setText(""); 
        jtfState.setText(""); 
        jtfZip.setText(""); 
        jtfCard.setText("");
        }});



    jbtSubmit.addActionListener(new ActionListener()
    {@Override
        public void actionPerformed(ActionEvent arg0) {
            new Infopane(jtfFirst.getText());
            //dispose();
        }});





}//ENDOFCONSTRUCTOR

public static void main(String[] args) 
{
    Ch10Asg frame = new Ch10Asg();
    frame.pack();
    frame.setLocationRelativeTo(null); // Center the frame
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setTitle("Flights");
    frame.setVisible(true); 
    frame.setSize(700,450);


}//ENDOFMAIN

}//ENDOFCLASS




class Infopane extends JFrame
{

private JTextArea Info = new JTextArea();
private JButton jbtExit1 = new JButton ("EXIT");


public Infopane(String jtfFirst)
{
JPanel s1 = new JPanel();
add(Info, BorderLayout.NORTH);
//Info.setFont(new Font("Serif", Font.PLAIN, 14));
Info.setEditable(false);
JScrollPane scrollPane = new JScrollPane(Info);
//setLayout(new BorderLayout(5,5));
add(scrollPane);
add(jbtExit1, BorderLayout.SOUTH);


pack();
setLocationRelativeTo(null); // Center the frame
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Flight Summary");
setVisible(true);   
setSize(700,450);

jbtExit1.addActionListener(new ActionListener()
{@Override
    public void actionPerformed(ActionEvent arg0) {
        System.exit(0);
    }});




}


}

回答1:


Suggestions:

  • The second window shouldn't be a second JFrame but rather a modal JDialog since it truly is a dialog of the first window. This way, you don't have to fret about the second window closing the entire application when it is closed.
  • You already have an idea of what you should be doing -- passing information from one object to another -- since you're passing the String held by the first window's first name's JTextField into the second class: jtfFirst.getText()
  • Instead of passing just one JTextField's text, consider creating a third data class that holds all the information that you want to pass from one class to the other, create an object of this in the submit's ActionListener, and pass it into the new class.
  • Alternatively, you could give your first class getter methods, and just pass an instance of the first object into the second object, allowing the second object to extract info from the first by calling its getter methods.

Edit

For example here are snippets of a code example that works. The main JPanel is called MainPanel:

class MainPanel extends JPanel {

   // my JTextFields
   private JTextField fooField = new JTextField(10);
   private JTextField barField = new JTextField(10);

   public MainPanel() {
      add(new JLabel("Foo:"));
      add(fooField);
      add(new JLabel("Bar:"));
      add(barField);
      add(new JButton(new AbstractAction("Submit") {
         {
            putValue(MNEMONIC_KEY, KeyEvent.VK_S);
         }
         @Override
         public void actionPerformed(ActionEvent evt) {
            DialogPanel dialogPanel = new DialogPanel(MainPanel.this);

            // code deleted....
            // create and show a JDialog here
         }
      }));
      add(new JButton(new AbstractAction("Exit") {
         {
            putValue(MNEMONIC_KEY, KeyEvent.VK_X);
         }
         @Override
         public void actionPerformed(ActionEvent e) {
            Window win = SwingUtilities.getWindowAncestor((JButton)e.getSource());
            win.dispose();
         }
      }));
   }

   // getter methods to get information held in the fields.
   public String getFooText() {
      return fooField.getText();
   }

   public String getBarText() {
      return barField.getText();
   }
}

And in the dialog panel:

class DialogPanel extends JPanel {
   private JTextArea textarea = new JTextArea(10, 15);
   private MainPanel mainPanel;

   public DialogPanel(MainPanel mainPanel) {
      this.mainPanel = mainPanel; // actually don't even need this in your case

      // extract the information from the origianl class
      textarea.append("Foo: " + mainPanel.getFooText() + "\n");
      textarea.append("Bar: " + mainPanel.getBarText() + "\n");

      add(new JScrollPane(textarea));
      add(new JButton(new AbstractAction("Exit") {
         {
            putValue(MNEMONIC_KEY, KeyEvent.VK_X);
         }
         @Override
         public void actionPerformed(ActionEvent e) {
            Window win = SwingUtilities.getWindowAncestor((JButton)e.getSource());
            win.dispose();
         }
      }));
   }
}


来源:https://stackoverflow.com/questions/17899145/get-set-text-from-one-jframe-to-another-pop-up-jframe-with-a-textarea

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!