JAVA 按钮 监听
请问下面的一段代码,如果才能实现监听,使按键按一下,上面文本有显示相应的数字.importjava.awt.*;importjavax.swing.*;publiccla...
请问下面的一段代码,如果才能实现监听,使按键按一下,上面文本有显示相应的数字.
import java.awt.*;
import javax.swing.*;
public class JiSuan extends JFrame
{
public JiSuan()
{
Container container=getContentPane();
container.setLayout(new BorderLayout());
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(4,3));
for(int i=1;i<=9;i++)
{
p1.add(new JButton(""+i));
}
p1.add(new JButton(""+0));
p1.add(new JButton("+/-"));
p1.add(new JButton("."));
JPanel p2=new JPanel(new BorderLayout());
p2.setLayout(new GridLayout(4,2));
p2.add(new JButton("/"));
p2.add(new JButton("sqrt"));
p2.add(new JButton("*"));
p2.add(new JButton("%"));
p2.add(new JButton("-"));
p2.add(new JButton("1/x"));
p2.add(new JButton("+"));
p2.add(new JButton("="));
JPanel p3=new JPanel(new BorderLayout());
p3.setLayout(new GridLayout(4,1));
p3.add(new JButton("MC"));
p3.add(new JButton("MR"));
p3.add(new JButton("MS"));
p3.add(new JButton("M+"));
JPanel p4=new JPanel(new BorderLayout());
p4.add(p3,BorderLayout.WEST);
p4.add(p1,BorderLayout.CENTER);
p4.add(p2,BorderLayout.EAST);
JPanel p5=new JPanel(new BorderLayout());
p5.setLayout(new GridLayout(1,3));
p5.add(new JButton("Backspace"));
p5.add(new JButton("CE"));
p5.add(new JButton("C"));
JPanel p6=new JPanel(new BorderLayout());
p6.add(new JTextField("0"),BorderLayout.NORTH);
p6.add(p5,BorderLayout.CENTER);
p6.add(p4,BorderLayout.SOUTH);
container.add(p6,BorderLayout.EAST);
container.add(new JButton("开发中..."),BorderLayout.CENTER);
}
public static void main(String[] args)
{
JiSuan ji =new JiSuan();
ji.setTitle("窗口布局");
ji.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ji.setSize(400,200);
ji.setVisible(true);
}
} 展开
import java.awt.*;
import javax.swing.*;
public class JiSuan extends JFrame
{
public JiSuan()
{
Container container=getContentPane();
container.setLayout(new BorderLayout());
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(4,3));
for(int i=1;i<=9;i++)
{
p1.add(new JButton(""+i));
}
p1.add(new JButton(""+0));
p1.add(new JButton("+/-"));
p1.add(new JButton("."));
JPanel p2=new JPanel(new BorderLayout());
p2.setLayout(new GridLayout(4,2));
p2.add(new JButton("/"));
p2.add(new JButton("sqrt"));
p2.add(new JButton("*"));
p2.add(new JButton("%"));
p2.add(new JButton("-"));
p2.add(new JButton("1/x"));
p2.add(new JButton("+"));
p2.add(new JButton("="));
JPanel p3=new JPanel(new BorderLayout());
p3.setLayout(new GridLayout(4,1));
p3.add(new JButton("MC"));
p3.add(new JButton("MR"));
p3.add(new JButton("MS"));
p3.add(new JButton("M+"));
JPanel p4=new JPanel(new BorderLayout());
p4.add(p3,BorderLayout.WEST);
p4.add(p1,BorderLayout.CENTER);
p4.add(p2,BorderLayout.EAST);
JPanel p5=new JPanel(new BorderLayout());
p5.setLayout(new GridLayout(1,3));
p5.add(new JButton("Backspace"));
p5.add(new JButton("CE"));
p5.add(new JButton("C"));
JPanel p6=new JPanel(new BorderLayout());
p6.add(new JTextField("0"),BorderLayout.NORTH);
p6.add(p5,BorderLayout.CENTER);
p6.add(p4,BorderLayout.SOUTH);
container.add(p6,BorderLayout.EAST);
container.add(new JButton("开发中..."),BorderLayout.CENTER);
}
public static void main(String[] args)
{
JiSuan ji =new JiSuan();
ji.setTitle("窗口布局");
ji.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ji.setSize(400,200);
ji.setVisible(true);
}
} 展开
1个回答
展开全部
class MyAction extends immplements ActionLister {
public void actionPerformed(ActionEvent e) {
String str = (JButton)e.getSource).getText();
if (str == "你的按钮上面的字") {
new JTextArea().setText(str);
}
}
}
这样就可以了。然后上面每个按钮都监听上,
比如
new JButton(""+i).addActionListener(new MyAction());
每个都添加就好了。。
public void actionPerformed(ActionEvent e) {
String str = (JButton)e.getSource).getText();
if (str == "你的按钮上面的字") {
new JTextArea().setText(str);
}
}
}
这样就可以了。然后上面每个按钮都监听上,
比如
new JButton(""+i).addActionListener(new MyAction());
每个都添加就好了。。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询