Java求救求救。

Java求救求救。,第1张

Java参考源代码:

import javaawtColor;

import javaawtFlowLayout;

import javaawtGraphics;

import javaawtPoint;

import javaawteventMouseAdapter;

import javaawteventMouseEvent;

import javaxswing;


public class Demo extends JFrame {

private Point point = new Point();

private int flag = 0;


public Demo() {

thissetLayout(new FlowLayout());

thissetTitle("测试");

thissetBounds(200, 200, 600, 400);

thissetDefaultCloseOperation(JFrameDISPOSE_ON_CLOSE);

thissetVisible(true);


thisaddMouseListener(new MouseAdapter(){

public void mousePressed(MouseEvent e){

pointx = egetX();

pointy = egetY();


int count = egetClickCount();

if(count == 1) {

if(egetButton()== eBUTTON1) {

flag = 1;

}

if(egetButton()==eBUTTON3) {

flag = 2;

}

else if(count == 2) {

flag = 0;

}


repaint();

}

});

}


@Override

public void paint(Graphics g) {

superpaint(g);


switch(flag) {

case 1:

gsetColor(Colorgreen);

gdrawArc((int)pointgetX(), (int)pointgetY(), 100, 100, 0, 360);

break;

case 2:

gsetColor(Colorred);

gdrawRect((int)pointgetX(), (int)pointgetY(), 100, 100);

break;

default:

break;

}


}


public static void main(String[] args) {

new Demo();

}

}

运行测试:

分类: 电脑/网络 >> 程序设计 >> 其他编程语言
问题描述:

import javaawt;

import javaawtevent;

import javaxswing;

自定义异常
class NumException extends Exception{

public String toString(){

return "输入的值太小";

}

public String shuru(){

return "请输入数字";

}

}
class jisuanqi extends JFrame implements ActionListener,ItemListener{

JRadioButton rz;

JRadioButton rm;

ButtonGroup bg;

JTextField txt;

JButton btnj;

String msg;

jisuanqi(){

super("计算器");

rz=new JRadioButton("周长");

rm=new JRadioButton("面积");

bg=new ButtonGroup();

txt=new JTextField();

btnj=new JButton("计算");

msg="";

thisgetContentPane()setLayout(new GridLayout(2,2));

bgadd(rz);

bgadd(rm);

thisgetContentPane()add(rz);

thisgetContentPane()add(rm);

thisgetContentPane()add(txt);

thisgetContentPane()add(btnj);

thissetSize(200,200);

rzaddItemListener(this);

rmaddItemListener(this);

btnjaddActionListener(this);

}

实现接口

public void actionPerformed(ActionEvent e) throws NumException{声明异常

if (DoubleparseDouble(txtgetText())<=0){

throw new NumException();抛出异常

}

if (msgequals("周长")){

txtsetText(""+(3142DoubleparseDouble(txtgetText())));

}

if (msgequals("面积")){

txtsetText(""+314(DoubleparseDouble(txtgetText())DoubleparseDouble(txtgetText())));

}

}

实现接口

public void itemStateChanged(ItemEvent e){

if (egetSource()==rz){

msg="周长";

}

if (egetSource()==rm){

msg="面积";

}

}

public static void main(String args[]){

try{

jisuanqi js=new jisuanqi();

jssetVisible(true);

}

catch(NumException ex){

Systemoutprintln(ex);

}

}

}

编译时出现:

C:\Documents and Settings\Administrator\桌面\新建文件夹\jisaunqijava:45: jisuanqi 中的 actionPerformed(javaawteventActionEvent) 无法实现 javaawteventActionListener 中的 actionPerformed(javaawteventActionEvent);被覆盖的方法不抛出 NumException

解析:

这段代码有问题:

实现接口

public void actionPerformed(ActionEvent e) throws NumException{声明异常

public void actionPerformed(ActionEvent e)不应该抛出异常, ActionListener接口函数没有异常抛出

一种可能的解决方法是把判断移到另外一个函数中,然后抛出异常,在actionPerformed中捕获或者在外面捕获

panel1add(new JComboBoxModelTest());
问题在于这句,抛出了异常
异常为:
javalangIllegalArgumentException: adding a window to a container
JComboBoxModelTest继承了JFrame
不能将一个窗口添加到一个容器中这是不对的
ps与二楼说的无关

这是不是你想要的效果呢

if(s1equals("♂")){//在这个里面我想按下这个键能产生一个女生喜欢的颜色

for (int i=0;i<slength;i++){

b[i]setBackground(Colorpink);

b[i]setFont(new Font("宋体",FontPLAIN,15));

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/13319239.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-07-14
下一篇 2023-07-14

发表评论

登录后才能评论

评论列表(0条)

保存