java编程,设计一个窗体,要有三个部分(文本框、按钮、标签),按钮内容输出到文本框 20
如题,按钮上显示“Helloworld",然后点击按钮,文本框能够出现”Helloworld",老师说用最简单的流式布局就可以了。请大神帮忙,鞠躬。...
如题,按钮上显示“Hello world",然后点击按钮,文本框能够出现”Hello world",老师说用最简单的流式布局就可以了。请大神帮忙,鞠躬。
展开
2个回答
展开全部
package anfang;
import java.awt.*;
import java.awt.event.*;
public class example1 extends Frame{
Frame frm;
Button btn1,btn2,btn3,btn4;
TextField txt1,txt2,txt3;
public example1()
{
setLayout(null);
btn1 = new Button("+");
btn1.setLocation(55,55);
btn1.setSize(80,20);
class BtnClick implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
txt3.setText(""+(Integer.parseInt(txt1.getText())+Integer.parseInt(txt2.getText())));
}
}
BtnClick bc=new BtnClick();
btn1.addActionListener(bc);
add(btn1);
btn2 = new Button("-");
btn2.setLocation(50,100);
btn2.setSize(80,20);
class BtnClick1 implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
txt3.setText(""+(Integer.parseInt(txt1.getText())-Integer.parseInt(txt2.getText())));
}
}
BtnClick1 bc1=new BtnClick1();
btn2.addActionListener(bc1);
add(btn2);
btn3 = new Button("*");
btn3.setLocation(50,150);
btn3.setSize(80,20);
class BtnClick2 implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
txt3.setText(""+Integer.parseInt(txt1.getText())*Integer.parseInt(txt2.getText()));
}
}
BtnClick2 bc2=new BtnClick2();
btn3.addActionListener(bc2);
add(btn3);
btn4 = new Button("/");
btn4.setLocation(50,200);
btn4.setSize(80,20);
class BtnClick3 implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
txt3.setText(""+Integer.parseInt(txt1.getText())/Integer.parseInt(txt2.getText()));
}
}
BtnClick3 bc3=new BtnClick3();
btn4.addActionListener(bc3);
add(btn4);
txt1 = new TextField();
txt1.setLocation(200,60);
txt1.setSize(80,20);
add(txt1);
txt2 = new TextField();
txt2.setLocation(200,120);
txt2.setSize(80,20);
add(txt2);
txt3 = new TextField();
txt3.setLocation(200,180);
txt3.setSize(80,20);
add(txt3);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
example1 frm=new example1();
frm.setSize(450,300);
frm.show();
}
}
import java.awt.*;
import java.awt.event.*;
public class example1 extends Frame{
Frame frm;
Button btn1,btn2,btn3,btn4;
TextField txt1,txt2,txt3;
public example1()
{
setLayout(null);
btn1 = new Button("+");
btn1.setLocation(55,55);
btn1.setSize(80,20);
class BtnClick implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
txt3.setText(""+(Integer.parseInt(txt1.getText())+Integer.parseInt(txt2.getText())));
}
}
BtnClick bc=new BtnClick();
btn1.addActionListener(bc);
add(btn1);
btn2 = new Button("-");
btn2.setLocation(50,100);
btn2.setSize(80,20);
class BtnClick1 implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
txt3.setText(""+(Integer.parseInt(txt1.getText())-Integer.parseInt(txt2.getText())));
}
}
BtnClick1 bc1=new BtnClick1();
btn2.addActionListener(bc1);
add(btn2);
btn3 = new Button("*");
btn3.setLocation(50,150);
btn3.setSize(80,20);
class BtnClick2 implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
txt3.setText(""+Integer.parseInt(txt1.getText())*Integer.parseInt(txt2.getText()));
}
}
BtnClick2 bc2=new BtnClick2();
btn3.addActionListener(bc2);
add(btn3);
btn4 = new Button("/");
btn4.setLocation(50,200);
btn4.setSize(80,20);
class BtnClick3 implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
txt3.setText(""+Integer.parseInt(txt1.getText())/Integer.parseInt(txt2.getText()));
}
}
BtnClick3 bc3=new BtnClick3();
btn4.addActionListener(bc3);
add(btn4);
txt1 = new TextField();
txt1.setLocation(200,60);
txt1.setSize(80,20);
add(txt1);
txt2 = new TextField();
txt2.setLocation(200,120);
txt2.setSize(80,20);
add(txt2);
txt3 = new TextField();
txt3.setLocation(200,180);
txt3.setSize(80,20);
add(txt3);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
example1 frm=new example1();
frm.setSize(450,300);
frm.show();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
程序如下:
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class MyFrame extends JFrame implements ActionListener
{
private JButton button;
private JTextField textField;
private JLabel label;
public MyFrame()
{
button = new JButton("Hello world");
textField = new JTextField(15);
label = new JLabel("输入内容:");
setTitle("Simple-Frame");
setBounds((Toolkit.getDefaultToolkit().getScreenSize().width - 500)/2,
(Toolkit.getDefaultToolkit().getScreenSize().height - 300)/2
, 500, 300);
setLayout(new FlowLayout());
add(label);
add(textField);
add(button);
setResizable(false);
setVisible(true);
this.button.addActionListener(this);
}
public static void main(String[] args)
{
new MyFrame();
}
@Override
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == this.button)
{
this.textField.setText("Hello World");
}
}
}
有问题欢迎提问,满意请采纳,谢谢!
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class MyFrame extends JFrame implements ActionListener
{
private JButton button;
private JTextField textField;
private JLabel label;
public MyFrame()
{
button = new JButton("Hello world");
textField = new JTextField(15);
label = new JLabel("输入内容:");
setTitle("Simple-Frame");
setBounds((Toolkit.getDefaultToolkit().getScreenSize().width - 500)/2,
(Toolkit.getDefaultToolkit().getScreenSize().height - 300)/2
, 500, 300);
setLayout(new FlowLayout());
add(label);
add(textField);
add(button);
setResizable(false);
setVisible(true);
this.button.addActionListener(this);
}
public static void main(String[] args)
{
new MyFrame();
}
@Override
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == this.button)
{
this.textField.setText("Hello World");
}
}
}
有问题欢迎提问,满意请采纳,谢谢!
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询