JTextField使用JButton输出到新窗口

时间:2014-09-07 15:41:08

标签: java swing jbutton jtextfield

我有一个问题。如果您按下"提交"有可能在JTextField,单选按钮和复选框上输入的所有值都可以打印/显示在新窗口中。按钮?请帮助我了解更多。

我在这里使用Spring Layout制作一个简单的注册表单,因为我觉得它适合它。

这是我的代码:

  import java.awt.Component;
   import java.awt.Container;
   import java.awt.Font;
   import java.awt.BorderLayout;
   //import java.awt.GridLayout;

   import javax.swing.JFrame;
   import javax.swing.JLabel;
   import javax.swing.JTextField;
   import javax.swing.SpringLayout;
   import javax.swing.JRadioButton;
   import javax.swing.ButtonGroup;
   import javax.swing.JButton;
   import javax.swing.JComboBox;
   import javax.swing.JCheckBox;
   import javax.swing.JPanel;

   import java.awt.event.ItemEvent;
   import java.awt.event.ItemListener;



   class SpringSample {
   public static void main(String args[]) {
   JComboBox bDay;
   JComboBox days;
   JComboBox taon;
   JButton butones;

   JFrame frame = new JFrame("Enrollment Form");
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   Container contentPane = frame.getContentPane();

   //JLabel title = new JLabel("Enrollment Registration");

   SpringLayout layout = new SpringLayout();
   contentPane.setLayout(layout);

   JPanel mainPanel = new JPanel(new BorderLayout());

   /*JLabel titles = new JLabel("Enrollan na!");
   Font font = new Font("Agency FB", Font.BOLD, 50);
   titles.setFont(font);
   mainPanel.add(titles, BorderLayout.PAGE_START); */


   //last name
   Component last = new JLabel("Last Name:");
   Component last2 = new JTextField(15);
   contentPane.add(last);
   contentPane.add(last2);
   layout.putConstraint(SpringLayout.WEST, last, 10, SpringLayout.WEST,   
   contentPane);//pweto nugn name
   layout.putConstraint(SpringLayout.NORTH, last2, 25, SpringLayout.NORTH, 
   contentPane);//gaano kataas o kababa yung box
   layout.putConstraint(SpringLayout.NORTH, last, 25, SpringLayout.NORTH, 
   contentPane);//gaano kataas o kababa yung label
   layout.putConstraint(SpringLayout.WEST, last2, 20, SpringLayout.EAST, 
   last);//pwesto nung box

   //first name
   Component first = new JLabel("First Name:");
   Component first2 = new JTextField(15);
   contentPane.add(first);
   contentPane.add(first2);
   layout.putConstraint(SpringLayout.WEST, first, 10, SpringLayout.WEST, contentPane);
   layout.putConstraint(SpringLayout.NORTH, first2, 45, SpringLayout.NORTH, contentPane);
   layout.putConstraint(SpringLayout.NORTH, first, 45, SpringLayout.NORTH, contentPane);
layout.putConstraint(SpringLayout.WEST, first2, 20, SpringLayout.EAST, first);

   //middle
Component middle = new JLabel("M.I:");
Component middle2 = new JTextField(15);
contentPane.add(middle);
contentPane.add(middle2);
layout.putConstraint(SpringLayout.WEST, middle, 10, SpringLayout.WEST, contentPane);
layout.putConstraint(SpringLayout.NORTH, middle, 68, SpringLayout.NORTH, contentPane);
layout.putConstraint(SpringLayout.NORTH, middle2, 65, SpringLayout.NORTH, contentPane);
layout.putConstraint(SpringLayout.WEST, middle2, 65, SpringLayout.EAST, middle);

//gender
Component gender = new JLabel("Gender:");
contentPane.add(gender);
layout.putConstraint(SpringLayout.WEST, gender, 10, SpringLayout.WEST, contentPane);
layout.putConstraint(SpringLayout.NORTH, gender, 95, SpringLayout.NORTH, contentPane);

//radiohead:
ButtonGroup group = new ButtonGroup();

//male
JRadioButton option = new JRadioButton("Male", true);
group.add(option);
contentPane.add(option);
layout.putConstraint(SpringLayout.WEST, option, 90, SpringLayout.WEST, contentPane);
layout.putConstraint(SpringLayout.NORTH, option, 90, SpringLayout.NORTH, contentPane);
//female
JRadioButton option2 = new JRadioButton("Female", true);
group.add(option2);
contentPane.add(option2);
layout.putConstraint(SpringLayout.NORTH, option2, 90, SpringLayout.NORTH, contentPane);
layout.putConstraint(SpringLayout.WEST, option2, 150, SpringLayout.WEST, contentPane);

//birthday
JLabel biday = new JLabel("Birth Date:");
//months
bDay = new JComboBox();
bDay.addItem("January");
bDay.addItem("February"); 
bDay.addItem("March");
bDay.addItem("April");
bDay.addItem("May");
bDay.addItem("June");
bDay.addItem("July");
bDay.addItem("August");
bDay.addItem("September");
bDay.addItem("October");
bDay.addItem("November");
bDay.addItem("December");
contentPane.add(biday);
contentPane.add(bDay);
layout.putConstraint(SpringLayout.NORTH, biday, 125, SpringLayout.NORTH, contentPane);
layout.putConstraint(SpringLayout.WEST, biday, 10, SpringLayout.WEST, contentPane);
layout.putConstraint(SpringLayout.NORTH, bDay, 120, SpringLayout.NORTH, contentPane);
layout.putConstraint(SpringLayout.WEST, bDay, 95, SpringLayout.WEST, contentPane);

//days
days = new JComboBox();
days.addItem("1");
days.addItem("2"); 
days.addItem("3");
days.addItem("4");
days.addItem("5");
days.addItem("6");
days.addItem("7");
days.addItem("8");
days.addItem("9");
days.addItem("10");
days.addItem("11");
days.addItem("12");
days.addItem("13");
days.addItem("14");
days.addItem("15");
days.addItem("16");
days.addItem("17");
days.addItem("18");
days.addItem("19");
days.addItem("20");
days.addItem("21");
days.addItem("22");
days.addItem("23");
days.addItem("24");
days.addItem("25");
days.addItem("26");
days.addItem("27");
days.addItem("28");
days.addItem("29");
days.addItem("30");
days.addItem("31");
contentPane.add(days);
layout.putConstraint(SpringLayout.NORTH, days, 120, SpringLayout.WEST, contentPane);
layout.putConstraint(SpringLayout.WEST, days, 200, SpringLayout.WEST, contentPane);

//years
taon = new JComboBox();
taon.addItem("1994");
taon.addItem("1995");
taon.addItem("1996");
taon.addItem("1997");
taon.addItem("1998");
taon.addItem("1999");
taon.addItem("2000");
contentPane.add(taon);
layout.putConstraint(SpringLayout.NORTH, taon, 120, SpringLayout.WEST, contentPane);
layout.putConstraint(SpringLayout.WEST, taon, 255, SpringLayout.WEST, contentPane);

//address
Component address = new JLabel("Address: ");
Component address2 = new JTextField(25);
contentPane.add(address);
contentPane.add(address2);
layout.putConstraint(SpringLayout.NORTH, address, 158, SpringLayout.NORTH, contentPane);
layout.putConstraint(SpringLayout.WEST, address, 10, SpringLayout.WEST, contentPane);
layout.putConstraint(SpringLayout.NORTH, address2, 160, SpringLayout.NORTH, contentPane);
layout.putConstraint(SpringLayout.WEST, address2, 94, SpringLayout.WEST, contentPane);

//contact number
Component cNumber = new JLabel("Contact No.: ");
Component cNumber2 = new JTextField(15);
contentPane.add(cNumber);
contentPane.add(cNumber2);
layout.putConstraint(SpringLayout.NORTH, cNumber, 182, SpringLayout.NORTH, contentPane);
layout.putConstraint(SpringLayout.WEST, cNumber, 10, SpringLayout.WEST, contentPane);
layout.putConstraint(SpringLayout.NORTH, cNumber2, 180, SpringLayout.NORTH, contentPane);
layout.putConstraint(SpringLayout.WEST, cNumber2, 94, SpringLayout.WEST, contentPane);

//father's name
Component fName = new JLabel("Father's Name: ");
Component fName2 = new JTextField(20);
contentPane.add(fName);
contentPane.add(fName2);
layout.putConstraint(SpringLayout.NORTH, fName, 212, SpringLayout.NORTH, contentPane);
layout.putConstraint(SpringLayout.WEST, fName, 10, SpringLayout.WEST, contentPane);
layout.putConstraint(SpringLayout.NORTH, fName2, 210, SpringLayout.NORTH, contentPane);
layout.putConstraint(SpringLayout.WEST, fName2, 105, SpringLayout.WEST, contentPane);

//mother's name
Component mName = new JLabel("Mother's Name: ");
Component mName2 = new JTextField(20);
contentPane.add(mName);
contentPane.add(mName2);
layout.putConstraint(SpringLayout.NORTH, mName, 232, SpringLayout.NORTH, contentPane);
layout.putConstraint(SpringLayout.WEST, mName, 10, SpringLayout.WEST, contentPane);
layout.putConstraint(SpringLayout.NORTH, mName2, 230, SpringLayout.NORTH, contentPane);
layout.putConstraint(SpringLayout.WEST, mName2, 105, SpringLayout.WEST, contentPane);

//elementary
Component eSchool = new JLabel("Elementary School: ");
Component eSchool2 = new JTextField(20);
contentPane.add(eSchool);
contentPane.add(eSchool2);
layout.putConstraint(SpringLayout.NORTH, eSchool, 262, SpringLayout.NORTH, contentPane);
layout.putConstraint(SpringLayout.WEST, eSchool, 10, SpringLayout.WEST, contentPane);
layout.putConstraint(SpringLayout.NORTH, eSchool2, 260, SpringLayout.NORTH, contentPane);
layout.putConstraint(SpringLayout.WEST, eSchool2, 125, SpringLayout.WEST, contentPane);

//hayskul
Component hiSchool = new JLabel("Secondary School: ");
Component hiSchool2 = new JTextField(20);
contentPane.add(hiSchool);
contentPane.add(hiSchool2);
layout.putConstraint(SpringLayout.NORTH, hiSchool, 282, SpringLayout.NORTH, contentPane);
layout.putConstraint(SpringLayout.WEST, hiSchool, 10, SpringLayout.WEST, contentPane);
layout.putConstraint(SpringLayout.NORTH, hiSchool2, 280, SpringLayout.NORTH, contentPane);
layout.putConstraint(SpringLayout.WEST, hiSchool2, 125, SpringLayout.WEST, contentPane);

//interests with checkbox
Component interest = new JLabel("Interests: ");
Component interest_arts = new JCheckBox("Arts");
Component interest_sci = new JCheckBox("Science");
Component interest_cook = new JCheckBox("Cooking");
Component interest_sports = new JCheckBox("Sports");
contentPane.add(interest);
contentPane.add(interest_arts);
contentPane.add(interest_sci);
contentPane.add(interest_cook);
contentPane.add(interest_sports);
layout.putConstraint(SpringLayout.NORTH, interest, 312, SpringLayout.NORTH, contentPane);
layout.putConstraint(SpringLayout.WEST, interest, 10, SpringLayout.WEST, contentPane);
layout.putConstraint(SpringLayout.NORTH, interest_arts, 310, SpringLayout.NORTH, contentPane);
layout.putConstraint(SpringLayout.WEST, interest_arts, 125, SpringLayout.WEST, contentPane);
layout.putConstraint(SpringLayout.NORTH, interest_sci, 310, SpringLayout.NORTH, contentPane);
layout.putConstraint(SpringLayout.WEST, interest_sci, 200, SpringLayout.WEST, contentPane);
layout.putConstraint(SpringLayout.NORTH, interest_cook, 330, SpringLayout.NORTH, contentPane);
layout.putConstraint(SpringLayout.WEST, interest_cook, 125, SpringLayout.WEST, contentPane);
layout.putConstraint(SpringLayout.NORTH, interest_sports, 330, SpringLayout.NORTH, contentPane);
layout.putConstraint(SpringLayout.WEST, interest_sports, 200, SpringLayout.WEST, contentPane);

Component button = new JButton("Submit");
contentPane.add(button);
layout.putConstraint(SpringLayout.NORTH, button, 400, SpringLayout.NORTH, contentPane);
layout.putConstraint(SpringLayout.WEST, button, 170, SpringLayout.WEST, contentPane);



frame.setSize(500, 550);    
frame.setVisible(true);
}
}

此代码仅显示接口程序。一个简单的界面。

2 个答案:

答案 0 :(得分:1)

如果你只是想在新窗口中显示JTextFiled中的内容,你可以试试 JOptionPane.ShowMessageDialog(null,“text here”);

这会弹出一个包含w.e text u put的msg框...所以你想要的文字是:

address1.getText()+"\n"+.... 

并且为了在单击提交按钮时实现这一点,您需要将ActionListenner添加到该按钮,因此它将类似于:

 submitButton.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent evt){

  JOptionPane.ShowMessageDialog(null,"text here");
  }
 });

答案 1 :(得分:0)

首先获取所有字段的所有信息

语法:     String s = xx.getText()。toString();

检索按钮点击信息后

使用

Joptionpane.showMessageDialogue(NULL,S);

它在对话框中显示文本字段信息