我的代码如下:
package playingwithjava;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
*
* @author user
*/
public class Window extends JFrame implements ActionListener{
//init stuff
Container ca=getContentPane();
GridBagLayout gridLayout= new GridBagLayout();
GridBagConstraints lol=new GridBagConstraints();
//actual important stuff
JButton done=new JButton("Done!");
JTextField text=new JTextField("",25);
JLabel label=new JLabel("Your name is:");
public Window(int x,int y)
{
super("Playing With Java");
setSize(x,y);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ca.setLayout(gridLayout);
//start of the code
lol.gridx=0;lol.gridy=0;
ca.add(label,lol);
lol.gridx=0;lol.gridy=1;
text.addActionListener(this);
ca.add(text,lol);
lol.gridx=1;lol.gridy=1;
//done.setEnabled(false);
done.addActionListener(this);
ca.add(done,lol);
//ending
setContentPane(ca);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent event)
{
if(event.getSource()==done)
{
JOptionPane.showMessageDialog(null,"Hello, "+text.getText()+"!","Hi!",JOptionPane.NO_OPTION);
}
if(event.getSource()==text)
{
JOptionPane.showMessageDialog(null,"a","Hi!",JOptionPane.NO_OPTION);
}
}
}
我不明白为什么当我更改 TextField 中的文本时什么都没有发生。