Java编写一个应用程序,在应用程序中有个按钮和一个文本框。

2024-12-14 00:44:02
推荐回答(2个)
回答1:

import javax.swing.* ;
import java.awt.event.* ;
import java.awt.* ;
public class test extends JFrame implements ActionListener
{
JButton button ;
JTextField text ;
public test()
{
button=new JButton("确定") ;
text=new JTextField(10) ;
setTitle("测试按键监听") ;

button.addActionListener(this) ;
getContentPane().add(text,BorderLayout.NORTH) ;
getContentPane().add(button,BorderLayout.SOUTH) ;

setSize(300,200) ;
setVisible(true) ;
}
public void actionPerformed(ActionEvent e)
{
text.setText(button.getText()) ;
}
public static void main(String []s)
{
new test() ;
}
}import javax.swing.* ;
import java.awt.event.* ;
import java.awt.* ;
public class test extends JFrame implements ActionListener
{
JButton button ;
JTextField text ;
public test()
{
button=new JButton("确定") ;
text=new JTextField(10) ;
setTitle("测试按键监听") ;

button.addActionListener(this) ;
getContentPane().add(text,BorderLayout.NORTH) ;
getContentPane().add(button,BorderLayout.SOUTH) ;

setSize(300,200) ;
setVisible(true) ;
}
public void actionPerformed(ActionEvent e)
{
text.setText(button.getText()) ;
}
public static void main(String []s)
{
new test() ;
}
}import javax.swing.* ;
import java.awt.event.* ;
import java.awt.* ;
public class test extends JFrame implements ActionListener
{
JButton button ;
JTextField text ;
public test()
{
button=new JButton("确定") ;
text=new JTextField(10) ;
setTitle("测试按键监听") ;

button.addActionListener(this) ;
getContentPane().add(text,BorderLayout.NORTH) ;
getContentPane().add(button,BorderLayout.SOUTH) ;

setSize(300,200) ;
setVisible(true) ;
}
public void actionPerformed(ActionEvent e)
{
text.setText(button.getText()) ;
}
public static void main(String []s)
{
new test() ;
}
}import javax.swing.* ;
import java.awt.event.* ;
import java.awt.* ;
public class test extends JFrame implements ActionListener
{
JButton button ;
JTextField text ;
public test()
{
button=new JButton("确定") ;
text=new JTextField(10) ;
setTitle("测试按键监听") ;

button.addActionListener(this) ;
getContentPane().add(text,BorderLayout.NORTH) ;
getContentPane().add(button,BorderLayout.SOUTH) ;

setSize(300,200) ;
setVisible(true) ;
}
public void actionPerformed(ActionEvent e)
{
text.setText(button.getText()) ;
}
public static void main(String []s)
{
new test() ;
}
}

回答2:

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.event.*;
import java.util.Calendar;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;

/**
* Sample application using Frame.
*
* @author
* @version 1.00 04/05/03
*/
public class rame extends JFrame implements AncestorListener{
//f1 f2;
JButton jb;
JTextField jt;
public static void main(String[] arg)
{ new rame();}

public rame() {
jb=new JButton("按钮");
jt=new JTextField();
jb.addAncestorListener(this);
JFrame rame=new JFrame();
Container c=rame.getContentPane();
// f2=new f1();
c.add(jb,BorderLayout.NORTH);
c.add(jt,BorderLayout.CENTER);
// g=new Graphics();

rame.addWindowListener
(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
rame.this.windowClosed();
}
}
);

rame.pack();
rame.setVisible(true);
}

/**
* Shutdown procedure when run as an application.
*/
protected void windowClosed() {

// TODO: Check if it is safe to close the application

// Exit application.
System.exit(0);
}

public void ancestorRemoved(AncestorEvent arg0) {
// TODO 自动生成方法存根

}

public void ancestorAdded(AncestorEvent arg0) {
// TODO 自动生成方法存根

}

public void ancestorMoved(AncestorEvent arg0) {
// TODO 自动生成方法存根
jt.setText(jb.getText());
}

}