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

面向对象程序设计实验报告java实验报告图形用户界面

来源:用户分享 时间:2025/5/15 14:52:08 本文由loading 分享 下载这篇文档手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

2. Modify the program so that there are two candidates to vote for—Joe and Sam. To do this you need to do the following:

a. Add variables for Sam—a vote counter, a button, and a label.

b. Add a new inner class named Sam Button Listener to listen for clicks on the button for Sam. Instantiate an instance of the class when adding the Action Listener to the button for Sam.

c. Add the button and label for Sam to the panel. 代码如下:

//************************************************************

//Vote Counter Panel.java //Demonstrates a graphical user interface and event listeners to

//tally votes for two candidates, Joe and Sam.

//*************************************************** package lab4;

import java.awt.*;

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

public class VoteCounterPanel extends JPanel {

private int votesForJoe,votesForSam; private JButton joe,Sam;

private JLabel labelJoe,labelSam; public VoteCounterPanel() {

votesForJoe = 0; votesForSam = 0;

joe = new JButton(\ Sam = new JButton(\

joe.addActionListener(new JoeButtonListener()); Sam.addActionListener(new SamButtonListener());

labelJoe = new JLabel(\ labelSam = new JLabel(\ add(joe);

add(labelJoe); add(Sam);

add(labelSam);

setPreferredSize(new Dimension(300, 80)); setBackground(Color.cyan); }

private class JoeButtonListener implements ActionListener {

public void actionPerformed(ActionEvent event) {

votesForJoe++;

labelJoe.setText(\ } }

private class SamButtonListener implements ActionListener {

public void actionPerformed(ActionEvent event) {

votesForSam++;

labelSam.setText(\ } } }

3、Compile and run the program.

点击按钮后:

4、以重用的思想实现该界面的代码如下: package lab4;

import java.awt.Color; import java.awt.Dimension; import java.awt.event.*;

import javax.swing.*;

public class VoteCounter1 extends JFrame ActionListener{

private JPanel VoteCounterPanel; private int votesForJoe,votesForSam; private JButton joe,Sam;

private JLabel labelJoe,labelSam;

public VoteCounter1() {

votesForJoe = 0;

implements votesForSam = 0;

VoteCounterPanel=new JPanel(); joe = new JButton(\ Sam = new JButton(\ joe.addActionListener(this); Sam.addActionListener(this);

labelJoe = new JLabel(\for Joe: \+ votesForJoe); labelSam = new JLabel(\for Sam: \+ votesForSam); VoteCounterPanel.add(joe); VoteCounterPanel.add(labelJoe); VoteCounterPanel.add(Sam); VoteCounterPanel.add(labelSam);

VoteCounterPanel.setPreferredSize(new Dimension(300, 80));

VoteCounterPanel.setBackground(Color.cyan); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); getContentPane().add(VoteCounterPanel); pack();

setVisible(true); }

public void actionPerformed(ActionEvent event)

面向对象程序设计实验报告java实验报告图形用户界面.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/c9bq0f6z1vw9o8y29wt5t6vudb8bhn000zco_3.html(转载请注明文章来源)
热门推荐
Copyright © 2012-2023 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top