求解(java)按要求编写如下图形用户界面程序,程序功能如下:

按要求编写如下图形用户界面程序,程序功能如下:1)单击“确定”按钮将各文本框中的内容添加显示到下方文本区域中。2)单击“保存”按钮将文本区域中内容保存到result.tx... 按要求编写如下图形用户界面程序,程序功能如下:
1) 单击“确定”按钮将各文本框中的内容添加显示到下方文本区域中。
2) 单击“保存”按钮将文本区域中内容保存到result.txt文件中。
展开
 我来答
18388138
2012-05-31 · TA获得超过1956个赞
知道小有建树答主
回答量:1104
采纳率:40%
帮助的人:747万
展开全部
写了一个多小时才写完的!
package baiduKnow;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileOutputStream;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/**
* @author 18388138
*
* @version 2012/5/31 0:20
*
* @proplemForm 百度知道
*/
public class Shiti2 extends JFrame implements ActionListener {

/**
* 序列化
*/
private static final long serialVersionUID = 1L;

//所需的面板
private JPanel basePanel = new JPanel();
private JPanel jpBaseInfo = new JPanel();
private JPanel jpScore = new JPanel();
private JPanel jpButton = new JPanel();
//所需的标签
private JLabel lblName = new JLabel(" 姓名");
private JLabel lblNumber = new JLabel("学号");
private JLabel lblMath = new JLabel(" 数学");
private JLabel lblEnglish = new JLabel(" 英语");
private JLabel lblSports = new JLabel(" 体育");
private JLabel lblTotalScore = new JLabel(" 总分");
//所需的文本框
private JTextField txtName = new JTextField("李四", 14);
private JTextField txtNumber = new JTextField("2011122002", 14);
private JTextField txtMath = new JTextField("60");
private JTextField txtEnglish = new JTextField("70");
private JTextField txtSports = new JTextField("80");
private JTextField txtTotalScore= new JTextField();
//所需的按钮
private JButton btnOK = new JButton("确定");
private JButton btnSave = new JButton("保存");
//所需的文本区
private JTextArea txaContext = new JTextArea();
private JScrollPane jspTextArea= new JScrollPane(txaContext);

public Shiti2() {

this.setTitle("试题二"); //设置标题
this.setLayout(new BorderLayout()); //顶层容器边框布局
basePanel.setLayout(new BorderLayout()); //控件容器边框布局

jpBaseInfo.setLayout(new FlowLayout()); //基本信息容器 流布局
//添加姓名学号控件
jpBaseInfo.add(lblName);
jpBaseInfo.add(txtName);
jpBaseInfo.add(lblNumber);
jpBaseInfo.add(txtNumber);
//将姓名学号容器添加到基本信息容器上边
basePanel.add(jpBaseInfo, BorderLayout.NORTH);

jpScore.setLayout(new GridLayout(1,8)); //分数容器 网络布局
//添加科目与分数控件
jpScore.add(lblMath);
jpScore.add(txtMath);
jpScore.add(lblEnglish);
jpScore.add(txtEnglish);
jpScore.add(lblSports);
jpScore.add(txtSports);
jpScore.add(lblTotalScore);
txtTotalScore.setEditable(false); //将总分设置为不可编辑文本框
jpScore.add(txtTotalScore);
//将分数容器添加到基本信息容器中间位置
basePanel.add(jpScore, BorderLayout.CENTER);

//设置按钮并注册监听器
jpButton.add(btnOK);
btnOK.addActionListener(this);
jpButton.add(btnSave);
btnSave.addActionListener(this);
//将按钮容器添加到基本信息容器下边
basePanel.add(jpButton, BorderLayout.SOUTH);

//将基本信息容器添加到顶层容器上边
this.add(basePanel, BorderLayout.NORTH);
txaContext.setLineWrap(true); //设置文本区自动换行
//将文本区添加到顶层容器中间
this.add(jspTextArea, BorderLayout.CENTER);
this.setSize(400, 300);
this.setLocationRelativeTo(null);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}

/**
* @param args
*/
public static void main(String[] args) {

new Shiti2();

}

@Override
public void actionPerformed(ActionEvent e) {

//如果点击了确定按钮
if(e.getSource().equals(btnOK)) {

int mathScore = Integer.parseInt(txtMath.getText());
int englishScore = Integer.parseInt(txtEnglish.getText());
int sportsScore = Integer.parseInt(txtSports.getText());
int sum = mathScore+ englishScore + sportsScore;
String strSum = String.valueOf(sum);

String allContext = txtName.getText() + "," + txtNumber.getText() + "," +txtMath.getText() + "," +txtEnglish.getText() + "," + txtSports.getText() + "," + strSum +"\n";

txtTotalScore.setText(strSum); //计算总分,将总分设置到相应的文本框

txaContext.append(allContext); //将所有信息添加到文本区

}

//如果点击了保存按钮
if(e.getSource().equals(btnSave)) {

String test=txaContext.getText(); //获取文本区内容
try {
FileOutputStream fos=new FileOutputStream("e:/test.txt"); //创建txt
fos.write(test.getBytes()); //将文本区内容写入txt
} catch (Exception e1) {
e1.printStackTrace();
}

}

}

}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
云飞の孤雁
2012-05-30 · 超过51用户采纳过TA的回答
知道答主
回答量:211
采纳率:0%
帮助的人:170万
展开全部
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class NewJFrame44 extends javax.swing.JFrame {
private JLabel jLabel1;
private JTextField jTextField1;
private JLabel jLabel2;
private JLabel jLabel3;
private JLabel jLabel4;
private JLabel jLabel5;
private JTextArea jTextArea1;
private JButton jButton1;
private JScrollPane jScrollPane1;
private JButton jButton2;
private JLabel jLabel6;
private JTextField jTextField4;
private JTextField jTextField3;
private JTextField jTextField2;

/**
* Auto-generated main method to display this JFrame
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
NewJFrame44 inst = new NewJFrame44();
inst.setLocationRelativeTo(null);
inst.setVisible(true);
}
});
}

public NewJFrame44() {
super();
initGUI();
}

private void initGUI() {
try {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
getContentPane().setLayout(null);
{
jLabel1 = new JLabel();
getContentPane().add(jLabel1);
jLabel1.setText("\u59d3\u540d");
jLabel1.setBounds(22, 38, 44, 15);
}
{
jTextField1 = new JTextField();
getContentPane().add(jTextField1);
jTextField1.setBounds(78, 35, 80, 22);
}
{
jLabel2 = new JLabel();
getContentPane().add(jLabel2);
jLabel2.setText("\u5b66\u53f7");
jLabel2.setBounds(170, 34, 48, 23);
}
{
jTextField2 = new JTextField();
getContentPane().add(jTextField2);
jTextField2.setBounds(230, 35, 138, 22);
}
{
jLabel3 = new JLabel();
getContentPane().add(jLabel3);
jLabel3.setText("\u6570\u5b66");
jLabel3.setBounds(12, 89, 43, 22);
}
{
jTextField3 = new JTextField();
getContentPane().add(jTextField3);
jTextField3.setBounds(73, 90, 49, 22);
}
{
jLabel4 = new JLabel();
getContentPane().add(jLabel4);
jLabel4.setText("\u82f1\u8bed");
jLabel4.setBounds(133, 91, 37, 21);
}
{
jTextField4 = new JTextField();
getContentPane().add(jTextField4);
jTextField4.setBounds(182, 91, 48, 22);
}
{
jLabel5 = new JLabel();
getContentPane().add(jLabel5);
jLabel5.setText("\u603b\u5206");
jLabel5.setBounds(242, 90, 42, 23);
}
{
jLabel6 = new JLabel();
Border border=BorderFactory.createEtchedBorder();
jLabel6.setBorder(border);
getContentPane().add(jLabel6);
jLabel6.setBounds(296, 91, 72, 22);
}
{
jButton1 = new JButton();
getContentPane().add(jButton1);
jButton1.setText("\u4fdd\u5b58");
jButton1.setBounds(199, 145, 73, 22);
jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
}
{
jButton2 = new JButton();
getContentPane().add(jButton2);
jButton2.setText("\u786e\u5b9a");
jButton2.setBounds(85, 145, 73, 22);
jButton2.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent evt) {
jButton2MouseClicked(evt);
}
});
}
{
jScrollPane1 = new JScrollPane();
getContentPane().add(jScrollPane1);
jScrollPane1.setBounds(12, 187, 356, 105);
{
jTextArea1 = new JTextArea();
jScrollPane1.setViewportView(jTextArea1);
jTextArea1.setText("jTextArea1");
}
}
pack();
this.setSize(388, 337);
} catch (Exception e) {
//add your error handling code here
e.printStackTrace();
}
}

private void jButton2MouseClicked(MouseEvent evt) {
System.out.println("jButton2.mouseClicked, event="+evt);
//TODO add your code for jButton2.mouseClicked
String a=jTextField3.getText();
String b=jTextField4.getText();
double x=Double.parseDouble(a);
double y=Double.parseDouble(b);
jLabel6.setText((x+y)+"");
jTextArea1.setText( jTextField1.getText()+","+
jTextField2.getText()+","+
jTextField3.getText()+","+
jTextField4.getText()+","+
jLabel6.getText());
}

private void jButton1ActionPerformed(ActionEvent evt) {
System.out.println("jButton1.actionPerformed, event="+evt);
//TODO add your code for jButton1.actionPerformed
File file=new File("c:\\result.txt");
try {
FileOutputStream fout=new FileOutputStream(file);

FileWriter os = new FileWriter(file);
os.write(jTextArea1.getText());
os.flush();
os.close();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
alwing
2012-05-31 · TA获得超过132个赞
知道小有建树答主
回答量:294
采纳率:0%
帮助的人:137万
展开全部
没分
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式