本题中定义了一个树型的通信录,窗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();}
}
第1题
本题是一个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]);
}
}
}}
第2题
文本域中显示该整数的平方值;如果在文本框中输入的不是一个整数,将弹出一个警告窗口。
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");
}
}
第3题
如显示为"鼠标的当前位置: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();
}
}
}
第4题
本题的功能是计算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);
}
}
第5题
本题将一个整形变量opl的二进制的低3位全部变成1。
public class javal{
public static void main(String[]args){
pl=21;
int op2=7:
res;
System.out.println(res);
}
}
第6题
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;
;
}
}
第7题
本题将数组中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]+"");
;
}
}
}
第8题
值,并输出。
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);
}
}
第9题
按钮,就能弹出一个对应的对话框。其中,消息对话框只有一个提示信息和一个“确定’’按钮,输入对话框有一个供输入的文本框及“确定”和“撤销”两个按钮;确定对话框中有一个提示信息和三个按钮“是”、“否”和“撤销”;而选择对话框中有一个提示信息和两个按钮“确定,,和“取消”。
import javax.swin9.*:
import java.awt.event.*;
import java.awt.*;
Public class java3 extends JFrame. implements ButtonSelecte ActionListener
{
JButton btnMessage=new JButton("消息");
JButton btnlnput=new JButton("输入");
JButton btnConfirm=new JButton("确认");
JButton btnOption=new JButton("选择");
public java3()
{
super("java3");
btnMessage.addActionListener(this);
btnlnput.addActionListener(this);
btnConfirm.addActionListener(this);
btnOption.addActionListener(this);
getContentPane().setLayout( new FIowLavout
()):
getContentPane().add(btnMessage);
getContentPane().add(btnlnput):
getContentPane().add(btnConfirm):
getContentPane().add(btnOption);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
public static void main(String args[])
{
java3 fr=new java3();
fr.pack();
fr.setVisible(true);
}
Public void actionperformed(ActionEvent e)
{
Object[]opt={"确认","取消");
JButton instance:(JButton)e.getObject();
if(instance= =btnMessage)
JOptionPane.showMessageDialog(this,"消息对话框");
else if(instance= =btnInput、
JOptionPane.showInputDialog(this,"输入对话框");
else if(instance= =btnConfirm、
JOptionPane.showConfirmDialog(this,"确认对话框");
else
JOptionPane.showOptionDialog(this,"选择对话框","选择",JOptionPane.YES_OPTION,JOptionPane.QUESTION_MESSAGE,null,opt,opt[1]);
}
}
第10题
有数字的面板,该面板上的数字指示着文本域中的行数。
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
public class java3 extends JFrame
{
public static JTextPane textPane;
public static JScrollPane scrollPane;
JPanel panel;
public java3()
{
super("java3()");
panel=new JPanel();
panel.setLayout(new BorderLayout());
panel.setBorder(BorderFactory.createEmptyBor-
der(20,20,20,20));
textPane=new JTextPane();
textPane.setFont(new Font("monospaeed",
Font.PLAIN,12));
scrollPane=new JScrollPane(textPane);
panel.add(scrollPane);
scrollPane.setPreferredsize(new Dimension(300,
250));
setContentPane(panel);
setCloseOperation(JFrame.EXIT_ON_CLOSE);
LineNumber lineNumber=new LineNumber();
scrollPane.setRowHeaderView(lineNumber);
}
public static void main(String[]args)
{
java3 ttp=new java3();
ttp.pack();
ttp.setVisible(true);
}
}
class LineNumber extends JTextPane
{
private final static Color DEFAULT_BACK-
GROUND=Color.gray;
private final static Color DEFAULT_FORE-
GROUND=Color.black;
private final static Font DEFAUl。T—FONT=new
Font("monospaced",Font.PLAIN,12);
private final static int HEIGHT=Integer.MAX_
VALUE-1000000;
private final static int MARGIN=5;
private FontMetrics fontMetrics;
private int lineHeight;
private int currentRowWidth;
private JComponent component;
private int componentFontHeight;
private int componentFontAscent;
public LineNumber(JComponent component)
{
if(component= =null)
{
setBackground(DEFAULT_BACKGROUND);
setForegroun"DEFAULT_FOREGROUND);
setFont(DEFAULT FONT);
this.component=this;
}
else
{
setBaekground(DEFAULT_BACKGROUND);
setForeground(component.getForeground());
setFont(component.getFont());
this.component=component;
}
componentFontHeight=component.getFontMet-
rics(component.getFont()).getHeight();
componentFontAscent=component.getFontMet-
ries(component.getFont()).getAscent();
setPreferredWidth(9999);
}
public void setPreferredWidth(int row)
{
int width=fontMetrics.stringWidth(String.val-
ueOf(row));
if(currentRowWidth<;width)
{
currentRowWidth=width;
setPreferredSize(new Dfimension(2 * MARGIN
+width,HEIGHT));
}
}
public void setFont(Font font)
{
super.setFont(font);
fontMetrics=getFontMetrics(getFont());
}
public int getLineHeight()
{
if(hneHeight= =0)
return componentFontHeight;
else
return lineHeight;
}
public void setLineHeight(int lineHeight)
{
if(hneHeight>;0)
this.lineHeight=lineHeight;
}
public int getStartOffset()
{
return component.getlnsets().top+component-
FontAscent;
}
public void paintComponent(Graphics g)
{
int lineHeight=getLineHeight();
int startOffset=getStartOffset();
Rectangle drawHere=g.getClipBounds();
g.setColor(getBackground());
g.fillRect(drawHere.x,drawHere.Y,drawHere.
width,drawHere.height);
g.setColor(getForeground());
int startLineNumber=(drawHere.y/line-
Height)+1;
int endLineNUmber = startLineNumber+
(drawHere.height/lineHeight);
int start=(drawHere.Y/hneHeight)*line-
Height+startOffset;
for(int i=startLineNumber;i<;=endLineN-
umber;i++)
{
String lineNumber=String.valueOf(i);
int width=fontMetrics.stringWidth(lineNumber
);
g.drawstring(lineNumber,MARGIN+current-
RowWidth-width,start);
start+=lineHeight:
}
setPreferredWidth(endLineNumber);
}
}
为了保护您的账号安全,请在“上学吧”公众号进行验证,点击“官网服务”-“账号验证”后输入验证码“”完成验证,验证成功后方可继续查看答案!