java事件处理如何单击按钮添加文本框
importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;importjavax.swing.event.*...
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class J_Table extends JFrame{
public static final int WIDTH = 400;
public static final int HEIGHT = 250;
public static final String checkedLabel="";
private JTextField countertext;
private JButton counterbutton;
JTextField boardout;
String s;
public J_Table()
{
super("计算器");
setSize(WIDTH, HEIGHT);
Container c =getContentPane();
c.setLayout(new GridLayout());
//建立容纳操作的面板
JPanel buttonPanel = new JPanel();
int i;
for(i=0;i<10;i++)
{
s=""+i;
counterbutton=new JButton(s);
buttonPanel.add(counterbutton);
c.add(buttonPanel);
}
counterbutton=new JButton("=");
buttonPanel.add(counterbutton);
c.add(buttonPanel);
counterbutton=new JButton("c");
buttonPanel.add(counterbutton);
c.add(buttonPanel);
counterbutton=new JButton(" +");
buttonPanel.add(counterbutton);
c.add(buttonPanel);
counterbutton=new JButton(" -");
buttonPanel.add(counterbutton);
c.add(buttonPanel);
counterbutton=new JButton(" * ");
buttonPanel.add(counterbutton);
c.add(buttonPanel);
counterbutton=new JButton(" /");
buttonPanel.add(counterbutton);
c.add(buttonPanel);
JPanel textPanel = new JPanel();
//新建文本区
countertext = new JTextField(checkedLabel,10);
textPanel.add(countertext);
c.add(textPanel);
//用匿名类来实现按钮动作
JButton n = new JButton();
n.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
char a ='0';
String v =String.valueOf(a);
if(a!='='&&a!='c')
{
JTextField boardout=(JTextField)e.getSource();
countertext.setText(v);
}
else
{
}
}
}
);
}
} 展开
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class J_Table extends JFrame{
public static final int WIDTH = 400;
public static final int HEIGHT = 250;
public static final String checkedLabel="";
private JTextField countertext;
private JButton counterbutton;
JTextField boardout;
String s;
public J_Table()
{
super("计算器");
setSize(WIDTH, HEIGHT);
Container c =getContentPane();
c.setLayout(new GridLayout());
//建立容纳操作的面板
JPanel buttonPanel = new JPanel();
int i;
for(i=0;i<10;i++)
{
s=""+i;
counterbutton=new JButton(s);
buttonPanel.add(counterbutton);
c.add(buttonPanel);
}
counterbutton=new JButton("=");
buttonPanel.add(counterbutton);
c.add(buttonPanel);
counterbutton=new JButton("c");
buttonPanel.add(counterbutton);
c.add(buttonPanel);
counterbutton=new JButton(" +");
buttonPanel.add(counterbutton);
c.add(buttonPanel);
counterbutton=new JButton(" -");
buttonPanel.add(counterbutton);
c.add(buttonPanel);
counterbutton=new JButton(" * ");
buttonPanel.add(counterbutton);
c.add(buttonPanel);
counterbutton=new JButton(" /");
buttonPanel.add(counterbutton);
c.add(buttonPanel);
JPanel textPanel = new JPanel();
//新建文本区
countertext = new JTextField(checkedLabel,10);
textPanel.add(countertext);
c.add(textPanel);
//用匿名类来实现按钮动作
JButton n = new JButton();
n.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
char a ='0';
String v =String.valueOf(a);
if(a!='='&&a!='c')
{
JTextField boardout=(JTextField)e.getSource();
countertext.setText(v);
}
else
{
}
}
}
);
}
} 展开
展开全部
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JTableTest extends JFrame implements ActionListener{
public JTableTest() {
JButton button = new JButton("添加文本框");
this.add(button);
button.addActionListener(this);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e){
JTextField text = new JTextField("");
this.add(text);
}
public static void main (String[] args) {
new JTableTest();
}
}
import java.awt.event.*;
import javax.swing.*;
public class JTableTest extends JFrame implements ActionListener{
public JTableTest() {
JButton button = new JButton("添加文本框");
this.add(button);
button.addActionListener(this);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e){
JTextField text = new JTextField("");
this.add(text);
}
public static void main (String[] args) {
new JTableTest();
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询