高分求JAVA题目注释2(800分了合起)

2024-12-31 00:31:40
推荐回答(5个)
回答1:

送给你更加详细的注释:

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

public class PopupTest extends JFrame {
private JRadioButtonMenuItem items[]; //建立一个单选按钮组
private final Color colorValues[] = { Color.GREEN, Color.YELLOW, Color.RED }; //建一个Color数组,并填充需要的颜色
private JPopupMenu popupMenu; //建一个弹出式菜单项
/**初始化构造器*/
public PopupTest() {
super("弹出式菜单的简单使用"); //调用父类的构造函数,并传递字符串参数.
popupMenu = new JPopupMenu(); //实例化弹出式菜单项
String colors[] = { "Green", "Yellow", "Red" }; //建立一个字符串数组
ButtonGroup colorGroup = new ButtonGroup(); // 建一个按钮组
items = new JRadioButtonMenuItem[colors.length]; //初始化单选按钮组
ItemHandler handler = new ItemHandler(); // 实例化 监听器
for (int count = 0; count < items.length; count++) { //加入到单选按钮组当中,并把他们加入到弹出菜单里
items[count] = new JRadioButtonMenuItem(colors[count]); //,再把他们放入按钮组中,然后加上监听器
popupMenu.add(items[count]);
colorGroup.add(items[count]);
items[count].addActionListener(handler);

}
getContentPane().setBackground(Color.WHITE); //设置背景颜色
this.addMouseListener(new MouseAdapter() { //加入鼠标监听
public void mousePressed(MouseEvent event) { //鼠标按下调用checkForTriggerEvent()函数
checkForTriggerEvent(event);
}

public void mouseReleased(MouseEvent event) {//鼠标松开调用checkForTriggerEvent()函数
checkForTriggerEvent(event);

}

private void checkForTriggerEvent(MouseEvent event) { // 在当前位置显示,弹出菜单
if (event.isPopupTrigger())
popupMenu.show(event.getComponent(), event.getX(), event
.getY());
}
});
setSize(300, 200); //设置窗口大小
setVisible(true); //将窗口设为显示
}

public static void main(String args[]) {
PopupTest application = new PopupTest(); //实例化类
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //设置程序窗口类型
}

private class ItemHandler implements ActionListener { //创建监听器
public void actionPerformed(ActionEvent event) {
for (int i = 0; i < items.length; i++) //更改背景颜色
if (event.getSource() == items[i]) {
getContentPane().setBackground(colorValues[i]);
}
}
}
}

回答2:

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

public class PopupTest extends JFrame implements ActionListener {

private static final long serialVersionUID = 1L;
private JRadioButtonMenuItem items[];
private final Color colorValues[] = { Color.GREEN, Color.YELLOW, Color.RED };
//声明菜单对象
private JMenu jm;

public PopupTest() {
super("弹出式菜单的简单使用");
//定义一个菜单栏,用来添加相应的菜单
JMenuBar bar = new JMenuBar();
//初始化菜单对象
jm = new JMenu(" Change Color");
String colors[] = { "Green", "Yellow", "Red" };
ButtonGroup colorGroup = new ButtonGroup();
items = new JRadioButtonMenuItem[colors.length];
for (int count = 0; count < items.length; count++) {
items[count] = new JRadioButtonMenuItem(colors[count]);
jm.add(items[count]);
colorGroup.add(items[count]);
//为每一个菜单项注册时间监听器
items[count].addActionListener(this);
}
//将菜单添加到菜单栏对象中
bar.add(jm);
//将菜单栏添加到JFrame中
super.setJMenuBar(bar);
getContentPane().setBackground(Color.WHITE);
setSize(300, 200);
setVisible(true);
}

public static void main(String args[]) {
PopupTest application = new PopupTest();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

//具体的事件处理,重写了接口ActionListener中的该方法
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < items.length; i++)
if (e.getSource() == items[i]) {
getContentPane().setBackground(colorValues[i]);
}
}

}

回答3:

你的问题我已经在不知道哪个页上回了,你去看下吧

回答4:

你去下载 java写的俄罗斯方块游戏 哪有你相同的地方

回答5:

把分给我吧,哥哥!