Java小题,急用(下午四点前有效)一个多行文本域ta,一个按钮ok,初始文本框内容为空,点击按钮。。
有一个多行文本域ta,一个按钮ok,初始文本框内容为空,点击按钮,文本框出现”Iamastudent....“并利用匿名类实现对窗口关闭的监听...
有一个多行文本域ta,一个按钮ok,初始文本框内容为空,点击按钮,文本框出现”I am a student....“并利用匿名类实现对窗口关闭的监听
展开
展开全部
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
public class OneButton extends JFrame {
private JTextArea textArea;
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
OneButton frame = new OneButton();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame
*/
public OneButton() {
super();
getContentPane().setLayout(null);
setBounds(100, 100, 314, 234);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JButton okButton = new JButton();
okButton.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent arg0) {
textArea.setText("I am a student,and you?");
}
});
okButton.setText("OK");
okButton.setBounds(3, 69, 61, 36);
getContentPane().add(okButton);
textArea = new JTextArea();
textArea.setBounds(70, 7, 226, 184);
getContentPane().add(textArea);
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
public class OneButton extends JFrame {
private JTextArea textArea;
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
OneButton frame = new OneButton();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame
*/
public OneButton() {
super();
getContentPane().setLayout(null);
setBounds(100, 100, 314, 234);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JButton okButton = new JButton();
okButton.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent arg0) {
textArea.setText("I am a student,and you?");
}
});
okButton.setText("OK");
okButton.setBounds(3, 69, 61, 36);
getContentPane().add(okButton);
textArea = new JTextArea();
textArea.setBounds(70, 7, 226, 184);
getContentPane().add(textArea);
}
}
2009-06-04
展开全部
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
public class SwingTest {
public static void main(String[] args) {
JFrame f = new JFrame();
//利用匿名类实现对窗口关闭的监听
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.out.println("正在关闭窗口...");
System.exit(0);
}
});
//文本区
final JTextArea ta = new JTextArea();
//按钮
JButton b = new JButton("OK");
//利用匿名类实现对按钮的监听
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
ta.setText("I am a student.And you?");
}
});
//窗口排版显示
f.getContentPane().setLayout(new BorderLayout());
f.add(b,BorderLayout.WEST);
f.add(ta);
f.setSize(300,200);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
public class SwingTest {
public static void main(String[] args) {
JFrame f = new JFrame();
//利用匿名类实现对窗口关闭的监听
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.out.println("正在关闭窗口...");
System.exit(0);
}
});
//文本区
final JTextArea ta = new JTextArea();
//按钮
JButton b = new JButton("OK");
//利用匿名类实现对按钮的监听
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
ta.setText("I am a student.And you?");
}
});
//窗口排版显示
f.getContentPane().setLayout(new BorderLayout());
f.add(b,BorderLayout.WEST);
f.add(ta);
f.setSize(300,200);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询