簡體   English   中英

如何在JOptionPane消息中顯示JTextfield輸入?

[英]How can I display JTextfield input, in an JOptionPane message?

我是新手,但嘗試用Java / Swing編寫我的第一個GUI。 我已經使用Javadoc進行了深入研究,但是我不明白為什么我的JOptionPane不顯示我的兩個JTextField中接受的輸入。

請不要建議使用布局管理器,我想先使用自己的坐標進行探索,以后可能會考慮使用布局管理器。

我只是在尋找正確方向的觀點,而不是有人為我做到這一點。 這是我的代碼段。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;  
import javax.swing.JOptionPane;
import javax.swing.JFrame;
import javax.swing.JPanel;        
import javax.swing.JLabel;
/**
 *
 * @author Darren Estcourt
 */

   public class ManagerName implements java.io.Serializable , ActionListener
   {
    JFrame f3;
    JLabel firstNameLabel , surnameLabel , fullName;
    JButton confirmNames , quit;
    String firstName , surname, playerFullName;
    JTextField myTextField , myTextField2;
    public ManagerName()
    {

        f3 = new JFrame("Create Profile");  
        f3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        firstNameLabel = new JLabel("Please enter your first name");
        firstNameLabel.setBounds(50,25,200,30);
        f3.add(firstNameLabel);


        myTextField = new JTextField(20); // or use 20 columns
        f3.add(myTextField);
        myTextField.setBounds(50,75,200,30);
        myTextField.addActionListener(this);
        String firstName = myTextField.getText();

        surnameLabel = new JLabel("Please enter your surname");
        surnameLabel.setBounds(50,125,200,30);
        f3.add(surnameLabel);

        confirmNames = new JButton("Submit Names");
        confirmNames.addActionListener(this);
        confirmNames.setBounds(50,225,200,30);
        f3.add(confirmNames);

        quit = new JButton("Quit");
        quit.addActionListener(this);
        quit.setBounds(50,325,200,30);
        f3.add(quit);


        myTextField2 = new JTextField(20);
        f3.add(myTextField2);
        myTextField2.setBounds(50,175,200,30);
        myTextField2.addActionListener(this);
        String surname = myTextField2.getText();

        playerFullName = firstName + surname;

        f3.setSize(500,500);  
        f3.setLayout(null); 
        f3.setVisible(true);

      } // end managerName

        public void setFrame(JFrame f3)
        {
            this.f3 = f3;
        }
        public JFrame getFrame()
        {
              return f3;
        }

        public String getManagerName()
        {
        return playerFullName;
        }
        @Override public void actionPerformed(ActionEvent e)
            {  
                if(e.getSource()==confirmNames)
                {

                    JOptionPane.showMessageDialog(f3, "This works" + playerFullName);

                }  

                if(e.getSource()==quit)
                {
                    System.exit(0);
                }  

            }    

}

按下confirmNames時,您需要從JTextField中獲取文本,否則在創建時會從JTextField中獲取文本。

if(e.getSource()==confirmNames) {
    String firstName = myTextField.getText();
    String surname = myTextField2.getText();

    playerFullName = firstName + surname;
    JOptionPane.showMessageDialog(f3, "This works" + playerFullName);

}  

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM