图形用户界面设计

更新时间:2024-02-02 04:47:02 阅读量: 教育文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

实验名称:图形用户界面及事件处理

一、实验目的

掌握常用GUI控制组件及其事件处理。

二、实验内容

1.编写一程序,创建菜单和工具栏,如下图所示。

2.编写一程序,可以动态地改变面板上显示信息的字体,效果如图所示。

用来显示文字信息。

3.编写应用程序,界面显示效果是下图所示的通信录,窗口左边是JTree类对象,窗口右边是一个文本区,

4.编写继承自JFrame类的窗口应用程序,如下图所示:窗口的中间是一个3行、4列的表格(设置表格为不可编

辑修改);窗口的南边放置按钮“求平均分”,点击该按钮则把表格中第4列的成绩取出,求平均分后并显示在弹出

信息对话框中(JOptionPane)。窗口的北边放置两个按钮“增加行”和“修改成绩”:点击按钮“增加行”,则可在表格中

新增1行,行号自动增加,并弹出输入对话框3次,用来输入并设置新行的“姓名”、“学号”和“成绩”;点击按钮“修把指定行号的成绩修改为输入的成绩。

改成绩”,则弹出输入对话框(JOptionPane),输入要要修改的哪一行的成绩,确定后弹出输入对话框输入成绩,并

三、实验程序的分析和调试

(1)

import java.awt.BorderLayout; import java.awt.Color; import java.awt.FlowLayout; import java.awt.Rectangle; import java.awt.Toolkit;

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

public class FQQJToolBar3 extends JFrame{

public FQQJToolBar3(){

init();

setTitle(\);

setIconImage(Toolkit.getDefaultToolkit().createImage(\));

}

setBounds( 400, 200, 450, 350); setResizable(true); setVisible(true);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//标题窗口退出的设置

void init() {

JPanel p = new JPanel(); JToolBar bar = new JToolBar();

BorderLayout bord = new BorderLayout(); p.setLayout(bord); p.add(\,bar);

JMenuBar myBar = new JMenuBar(); JMenu menu = new JMenu(\);

JMenuItem editItem1,editItem2,editItem3,editItem4;

editItem1 = new JMenuItem(\,new ImageIcon(\)); editItem2 = new JMenuItem(\,new ImageIcon(\)); editItem3 = new JMenuItem(\,new ImageIcon(\)); editItem4 = new JMenuItem(\,new ImageIcon(\));

editItem1.setAccelerator(KeyStroke.getKeyStroke('N',InputEvent.CTRL_MASK)); editItem2.setAccelerator(KeyStroke.getKeyStroke('O',InputEvent.CTRL_MASK)); editItem3.setAccelerator(KeyStroke.getKeyStroke('L',InputEvent.CTRL_MASK)); editItem4.setAccelerator(KeyStroke.getKeyStroke('X',InputEvent.CTRL_MASK)); menu.add(editItem1); menu.add(editItem2); menu.add(editItem3); menu.addSeparator(); menu.add(editItem4); myBar.add(menu); bar.add(myBar);

JButton key0 =new JButton(new ImageIcon(\)); JButton key1 =new JButton(new ImageIcon(\)); JButton key2 =new JButton(new ImageIcon(\)); JButton key3 =new JButton(new ImageIcon(\)); JButton key4 =new JButton(new ImageIcon(\)); JButton key5 =new JButton(new ImageIcon(\)); bar.add(key0); bar.add(key1); bar.add(key2); bar.add(key3); bar.add(key4); bar.add(key5);

JLabel label=new JLabel(\); bar.add(label);

JComboBox zi= new JComboBox(); zi.addItem(\); this.add(bar);

}

zi.addItem(\宋体\); zi.addItem(\黑体\); bar.add(zi);

JTextField text= new JTextField();

text.setBackground(Color.white);//文本区域的背景色 this.add(text);

p.add(\,text); this.setContentPane(p); this.setJMenuBar(myBar); }

public static void main(String[] args){

FQQJToolBar3 bar=new FQQJToolBar3(); }

(2)

import java.awt.BorderLayout; import java.awt.Color; import java.awt.Font; import java.awt.Toolkit;

import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.awt.event.KeyEvent; import javax.swing.JCheckBox; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.JToolBar;

public class FQQNAME extends JFrame{

JTextField text; String str=\宋体\; int index=30; public FQQNAME(){

init();

setTitle(\);

setIconImage(Toolkit.getDefaultToolkit().createImage(\)); setBounds( 400, 200, 450, 350); setResizable(true);

}

setVisible(true);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//标题窗口退出的设置

void init() {

JPanel p = new JPanel(); JToolBar bar = new JToolBar();

BorderLayout bord = new BorderLayout(); p.setLayout(bord); p.add(\,bar);

JLabel label1=new JLabel(\); bar.add(label1);

final JComboBox zi= new JComboBox(); zi.addItem(\); zi.addItem(\); zi.addItem(\宋体\);

zi.addItemListener(new ItemListener() {

public void itemStateChanged(final ItemEvent e) { }

String str = zi.getSelectedItem().toString(); if (str.equalsIgnoreCase(\)){

text.setFont(new Font(\, Font.BOLD, index));} else if (str.equalsIgnoreCase(\))

text.setFont(new Font(\, Font.BOLD,index)); else if (str.equalsIgnoreCase(\宋体\))

text.setFont(new Font(\宋体\, Font.BOLD, index));

this.add(bar);

});

bar.add(zi);

JLabel label2=new JLabel(\); bar.add(label2);

final JComboBox ziti= new JComboBox(); for(int i=10;i<=30;i++) ziti.addItem(i); ziti.addItemListener(new ItemListener() {

public void itemStateChanged(final ItemEvent e) { }

index= Integer.parseInt(ziti.getSelectedItem().toString()); text.setFont(new Font(str, Font.BOLD, index));

});

bar.add(ziti);

text= new JTextField(\);

text.setBackground(Color.yellow);//文本区域的背景色 text.setFont(new Font(\宋体\, Font.BOLD, 30));

text.setHorizontalAlignment(JTextField.CENTER);//文字右对齐 this.add(text);

}

p.add(\,text);

JCheckBox box1=new JCheckBox(\); JCheckBox box2=new JCheckBox(\); JCheckBox box3=new JCheckBox(\); box1.setSelected(true); box2.setSelected(true);

box1.setFont(new Font(\宋体\, Font.BOLD, 16));

box2.setFont(new Font(\宋体\, Font.BOLD, 16)); box3.setFont(new Font(\宋体\, Font.BOLD, 16)); }

public static void main(String[] args){

FQQNAME name=new FQQNAME(); }

box1.setMnemonic(KeyEvent.VK_C); box2.setMnemonic(KeyEvent.VK_B);

box3.setMnemonic(KeyEvent.VK_I);//用来给按钮设置助记符 JPanel sp=new JPanel(); sp.add(box1); sp.add(box2); sp.add(box3); p.add(\, sp); this.setContentPane(p);

(3)

import java.awt.BorderLayout; import java.awt.Color; import java.awt.Font; import java.awt.Rectangle; import java.awt.Toolkit; import javax.swing.*;

import javax.swing.border.Border; import javax.swing.event.*;

import javax.swing.tree.DefaultMutableTreeNode; public class FQQTXL extends JFrame{

JTable tabel; JTree tree=null;

public FQQTXL(){ }

init();

setTitle(\我的通讯录\);

setIconImage(Toolkit.getDefaultToolkit().createImage(\)); setBounds( 400, 200, 400, 300); setResizable(true); setVisible(true);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//标题窗口退出的设置

void init() {

DefaultMutableTreeNode node1 = new DefaultMutableTreeNode(\小学同学\); node1.add(new DefaultMutableTreeNode(new tongxue(\小花\))); node1.add(new DefaultMutableTreeNode(new tongxue(\小虎\))); node1.add(new DefaultMutableTreeNode(new tongxue(\小龙\)));

DefaultMutableTreeNode node2 = new DefaultMutableTreeNode(\中学同学\); node2.add(new DefaultMutableTreeNode(new tongxue(\小叶\))); node2.add(new DefaultMutableTreeNode(new tongxue(\小雯\))); node2.add(new DefaultMutableTreeNode(new tongxue(\小夏\)));

DefaultMutableTreeNode node3 = new DefaultMutableTreeNode(\大学同学\); node3.add(new DefaultMutableTreeNode(new tongxue(\张三\))); node3.add(new DefaultMutableTreeNode(new tongxue(\李四\))); node3.add(new DefaultMutableTreeNode(new tongxue(\李佳\))); DefaultMutableTreeNode top = new DefaultMutableTreeNode(\同学录\); top.add(node1); top.add(node2); top.add(node3);

final JTree tree = new JTree(top); add(tree);

JPanel p = new JPanel();

BorderLayout bord = new BorderLayout();

p.setLayout(bord);

final JTextArea text=new JTextArea();

text.setBackground(Color.white);//文本区域的背景色

text.setFont(new Font(\宋体\, Font.BOLD, 15));

// text.setHorizontalAlignment(JTextField.LEFT);//文字右对齐 add(text);

p.add(\,text); p.add(\,tree); this.setContentPane(p);

tree.addTreeSelectionListener(new TreeSelectionListener() {

@Override

public void valueChanged(TreeSelectionEvent e) {

DefaultMutableTreeNode node = (DefaultMutableTreeNode) if (node == null) return;

Object object = node.getUserObject(); if (node.isLeaf()) {

tongxue tong = (tongxue) object;

text.setText(\+\+\+tong.toString()+\+\联系电话:

tree.getLastSelectedPathComponent();

\+tong.toPhone()+\+\通信地址:\+ tong.toAddress()+\+\电子邮件:\+tong.email);

} } });

}

public static void main(String[] args){

}

}

FQQTXL txl=new FQQTXL();

public class tongxue {

String name=\张三\;

String phone=\; String address=\燕山大学理学院\; String email=\; public tongxue(String name) { }

public String toString() {

this.name=name;

return name; }

public String toPhone() { return phone; }

public String toAddress() { return address; }

public String toEmail() { } }

return email;

(4)

import java.awt.BorderLayout; import java.awt.FlowLayout; import java.awt.Font; import java.awt.Toolkit;

import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.text.DecimalFormat; import javax.swing.*;

import javax.swing.table.DefaultTableModel; public class FQQBG extends JFrame { int rows=3;

Object [] name={\行号\,\姓名\,\学号\,\成绩\};

Object [][] a={{new Integer(0),\,new Integer(111111),new Integer(92)},{new

Jack\,new Integer(222222),new Integer(82)},{new Integer(2),\,new

Integer(1),\

Integer(333333),new Integer(58)}};

DefaultTableModel tableModel = new DefaultTableModel(a,name); JTable table=new JTable(tableModel); public FQQBG(){

init();

setTitle(\表格使用\);

DecimalFormat df = new DecimalFormat(\);//设置保留两位小数

}

setIconImage(Toolkit.getDefaultToolkit().createImage(\)); setBounds( 400, 200, 400, 300); setResizable(true); setVisible(true);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//标题窗口退出的设置

void init() {

table.setEnabled(false); table.setRowHeight(30);

JScrollPane scrollPane = new JScrollPane(table);

//如果将其放置在JScrollPane中显示数据的话,表头会自动显示出来 JButton add=new JButton(\增加行 \); add.addActionListener(new AddListen()); add.setFont(new Font(\宋体\, Font.BOLD, 20)); JButton alter=new JButton(\修改成绩 \); alter.addActionListener(new AltListen()); alter.setFont(new Font(\宋体\, Font.BOLD, 20)); JButton ave=new JButton(\求平均分\); ave.addActionListener(new AveListen()); ave.setFont(new Font(\宋体\, Font.BOLD, 20)); JPanel sp = new JPanel(); sp.add(add); sp.add(alter);

sp.setLayout(new FlowLayout()); JPanel p = new JPanel(); p.setLayout(bord);

p.add(\,scrollPane);

//JTable组件显示数据时,如果直接将其放置在Frame的contentPane中则表头一行会显示不出来,

BorderLayout bord = new BorderLayout();

p.add(\,sp); p.add(\,ave); this.setContentPane(p); }

class AddListen implements ActionListener{

public void actionPerformed(ActionEvent e) {// defaultModel.addColumn(\增加列

rows=rows+1;

if(str!=null){

String str1=JOptionPane.showInputDialog(\输入该学生的学号\); if(str1!=null){

String str2=JOptionPane.showInputDialog(\输入该学生的成绩\); if(str2!=null){

String []rowValues = {String.valueOf(rows-1),str,str1,str2};

\

String str=JOptionPane.showInputDialog(\输入该学生的姓名\);

tableModel.addRow(rowValues); //添加一行 }}}}}

}

class AltListen implements ActionListener{ }

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

FQQBG bg=new FQQBG();

public void actionPerformed(ActionEvent e) { }

public void actionPerformed(ActionEvent e) {

double sum=0.0; }

boolean flag=true; for(int i=0;i

try{ }

JOptionPane.showMessageDialog(null, \平均成绩为:

sum=sum+Double.parseDouble(a[i][3].toString()); flag=false; table.repaint(); }catch(Exception ee){

String str=JOptionPane.showInputDialog(\输入要修改的行号\); }

if(str!=null)

try{

int n=Integer.parseInt(str);

String str1=JOptionPane.showInputDialog(\输入修改后的成绩\); if(str1!=null){ }

catch(Exception exp){}

tableModel.setValueAt(str1, n, 3);} //修改数值

class AveListen implements ActionListener{

if(flag==true)

\+df.format((sum/rows)));

四、实验结论和心得体会

(1)

本文来源:https://www.bwwdw.com/article/i92w.html

Top