JAVA程序 在线等
创建一个GUI程序,含有四个按钮,一个随机产生实数generate,一个计算正弦sin,一个计算余弦cos,一个退出程序exit,两个文本域。点击generate按钮,在...
创建一个 GUI 程序,含有四个按钮,一个随机产生实数generate,一个计算正弦sin ,一个计算余弦 cos ,一个退出程序 exit ,两个文本域。点击generate按钮,在一个文本域中产生一个0~PI之间的实数,用鼠标单击对应的按钮,调用Math库里相应的函数,将计算结果放入另一个文本域。
展开
展开全部
产生实数的:
double generate=Math.random()*Math.PI; 这样就随机产生一个[0,PI)的实数了
double sin=Math.sin(generate);//这个是计算sin的
double cos=Math.cos(generate);//这个是计算cos的
把结果放到某个文本框:
JTextArea text=new JTextArea();
.....
text.setText("sin "+generate+"="+sin);
其实,如果你嫌写代码的时候,布局处理太麻烦,可以用myeclipse或者netbeans,直接拖动组件的
double generate=Math.random()*Math.PI; 这样就随机产生一个[0,PI)的实数了
double sin=Math.sin(generate);//这个是计算sin的
double cos=Math.cos(generate);//这个是计算cos的
把结果放到某个文本框:
JTextArea text=new JTextArea();
.....
text.setText("sin "+generate+"="+sin);
其实,如果你嫌写代码的时候,布局处理太麻烦,可以用myeclipse或者netbeans,直接拖动组件的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
package zhidao;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class TestGui extends JFrame implements ActionListener{
private JPanel pan;
private JTextField var1;
private JButton generate;
private JButton sin;
private JButton cos;
private JButton exit;
private JTextField var2;
private Random random = new Random();
public TestGui(){
this.setLayout(null);
pan = new JPanel();
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("计算器");
setBounds(200, 200, 400, 150);
var1 = new JTextField();
var2 = new JTextField();
var1.setBounds(5, 5, 350, 20);
pan.add(var1);
generate = new JButton("generate");
generate.setBounds(5, 30, 80, 25);
pan.add(generate);
generate.addActionListener(this);
sin = new JButton("sin");
sin.setBounds(95, 30, 80, 25);
pan.add(sin);
sin.addActionListener(this);
cos = new JButton("cos");
cos.setBounds(185, 30, 80, 25);
pan.add(cos);
cos.addActionListener(this);
exit = new JButton("exit");
exit.setBounds(275, 30, 80, 25);
pan.add(exit);
exit.addActionListener(this);
var2.setBounds(5, 60, 350, 20);
pan.add(var2);
pan.setBounds(0, 0, 400, 400);
pan.setLayout(null);
add(pan);
}
@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
// TODO Auto-generated method stub
if("generate".equals(command)){
float f = random.nextFloat();
double f2 = Math.PI * f;
var1.setText(f2+"");
}
if("sin".equals(command)){
var2.setText(Math.sin(Double.valueOf(var1.getText()))+"");
}
if("cos".equals(command)){
var2.setText(Math.cos(Double.valueOf(var1.getText()))+"");
}
if("exit".equals(command)){
System.exit(0);
}
}
public static void main(String[] args) {
new TestGui().setVisible(true);
}
}
如果有兴趣 可以使用SWT插件生成布局,这个就两个地方有知识点,一个布局,一个监听
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class TestGui extends JFrame implements ActionListener{
private JPanel pan;
private JTextField var1;
private JButton generate;
private JButton sin;
private JButton cos;
private JButton exit;
private JTextField var2;
private Random random = new Random();
public TestGui(){
this.setLayout(null);
pan = new JPanel();
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("计算器");
setBounds(200, 200, 400, 150);
var1 = new JTextField();
var2 = new JTextField();
var1.setBounds(5, 5, 350, 20);
pan.add(var1);
generate = new JButton("generate");
generate.setBounds(5, 30, 80, 25);
pan.add(generate);
generate.addActionListener(this);
sin = new JButton("sin");
sin.setBounds(95, 30, 80, 25);
pan.add(sin);
sin.addActionListener(this);
cos = new JButton("cos");
cos.setBounds(185, 30, 80, 25);
pan.add(cos);
cos.addActionListener(this);
exit = new JButton("exit");
exit.setBounds(275, 30, 80, 25);
pan.add(exit);
exit.addActionListener(this);
var2.setBounds(5, 60, 350, 20);
pan.add(var2);
pan.setBounds(0, 0, 400, 400);
pan.setLayout(null);
add(pan);
}
@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
// TODO Auto-generated method stub
if("generate".equals(command)){
float f = random.nextFloat();
double f2 = Math.PI * f;
var1.setText(f2+"");
}
if("sin".equals(command)){
var2.setText(Math.sin(Double.valueOf(var1.getText()))+"");
}
if("cos".equals(command)){
var2.setText(Math.cos(Double.valueOf(var1.getText()))+"");
}
if("exit".equals(command)){
System.exit(0);
}
}
public static void main(String[] args) {
new TestGui().setVisible(true);
}
}
如果有兴趣 可以使用SWT插件生成布局,这个就两个地方有知识点,一个布局,一个监听
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-06-13
展开全部
public class GUI extends JFrame implements MouseListener{
JButton jb1,jb2,jb3,jb4;
JTextField jt1,jt2;
public GUI(){
jb1 = new JButton("sin");
jb2 = new JButton("cos");
jb3 = new JButton("generate");
jb4 = new JButton("exit");
jt1 = new JTextField(16);
jt2 = new JTextField(16);
this.setLayout(new FlowLayout());
this.add(jb1);
this.add(jb2);
this.add(jb3);
this.add(jb4);
this.add(jt1);
this.add(jt2);
jb1.addMouseListener(this);
jb2.addMouseListener(this);
jb3.addMouseListener(this);
jb4.addMouseListener(this);
this.setSize(400,300);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(true);
}
public static void main(String[] args) {
GUI gui = new GUI();
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
final double n = Math.random()*Math.PI;
if(e.getSource() == jb3){
jt1.setText(n+"");
}
if(e.getSource() == jb1){
jt2.setText(Math.sin(n)+"");
}
if(e.getSource() == jb2){
jt2.setText(Math.cos(n)+"");
}
if(e.getSource() == jb4){
System.exit(0);
}
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
}
功能都实现了,界面有点粗糙,你可以改改,望采纳!
JButton jb1,jb2,jb3,jb4;
JTextField jt1,jt2;
public GUI(){
jb1 = new JButton("sin");
jb2 = new JButton("cos");
jb3 = new JButton("generate");
jb4 = new JButton("exit");
jt1 = new JTextField(16);
jt2 = new JTextField(16);
this.setLayout(new FlowLayout());
this.add(jb1);
this.add(jb2);
this.add(jb3);
this.add(jb4);
this.add(jt1);
this.add(jt2);
jb1.addMouseListener(this);
jb2.addMouseListener(this);
jb3.addMouseListener(this);
jb4.addMouseListener(this);
this.setSize(400,300);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(true);
}
public static void main(String[] args) {
GUI gui = new GUI();
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
final double n = Math.random()*Math.PI;
if(e.getSource() == jb3){
jt1.setText(n+"");
}
if(e.getSource() == jb1){
jt2.setText(Math.sin(n)+"");
}
if(e.getSource() == jb2){
jt2.setText(Math.cos(n)+"");
}
if(e.getSource() == jb4){
System.exit(0);
}
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
}
功能都实现了,界面有点粗糙,你可以改改,望采纳!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询