get text将一个JFrame设置为另一个带有textArea的弹出式JFrame

时间:2013-07-27 14:59:08

标签: java swing jframe textarea jtextarea

我需要从第一帧中获取所有textField,comboBox,jlst和jslider输入,并在选择Submit按钮后将其设置为第二帧中的摘要。我无法弄清楚这样做的最佳方法。我的作业需要弹出第二帧,其中包含textArea和退出按钮,可以将您带回到第一帧。

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 个答案:

答案 0 :(得分:2)

建议:

  • 第二个窗口不应该是第二个JFrame而是一个模态JDialog,因为它确实是第一个窗口的对话框。这样,您不必担心关闭整个应用程序时关闭整个应用程序的第二个窗口。
  • 您已经知道应该做什么 - 将信息从一个对象传递到另一个对象 - 因为您将第一个窗口的第一个名称的JTextField保存的String传递给第二个类:jtfFirst.getText()
  • 不要只传递一个JTextField的文本,而是考虑创建第三个数据类,它包含您想要从一个类传递到另一个类的所有信息,在submit的ActionListener中创建一个这样的对象,并将其传递给新的类。
  • 或者,您可以提供第一类getter方法,只需将第一个对象的实例传递给第二个对象,允许第二个对象通过调用其getter方法从第一个对象中提取信息。

修改

例如,这里是一个有效的代码示例的片段。主JPanel称为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();
   }
}

在对话框面板中:

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