JAVA编程题:创建有一个文本框和三个按钮的小程序。当按下每个按钮时,使不同的文字显示在文本框中
2个回答
展开全部
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Test extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
private JPanel jPanel;
private JButton jButton1,jButton2,jButton3;
private JTextField tf1;
public Test(String title) {
super(title);
init();
}
private void init() {
jPanel=new JPanel();
jPanel.setLayout(new FlowLayout());
tf1=new JTextField(20);
jButton1=new JButton("按钮1");
jButton2=new JButton("按钮2");
jButton3=new JButton("按钮3");
jButton1.addActionListener(this);
jButton2.addActionListener(this);
jButton3.addActionListener(this);
jPanel.add(tf1);
jPanel.add(jButton1);
jPanel.add(jButton2);
jPanel.add(jButton3);
this.add(jPanel);
this.setSize(300,100);
this.setResizable(false);
this.setVisible(true);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(final WindowEvent e) {
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(jButton1)){
tf1.setText("按钮1");
}
if(e.getSource().equals(jButton2)){
tf1.setText("按钮2");
}
if(e.getSource().equals(jButton3)){
tf1.setText("按钮3");
}
}
public static void main(String[] args){
new Test("文字变换");
}
}
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Test extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
private JPanel jPanel;
private JButton jButton1,jButton2,jButton3;
private JTextField tf1;
public Test(String title) {
super(title);
init();
}
private void init() {
jPanel=new JPanel();
jPanel.setLayout(new FlowLayout());
tf1=new JTextField(20);
jButton1=new JButton("按钮1");
jButton2=new JButton("按钮2");
jButton3=new JButton("按钮3");
jButton1.addActionListener(this);
jButton2.addActionListener(this);
jButton3.addActionListener(this);
jPanel.add(tf1);
jPanel.add(jButton1);
jPanel.add(jButton2);
jPanel.add(jButton3);
this.add(jPanel);
this.setSize(300,100);
this.setResizable(false);
this.setVisible(true);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(final WindowEvent e) {
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(jButton1)){
tf1.setText("按钮1");
}
if(e.getSource().equals(jButton2)){
tf1.setText("按钮2");
}
if(e.getSource().equals(jButton3)){
tf1.setText("按钮3");
}
}
public static void main(String[] args){
new Test("文字变换");
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
swing和awt的编程,网上会有很多教程,书上肯定也有
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询