1、代码如下:package TestChange;import java.awt.BorderLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JTextField;public class TestChange extends JFrame implements ActionListener pr
2、ivate static final long serialVersionUID = -3684503858019589006L; JPanel panel; JTextField tt1; JTextField tt2; JButton button; public TestChange() this.setTitle(TestChange); this.setSize(355, 85); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); panel = new JPanel(); tt1 = new JTextField(10); t
3、t2 = new JTextField(10); button = new JButton(Change button.addActionListener(this); panel.add(tt1); panel.add(tt2); panel.add(button); this.add(panel, BorderLayout.NORTH); this.setResizable(false); this.setVisible(true); public static void main(String str) new TestChange(); Override public void act
4、ionPerformed(ActionEvent e) if(e.getSource() = button) String temp = tt1.getText(); tt1.setText(tt2.getText(); tt2.setText(temp);2. 采用图形界面设计如下图2所示的界面。并能够实现当点击“+”按钮时,标签给出点击次数,如图3所示图2 开始界面图3 点击“+”按钮后的界面3教材P220页,第3题,创建一个会员注册窗体,要求使用上JCheckBox,JRadioButton,JTextFiled,JTextArea等组件,并将用户的注册信息显示到文本区中。package
5、 java实验六;import java.*;import javax.swing.*;import java.awt.event.*;import java.awt.*;public class VipDemo extends JFrame implements ActionListener private Color c; private int style; private Font f,fl; private JLabel jlExample,jlFontstyle,jlColor,jlname,jlid,jlphone,jlsex; private JButton jbOk,jbCa
6、ncel; private JRadioButton rbRed,rbBlue,rbBlack,rbm,rbf; private ButtonGroup g; private JCheckBox jcbBold,jcbItalic; private JPanel jpFontstyle,jpColor,jpLabel,jpsex; private JTextField jtname,jtid,jtphone; private JTextArea jta; public VipDemo() style=Font.PLAIN; jlExample=new JLabel(请看设置效果 jlFonts
7、tyle=new JLabel(字形 jlColor=new JLabel(颜色 jlsex=new JLabel(性别 jlname=new JLabel(名字 jlid=new JLabel(编号 jlphone=new JLabel(联系方式 jbOk=new JButton(确定 jbCancel=new JButton(取消 rbRed=new JRadioButton( rbBlue=new JRadioButton( rbBlack=new JRadioButton( rbm=new JRadioButton(男 rbf=new JRadioButton(女 g=new Butt
8、onGroup(); jcbBold=new JCheckBox(粗体 jcbItalic=new JCheckBox(斜体 jpFontstyle=new JPanel(); jpColor=new JPanel(); jpLabel=new JPanel(); jpsex=new JPanel(); jtname=new JTextField(); jtid=new JTextField(); jtphone=new JTextField(); jta=new JTextArea(4,15); public void launchButtonTest() jta.setEditable(f
9、alse); f=jlExample.getFont(); fl=f.deriveFont(Font.PLAIN); jlExample.setFont(fl); rbRed.setBackground(Color.red); rbBlue.setBackground(Color.blue); rbBlack.setBackground(Color.black); rbRed.addActionListener(this); rbBlue.addActionListener(this); rbBlack.addActionListener(this); jcbBold.addActionLis
10、tener(this); jcbItalic.addActionListener(this); rbm.addActionListener(this); rbf.addActionListener(this); jbOk.addActionListener(this); jbCancel.addActionListener(this); g.add(rbRed); g.add(rbBlue); g.add(rbBlack); g.add(rbm); g.add(rbf); jpFontstyle.add(jlFontstyle); jpFontstyle.add(jcbBold); jpFon
11、tstyle.add(jcbItalic); jpColor.add(jlColor); jpColor.add(rbRed); jpColor.add(rbBlue); jpColor.add(rbBlack); jpLabel.add(jlExample); jpsex.add(rbm); jpsex.add(rbf); Container cp=getContentPane(); cp.setLayout(new GridLayout(7,2,5,5); cp.add(jpFontstyle); cp.add(jpColor); cp.add(jpLabel); cp.add(jpsex
12、); cp.add(jlname); cp.add(jtname); cp.add(jlid); cp.add(jtid); cp.add(jlphone); cp.add(jtphone); cp.add(jbOk); cp.add(jbCancel); cp.add(jta); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pack(); setVisible(true); public void actionPerformed(ActionEvent e) if(rbRed.isSelected() c=Color.red; if(rbB
13、lue.isSelected() c=Color.blue; if(rbBlack.isSelected() c=Color.black; jlExample.setForeground(c); Object source=e.getSource(); if(source=jcbBold) style=styleFont.BOLD; if(source=jcbItalic) style=styleFont.ITALIC; jlExample.setFont(fl.deriveFont(style); if(source=rbm) jta.append(男n if(source=rbf) if(
14、source=jbOk)姓名 +jtname.getText()+n+jtid.getText()+联系方式 +jtphone.getText()+ public static void main(String args) VipDemo bt=new VipDemo(); bt.setTitle(会员注册 bt.launchButtonTest();4 利用相应的组件编辑一个文本编辑器,参照Windows记事本,可以对字体大小、字形(粗体,斜体)、颜色进行设置,可以对内容进行添加、删除、修改,并保存文本。 说明:(1)可以选用单选框、复选框、列表、下拉框或菜单任意一种或多种组合实现字体的设置 (2)根据自己的编程能力实现功能,可以只实现字体的设置,或者字体和内容的编辑,或者全部功能都实现。 (3)本题的目的是常用组件的使用。III 在本次实验中的问题和解决方法 本次实验中一切的设计和实现还比较理想,没遇上什么难题。IV心得体会通过本次实验我了解了图形用户界面的设计方法,掌握了常用的构建用户界面的组件的用法,掌握事件操作的原理,能够对所设计的用户界面进行事件处理,能够应用运算符解决实际小问题,熟悉了Java的面向对象的编程思想
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2