浙江工业大学软件学院《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)在线全文阅读。
相关推荐: