设计一个程序,使用Textfield输入一个文本当点击一个按钮后将该文本添加到一个choice当中
若以下回答无法解决问题,邀请你更新回答
1个回答
展开全部
import java.awt.Button;
import java.awt.Choice;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
public class TestData extends JFrame implements ActionListener
{
private TextField textField;
private Button button;
private Choice choice;
public TestData(){
textField = new TextField();
Dimension preferredSize = new Dimension(200, 20);
textField.setPreferredSize(preferredSize);
button = new Button("添加");
choice = new Choice();
this.setLayout(new FlowLayout(FlowLayout.CENTER, 1, 1));
this.add(textField);
this.add(button);
this.add(choice);
this.setBounds(100, 100, 300, 300);
this.setVisible(true);
button.addActionListener(this);
}
public static void main(String[] args)
{
// TODO Auto-generated method stub
TestData td = new TestData();
}
@Override
public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub
if(e.getSource() == button){
choice.add(textField.getText());
}
}
}
完整代码希望采纳
import java.awt.Choice;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
public class TestData extends JFrame implements ActionListener
{
private TextField textField;
private Button button;
private Choice choice;
public TestData(){
textField = new TextField();
Dimension preferredSize = new Dimension(200, 20);
textField.setPreferredSize(preferredSize);
button = new Button("添加");
choice = new Choice();
this.setLayout(new FlowLayout(FlowLayout.CENTER, 1, 1));
this.add(textField);
this.add(button);
this.add(choice);
this.setBounds(100, 100, 300, 300);
this.setVisible(true);
button.addActionListener(this);
}
public static void main(String[] args)
{
// TODO Auto-generated method stub
TestData td = new TestData();
}
@Override
public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub
if(e.getSource() == button){
choice.add(textField.getText());
}
}
}
完整代码希望采纳
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询