将设置的Text从一个JFrame设置到另一个带有textArea的JFrame [英] get set Text from one JFrame to another pop up JFrame with a textArea

查看:87
本文介绍了将设置的Text从一个JFrame设置到另一个带有textArea的JFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从第一帧中获取所有textField,comboBox,jlst和jslider输入,并在选中提交"按钮后将其输入到第二帧中的摘要中.我想不出做到这一点的最佳方法.我的作业需要弹出第二个框架,并带有textArea和退出按钮,以将您带回到第一个框架.

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




}


}

推荐答案

建议:

  • 第二个窗口不应该是第二个JFrame,而应该是模式JDialog,因为它确实是第一个窗口的对话框.这样,您不必为关闭整个应用程序时关闭的第二个窗口而烦恼.
  • 您已经知道应该怎么做-将信息从一个对象传递到另一个对象-因为您要将第一个窗口的名字JTextField持有的String传递给第二个类:jtfFirst.getText()
  • 考虑创建第三个数据类,而不是仅传递一个JTextField的文本,该数据类包含要从一个类传递到另一个类的所有信息,在提交的ActionListener中创建此对象,然后将其传递给新的课.
  • 或者,您可以提供第一个类的getter方法,然后将第一个对象的实例传递给第二个对象,从而允许第二个对象通过调用其getter方法从第一个对象中提取信息.
  • 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.

修改

例如,这里是有效的代码示例的摘要.主要的JPanel称为MainPanel:

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

在对话框面板中:

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

这篇关于将设置的Text从一个JFrame设置到另一个带有textArea的JFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆