我怎么能有一个可写的文本框?

时间:2014-07-02 09:41:05

标签: java swing

使用纯java,我希望有一个播放器按下JButton,弹出一个可以输入的文本框,然后当播放器按下" Enter"时会发生某种动作。

我怎么能这样做?

我还没有尝试过这个,因为我不知道从哪里开始,但我当前的代码是

package Joehot200;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Image;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JButton;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.EOFException;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;

import javax.swing.ImageIcon;
import javax.swing.JMenu;
import javax.swing.JMenuBar;

public class Main extends JFrame {
static boolean start = false;
private JPanel contentPane;

/**
 * Launch the application.
 */
static Main frame = null;
String version = "0.4";
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                frame = new Main();
                frame.setVisible(true);
                frame.setTitle("Privateers");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
    TerrainDemo.startGame();
}
boolean cantConnect = false;
/**
 * Create the frame.
 */

public Main() {
    setExtendedState(JFrame.MAXIMIZED_BOTH);
    JMenuBar menuBar = new JMenuBar();
    setJMenuBar(menuBar);
    final JButton btnEnterBattlefield =  new JButton("Enter battlefield!"); 
    btnEnterBattlefield.setForeground(Color.red);
    //btnEnterBattlefield.setBackground(Color.green);
    //btnEnterBattlefield.setOpaque(true);
    menuBar.add(btnEnterBattlefield);
    btnEnterBattlefield.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e)
        {
            //Execute when button is pressed
             System.out.println(new File(System.getProperty("user.dir") + "res/images/heightmap.bmp").getAbsolutePath());
            //System.out.println("You clicked the button");
             if (cantConnect){
                 btnEnterBattlefield.setText("Unable to connect to the server!");
             }else{
                 btnEnterBattlefield.setText("Connecting to server...");
             }
            start = true;
        } 
    });      

    //JMenu mnLogIn = new JMenu("Log in");
    JMenu mnLogIn = new JMenu("Currently useless button");
    mnLogIn.setForeground(Color.magenta);
    menuBar.add(mnLogIn);

    JButton btnLogIntoGame = new JButton("Log into game.");
    mnLogIn.add(btnLogIntoGame);

    JButton btnRegisterNewAccount = new JButton("Register new account");
    mnLogIn.add(btnRegisterNewAccount);
    final JButton btnGoToWebsite = new JButton("We currently do not but soon will have a website.");
    btnGoToWebsite.setForeground(Color.BLUE);
        //btnGoToWebsite = new JButton("Go to website.");
    btnGoToWebsite.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e)
        {
            //Execute when button is pressed
            System.out.println("Going to website!");
                try {
                    java.awt.Desktop.getDesktop().browse(new URI("www.endcraft.net/none"));
                } catch (Exception e1) {
                    btnGoToWebsite.setText("Error going to website!");
                }
        }
    });      
    menuBar.add(btnGoToWebsite);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    FlowLayout flowLayout = (FlowLayout) contentPane.getLayout();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    //contentPane.setLayout(new BorderLayout(0, 0));
    setContentPane(contentPane);
    ImageIcon icon = new ImageIcon(System.getProperty("user.dir") + "/res/background.png");
    Image img = icon.getImage() ;  
       Image newimg = img.getScaledInstance(this.getWidth()*3, this.getHeight()*3,  java.awt.Image.SCALE_SMOOTH ) ;  
       icon = new ImageIcon( newimg );
      JLabel background=new JLabel(icon);

        getContentPane().add(background);

        background.setLayout(new FlowLayout());

    //JProgressBar progressBar = new JProgressBar();
    //contentPane.add(progressBar);

    //JMenu mnWelcomeToA = new JMenu("Welcome to a pirate game!");
    //contentPane.add(mnWelcomeToA);

    //JButton btnStartGame = new JButton("Start game");
    //mnWelcomeToA.add(btnStartGame);

    //JSplitPane splitPane = new JSplitPane();
    //mnWelcomeToA.add(splitPane);

    //JButton btnRegister = new JButton("Register");
    //splitPane.setLeftComponent(btnRegister);

    //JButton btnLogin = new JButton("Login");
    //splitPane.setRightComponent(btnLogin);


}


}

这是"注册帐户"和#34;登录游戏"我希望上述行动发生的按钮。

如果有人能告诉我如何做到这一点会很棒。

1 个答案:

答案 0 :(得分:0)

如果您决定使用新面板以及JTextField或JTextArea来实现新框架(非最佳实践),则必须将侦听器绑定到在新创建的框架上调用setVisible()的按钮。 。 例如:

public void actionPerformed(ActionEvent e) {
    yourFrame.setVisible(true);
}

此外,您可以在每次单击按钮时在actionPerformed方法中创建框架,但这也不是最佳实践..

更好的替代方案可能是实施JOptionPane,请参阅herehere ..