77范文网 - 专业文章范例文档资料分享平台

《Java程序设计》参考答案1(5)

来源:网络收集 时间:2019-01-26 下载这篇文档 手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:或QQ: 处理(尽可能给您提供完整文档),感谢您的支持与谅解。点击这里给我发消息

浙江工业大学软件学院《Java程序设计》练习题参考答案——赵小敏 自编

版权所有 不得转载 违者必究

}

public static void main(String args[]){ new TestButton(); } }

如果用Swing组件实现,则参考代码如下: import java.awt.*;

import java.awt.event.*; import javax.swing.*;

public class SwingApplication extends JFrame implements ActionListener{ JButton jb1,jb2; JTextField jt;

public SwingApplication() { super(\ jb1=new JButton(\ jb2=new JButton(\ jt=new JTextField(20);

jb1.addActionListener(this); jb2.addActionListener(this); Container c=getContentPane(); c.setLayout(new FlowLayout()); c.add(jt); c.add(jb1); c.add(jb2);

setSize(250,100); setVisible(true); }

public void actionPerformed(ActionEvent e){ if(e.getSource()==jb1){

jt.setText(jb1.getActionCommand()); }

if(e.getSource()==jb2){

jt.setText(jb2.getActionCommand()); } }

public static void main(String args[]){

SwingApplication sa=new SwingApplication();

sa.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }

22、参考代码如下:

第 21 页 共 32 页

浙江工业大学软件学院《Java程序设计》练习题参考答案——赵小敏 自编

版权所有 不得转载 违者必究

import java.awt.*; import javax.swing.*;

public class ColorSelect extends JFrame { private JButton ok, cancel;

private JCheckBox background, foreground; private JComboBox colorList; private JPanel panel, panel2; private Container c; public ColorSelect(){

super( \ c=getContentPane();

c.setLayout(new BorderLayout());

colorList = new JComboBox(); colorList.addItem( \

c.add( colorList, BorderLayout.NORTH ); panel = new JPanel();

background = new JCheckBox( \ foreground = new JCheckBox( \ panel.add( background ); panel.add( foreground );

c.add( panel, BorderLayout.CENTER ); ok = new JButton( \

cancel = new JButton( \ panel2 = new JPanel(); panel2.add( ok ); panel2.add( cancel ); panel2.add( cancel );

c.add( panel2, BorderLayout.SOUTH ); setSize( 300, 125 ); setVisible(true); }

public static void main ( String args[] ){ ColorSelect app = new ColorSelect();

app.setDefaultCloseOperation( EXIT_ON_CLOSE ); } }

24、参考代码如下: import java.awt.*;

import java.awt.event.*; import javax.swing.*;

public class Convert1 extends JFrame { private JPanel panel;

第 22 页 共 32 页

浙江工业大学软件学院《Java程序设计》练习题参考答案——赵小敏 自编

版权所有 不得转载 违者必究

private JLabel label1; private JTextField a; private JTextField b; private JButton button; private int result;

public Convert1() { super( \

label1 = new JLabel( \ a = new JTextField( 10 ); b = new JTextField( 10 ); button =new JButton(\

button.addActionListener( new ActionListener() {

public void actionPerformed(ActionEvent e) { int left = Integer.parseInt(a.getText() ); int right = Integer.parseInt(b.getText() );

result = left+right;

label1.setText(String.valueOf(result)); } } );

panel = new JPanel();

panel.add( a ); panel.add(b);

panel.add(label1); panel.add(button);

Container container = getContentPane(); container.add( panel); setSize( 225, 150 ); show(); }

public static void main ( String[] args ) { Convert1 app = new Convert1();

app.setDefaultCloseOperation( EXIT_ON_CLOSE ); }

第 23 页 共 32 页

浙江工业大学软件学院《Java程序设计》练习题参考答案——赵小敏 自编

版权所有 不得转载 违者必究

}

25、参考代码如下: import java.awt.*;

import java.awt.event.* ;

public class ButtonDemo implements ActionListener{ private Button ok,cancel; public ButtonDemo(){

Frame f = new Frame(\测试按钮\ f.setLayout(new FlowLayout()); ok = new Button(\

ok.addActionListener(this); cancel= new Button(\ cancel.addActionListener(this); f.add(ok); f.add(cancel);

f.setSize(200,100); f.setVisible(true) ; }

public void actionPerformed(ActionEvent e){ if(e.getSource()==ok){

System.out.println(\您按了OK按钮!\ } else{

System.out.println(\您按了Cancel按钮!\ } }

public static void main(String args[ ]){ new ButtonDemo(); } }

如果用Swing组件实现,则参考代码如下: import javax.swing.*;

import javax.swing.event.*; import java.awt.*;

import java.awt.event.* ;

public class JButtonDemo implements ActionListener{ private JButton ok,cancel; private JTextField tf; public JButtonDemo(){

JFrame f = new JFrame(\测试按钮\ f.setLayout(new FlowLayout());

第 24 页 共 32 页

浙江工业大学软件学院《Java程序设计》练习题参考答案——赵小敏 自编

版权所有 不得转载 违者必究

Container c=f.getContentPane();

tf=new JTextField(20); ok = new JButton(\

cancel= new JButton(\

ok.addActionListener(this);

cancel.addActionListener(this); c.add(tf); c.add(ok); c.add(cancel);

f.setSize(200,100); f.setVisible(true) ; }

public void actionPerformed(ActionEvent e){ if(e.getSource()==ok){

tf.setText(\您按了OK按钮!\ } else{

tf.setText(\您按了Cancel按钮!\ } }

public static void main(String args[ ]){ new JButtonDemo(); } } 26、

import java.io.*; import java.awt.*;

import java.awt.event.*;

class TextEdit extends Frame implements ActionListener{ TextArea text;

Button save,cancel,exit;

String fileName=\ public TextEdit(){

setTitle(\文本编辑器\ text=new TextArea();

text.setText(readFile(fileName)); save=new Button(\保 存 \ save.addActionListener(this); cancel=new Button(\取 消 \ cancel.addActionListener(this); exit=new Button(\退 出 \

第 25 页 共 32 页

百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库《Java程序设计》参考答案1(5)在线全文阅读。

《Java程序设计》参考答案1(5).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印 下载失败或者文档不完整,请联系客服人员解决!
本文链接:https://www.77cn.com.cn/wenku/zonghe/445641.html(转载请注明文章来源)
Copyright © 2008-2022 免费范文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ: 邮箱:tiandhx2@hotmail.com
苏ICP备16052595号-18
× 注册会员免费下载(下载后可以自由复制和排版)
注册会员下载
全站内容免费自由复制
注册会员下载
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: