import javax.swing.*;
import java.awt.*;
public class java3 extend Jbutton{
public java3(Icon icon,Icon pressed,Icon rollover){
this(icon);
setFocusPainted(false);
setRolloverEnabled(true);
setRolloverlcon(rollover);
setPressedIcon(pressed);
setBorderPainted(false);
setContentAreaFilled(false);
}
public static void main(String[]args){
java3 b1=new java3(
new ImageIcon("java3-redcube.gif"),
new ImageIcon("java3-redpaw.gif"),
new ImageIcon("java3-reddiamond.gif"));
java3 b2一new java3(
new ImageIcon("java3-bluecube.gif")。
new ImageIcon("java3-bluepaw.gif"),
new ImageIcon("java3-bluediamond.gif"));
JFrame. f=new JFrame();
f.setTitle("java3");
f.setDefauhCloseOperation(JFrame.EXIT_ON_
CLOSE);
c=f.getContentpane();
c.setLayout(new FlowLayout());
c.add(b1);
c.add(b2);
f.pack();
f.setVisible(true);
第1题
话框上有两个按钮“Yes"和“N0”,单击对话框上的“Yes”和“N0”按 钮后返回主窗口,并在右侧文本域中显示刚才所单击的按钮信息。
import java.awt.event.*;
import java.awt.*;
class MyDialog implements ActionListener
{static final int YES=1,N0=0;
int message=-1;Button yes,no;
MyDialog(Frame. f.String S,boolean b)
{super(f,S,b);
ves=new Button("Yes");yes.addActionListener
(this);
no=new Button("No"); no.addActionListener
(this)o
setLayout(new FlowLayout());
add(yes);add(no);
setBounds(60,60,100,100);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{message=-1;setVisible(false);)
});
}
public void actionPerformed(ActionEvent e)
{if(e.getSource()= =yes)
{message=YES;
setVisible(false);
}
else if(e.getSource()= = no)
{message=NO;
setVisible(false);
}
}
public int getMessage()
{return message;
}
}
class Dwindow extends Frame. implements ActionLis-
tener
{TextArea text;Button button;MyDialog dialog;
Dwindow(String s)
{super(s);
text=new TextArea(5,22);button=new Button
("打开对话框");
button.addActionListener(this);
setLayout(new FlowLayout());
add(button);add(text);
dialog=new MyDialog(this,"Dialog",true);
setBounds(60,60,300,300);setVisible(true);
validate();
addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{System.exit(0);}
});
}
public void actionPerformed(ActionEvent e)
{if(e.getSource()= =button)
{
if(dialog.getMessage()= =MyDialog.YES)
{text.append("\n你单击了对话框的yes按
钮");
}
else if(dialog.getMessage()= =MyDialog.NO)
{text.append("\n你单击了对话框的N0按
钮");
}
}
}
}
public class java2
{public static void main(String args[])
{new Dwindow("java2");
}
}
第2题
本题中定义了一个树型的通信录,窗El左侧是一个树,
右侧是一个文本域,单击树的结点,则在右侧文本域中显示
相关信息,如果单击的是树结点,则显示对应名字的电话
信息。
import javax.swing.*;
import javax.swing.tree.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
class Mytree2 extends JFrame
{JTree tree=null;JTextArea text=new JTextArea
(20,20);
Mytree2()
{Container con=getContentPane();
DefauhMutableTreeNode root=new Default-
MutableTreeNode("同学通信录");
DefaultMutableTreeNode tl=new Default-
MutableTreeNode("大学同学");
DefaultMutableTreeNode t2=new Default-
MutableTreeNode("研究生同学");
DefaultMutableTreeNode tl l=new Default-
MutatleTreeNode("陈艳");
DefaultMutableTreeNode tl 2=new Default-
MutableTreeNode("李小永");
DefaultMutableTreeNode t2 1=new Defauh-
MutableTreeNode("王小小");
DefauhMutableTreeNode t2 2=new Defauh-
MutableTreeNode("董小");
setTitle("java2");
root.add(t1);root.add(t2);
tl.add(t1_1);tl.add(t1_2);t2.add(t2_1);t2.
add(t2_2);
tree=new JTree(root);
JSerollPane scrollpane=new JScrollPane(text);
JSplitPane splitpane=new JSplitPane(JSplitPane.
HORIZONTAL SPLIT,
true,tree,scrollpane);
tree.addTreeSeleetionListener(this);
con.add(splitpane);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{System.exit(0);}});
setVisible(true);setBounds(70,80,200,300);
}
public void valueChanged(TreeSelectionEvent e)
{if(e.getSouree()= =tree)
{DefauhMutableTreeNode node=
(DefaultMutableTreeNode)tree.getLastSelected-
PathComponent();
if(node.isLeaf())
(String str= ;
if(str.equals("陈艳"))
(text.setText(str+":联系电话:0411-
4209876");}
else if(str.equals("李小永"))
{text.setText(str+":联系电话:010-
62789876");}
else if(str.equals("王小小"))
{text.setText(str+":联系电话:0430-
63596677");)
else if(str.equals("董小"))
{text.setText(str+":联系电话:020-
85192789");}
}
else
{text.setText(node.getUserObject().toString
());
}
}
}
}
public class java2
{public static void main(String args[])
{Mytree2 win=new Mytree2();win.pack();}
}
第3题
本题是一个Applet,它的功能是在窗口上添加12×12个标签,并且横向和纵向标签的颜色为黑白相间。
import java.applet.*;
import java..awt.*;
import java,.awt.event*;
pubhc class java2
extends Applet
{GridLayout grid;
pubhc void init()
{grid=new GridLayout(12,12);
setLayout(grid);
Label =new Label[12][12];
for(int i=0;i<;12;i++)
{for(int j=0;j<;12;j++)
{label[i][j]= ;
if((i+j)%2= =0)
label[i][j].setBackground(Color.black);
else
label[i][j].setBackground(color.white);
add(label[i][j]);
}
}
}}
第4题
文本域中显示该整数的平方值;如果在文本框中输入的不是一个整数,将弹出一个警告窗口。
tmport java.awt.event.*:
import java.awt.*;
import javax.swing.JOptionPane;
class Dwindow extends Frame. implements ActionLis-
tener
{TextField inputNumber;
TextArea show;
Dwindow(String s)
{super(s):
inputNumber=new TextField(22);
inputNumber.addActionListener(this);
show=new TextArea();
add(inputNumber,BorderLayout.NORTH);
add(show,BorderLayout.CENTER);
setBounds(60,60,300,300);setVisible(true);
validate();
addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e)
{boolean boo=false;
if(e.getSource()= =inputNumber)
{String s= ;
char a[]=S.toCharArray();
for(int i=0;i<;a.length;i++)
{if(!(Character.isDigit(a[i])))
boo=true;
}
if(boo= =true)
{JOptionPane.showMessageDialog(this,"您输入
了非法字符","警告对话框",
);
inputNumber.setText(null);
}
else if(boo= =false)
{int number=Integer.parseInt(s);
show.append("\n"+number+"平方:"+(num-
bet*number)):
}
}
}
}
public class java2
{public static void main(String args[])
{ new Dwindow("java2");
}
}
第5题
如显示为"鼠标的当前位置:X:Y"(其中,X为横坐标,Y为纵坐标)。
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class java2
{
public static void main(String[]args)
{
MouseFrame. frame=new MouseFrame();
frame.setDefaultCloseoperation(JFrame.EXIT_
0N CLOSE);
frame.show();
}
}
class MouseFrame. extends JFrame
{
public MouseFrame()
{
setTitle("java2");
setSize(WIDTH,HEIGHT);
MousePanel panel=new MousePanel();
Container contentPane=getContentPane();
contentPane.add(panel);
}
public static final int WIDTH = 300;
public static final int HEIGHT=200;
}
class MousePanel extends JPanel
{
public MousePanel()
{
addMouseListener(new MouseHandler());
addMouseMotionListener(new MouseMotionHan-
dler());
}
public void paintComponent(Graphics g)
(
super.paintComponent(g);
String text="鼠标指针位置:"+mousex+":"
+mousey;
g.drawString(text,10,10);
}
private int mousex,mousey;
private class MouseMotionHandler {
public void mouseMoved(MouseEvent event)
{
mousex=event.getX();
mousey=event.getY();
repaint();
}
public void mouseDragged(MouseEvent event)
{
mousex=event.getX();
mousey=event.getY();
repaint();
}
}
private class MouseHandler
{
public void mousePressed(MouseEvent eveat)
{mousex=event.getX();
mousey=event.getY();
}
}
}
第6题
本题的功能是计算l~10之间除了5以外的各个自然数的和。
public class javal{
public static void main(String[]args){
int i=1
int sum=0;
while(i<;=10){
if(i= =5){
;
;
}
;
i++;
}
System.out.println("sum="+sum);
}
}
第7题
本题将一个整形变量opl的二进制的低3位全部变成1。
public class javal{
public static void main(String[]args){
pl=21;
int op2=7:
res;
System.out.println(res);
}
}
第8题
sum,并将结果返回。程序中调用add()方法求整数24和34的和,并将结果打印输出。
public class javal{
public static void main(String[]args){
int a=24,b=34;
System.out.println(add(a,b));
}
public static int add( ){
;
sum—a+b;
;
}
}
第9题
本题将数组中arr[]={5,6,3,7,9,1}的各个元素按下标的逆序输出。
public class javal{
public static void main(String[]args){
int arr[]={5,6,3,7,9,1};
;
n= ;
while(n>;=O){
System.OUt.print(arr[n]+"");
;
}
}
}
第10题
值,并输出。
public class javal{
public static void main(String[]args){
int arr[][]={{34,21,45,67,20),{23,10,3,45,
76},{22,3,79,56,50}};
int i=0:
intj=0;
int min=arr[0][0];
while(i<;3){
while( ){
if(arr[i][j]<;min)
min=arr[i][j];
j++;
}
;
;
}
System.out.println("The min:"+min);
}
}
为了保护您的账号安全,请在“上学吧”公众号进行验证,点击“官网服务”-“账号验证”后输入验证码“”完成验证,验证成功后方可继续查看答案!