11[簡答題]
本題的功能是監(jiān)聽對于菜單項(xiàng)和工具條的操作。窗口中有一個菜單“Color”和一個工具體,菜單“Color”中有菜單項(xiàng)“Yellow”、“Blue”、“Red”和“Exit”,每個菜單項(xiàng)都有對應(yīng)的圖形,單擊前三個顏色菜單項(xiàng),主窗口就變成對應(yīng)的顏色,單擊“Exit”則退出程序。工具條上有4個按鈕,分別為三個顏色按鈕和一個退出程序的按鈕,單擊任意一個顏色按鈕,主窗口將變成按鈕對應(yīng)的顏色,單擊退出程序按鈕,則退出程序。
import java.a(chǎn)wt.*;
import java.a(chǎn)wt.event.*;
import java.beans.*;
import javax.swin9.*;
public class java3
{
public static void main(String[]args)
{
ToolBarFrame frame=new ToolBarFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_
ON_CLOSE);
frame.show();
}
}
class ToolBarFrame extends JFrame
{
public ToolBarFrame()
{
setTitle("java3");
setSize(DEFAULT_WIDTH,DEFAUlT_
HElGHT);
Container contentPane=getContentPane();
panel=new JPanel();
contentPane.a(chǎn)dd(panel,BorderLayout.CEN-
TER);
Action blueAction=new ColorAction("Blue".
new ImageIcon("java3-blue-ball.gif"),Color.
BLUE);
Action yellowAction=new ColorAction("
Yellow",
new Imagelcon("java3-yellow-ball.gif"),Col-
or.YELLOW);
Action redAction=new ColorAction("Red".
new Imagelcon("java3-red-ball.gif"),Color.
RED);
Action exitAction=new
AbstractAction("Exit".new Imagelcon("java3-
exit.gif"))
{
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}
};
exitAction.putValue(Action.SH()RT_DESCRIP-
TIoN,"Exit");
JToolBar bar=new JToolBar();
bar.a(chǎn)dd(blueAction);
bar.a(chǎn)dd(yellowAction);
bar.a(chǎn)dd(redAction);
bar.a(chǎn)ddSeparator();
bar.a(chǎn)dd(exitAction);
contentPane.a(chǎn)ddToolBar(bar,BorderLayout.
NoRTH);
JMenu menu=new JMenu("Color"):
menu.a(chǎn)dd(yellowAction);
menu.a(chǎn)dd(blueAction);
menu.a(chǎn)dd(redAction);
menu.a(chǎn)dd(exitAction);
JMenuBar menuBar=new JMenuBar():
menuBar.a(chǎn)dd(menu);
SetJ Menu(menuBar);
}
public static final int DEFAULT_WIDTH=300;
public static final int DEFAULT_HEIGHT
=200;
private JPanel panel;
class ColorAction extends AbstractAction
{
public ColorAction(String name,Icon icon,Color
c)
{
putValue(Action.NAME,name);
putValue(Action.SMALL_ICON,icon);
putValue(Action.SHORT_DESCRIPTION,
name+"background");
putValue("Color",c);
}
public void actionPerformed(ActionEvent evt)
{
Color C=(Color)getValue("Color");
panel.setBackcolor(c);
}
}
}
12[簡答題]
本題中,通過菜單“Connect”顯示一個對話框,單擊“ok”按鈕后,所填寫的內(nèi)容就會傳回到主窗口并顯示出來。
import java.a(chǎn)wt.* ;
import java.a(chǎn)wt.event.*;
import javax.swin9.*;
public class java3 extends JFrame implements ActionL-
istener{
public java3(){
setTitle("java3");
setSize(300,300);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
JMenuBar mbar = new JMenuBar();
setJMenuBar(bar);
JMenu fileMenu=new JMenu("File");
mbar.a(chǎn)dd(fileMenu);
connectltem=new JMenuhem("Connect");
connecthem.a(chǎn)ddActionListener(this);
fileMenu.a(chǎn)dd(connecthem);
exithem=new JMenuhem("Exit");
exithem.a(chǎn)ddActionListener(this);
fileMenu.a(chǎn)dd(exithem);
}
public void actionPerformed(ActionEvent evt){
Object source=evt.getSource();
if(source= =connecthem){
Connectlnfo transfer=new ConnectInfo ("your-
name","pw");
if(dialog= =null)
dialog=new ConnectDialog(this);
if(dialog.showDialog(transfer)){
String uname=transfer.username;
String pwd=transfer.password;
Container contentPane=getContentPane();
contentPane.a(chǎn)dd(new JLabel("username="+
uname+",password="+pwd),"South");
validate();
}
}
else if(source= =exitltem)
System.exit(0);
}
public static void main(String[]args){
JFrame f=new java3();
f.show();
}
private ConnectDialog dialog=null;
private JMenuhem connecthem;
private JMenuhem exithem;
}
class Connectlnfo{
public String username;
public String password;
public Connectlnfo(String U,String p){
username=u;password=P;
}
}
class ConnectDialog extends JDialog implements Ac-
tionListener{
public ConnectDialog(){
super(parent,"Connect",true);
Container contentPane=getContentPane();
JPanel pl=new JPanel();
pl.setLayout(new GridLayout(2,2));
pl.a(chǎn)dd(newJLabel("User name:"));
pl.a(chǎn)dd(username=new JTextField(""));
pl.a(chǎn)dd(new JLabel("Password:"));
pl.a(chǎn)dd(password=new JPasswordField(""));
contentPane.a(chǎn)dd("Center",pl);
Panel p2=new Panel();
okButton=addButton(p2,"ok");
cancelButton=addButton(p2。"Cancel");
contentPane.a(chǎn)dd("South",p2);
setSize(240,120);
}
JButton addButton(Container C,String name){
JButton button=new JButton(name);
button.a(chǎn)ddActionListener(this);
C.a(chǎn)dd(button);
return button;
}
public void actionPerformed(ActionEvent evt){
object source=evt.getSource();
if(source= =okButton){
ok=true:
setVisible(false);
}
else if(source= =cancelButton)
setVisible(false);
}
public void showDialog(Connectlnfo transfer){
username.setText(transfer.username);
password.setText(transfer.password);
ok=false;
show();
if(ok){
transfer.username=username.getText();
transfer.password=new String(password.get-
Password());
}
return ok;
}
private JTextField username ;
private JPasswordField password;
private boolean ok;
private JButton okButton;
private JButton cancelButton;
}
13[簡答題]
本題是一個Applet,功能是用鼠標(biāo)畫不同顏色的圖形。頁面中有5個按鈕“畫紅色圖形”、“畫綠色圖形”、“畫藍(lán)色圖形”、“橡皮”和“清除”,單擊前三個按鈕中的一個,按住鼠標(biāo)左鍵或右鍵在面板中拖動,就能兩出對應(yīng)顏色的線條,單擊“橡皮”按鈕,按住鼠標(biāo)左鍵或右鍵在面板中拖動就能將面板中的圖形擦除掉,單擊“清除”按鈕,就能將面板中所有的圖形清除掉。
import java.a(chǎn)pplet.*;
import java.a(chǎn)wt.*;
import java.a(chǎn)wt.event.*;
public class java3 extends Applet implements ActionListener
{int x=-1,y=-1,rubberNote=0,clearNote=0;
Color C=new Color(255,0,O);
int con=3;
Button b_red,b_blue,b_green,b_clear,b_quit;
public void init()
{
addMouseMotionListener(this);
b_red=new Button("畫紅色圖形");
b_blue=new Button("畫藍(lán)色圖形");
b_green=new Button("畫綠色圖形");
b_quit=new Button("橡皮");
b_clear=new Button("清除");
add(b_red);
add(b_green);
add(b_blue);
add(b_quit);
add(b_clear);
b_red.a(chǎn)ddActionListener(this);
b_green.a(chǎn)ddActionListener(this);
b_blue.a(chǎn)ddActionListener(this);
b_quit.a(chǎn)ddActionListener(this);
b_dear.a(chǎn)ddActionListener(this);
}
public void paint()
(if(x!=-l&&y!=-l&rubberNote= =
0R&clearNote= =0)
{g.setColor(c);
g.filloval(X,Y,con,con);
}
else if(rubberNote= =1&&clearNote= =O)
{g.clearRect(x,Y,10,10);
}
else if(clearNote= =1&&rubberNote= =O)
{g.clearRect(0,0,getSize().width,getSize().
height);
}
}
public void mouseDragged(MouseEvent e)
{x=(int)e.getX();y=(int)e.getY();repaint();
}
public void mouseMoved(MouseEvent e){)
public void update(Graphics g)
{paint(g);
}
public void actionPerformed(Event e)
{if(e.getSource()= =b-red)
{rubberNote=0;clearNote=0;c=new Color
(255,0,0);
}
else if(e.getSource()= =b_green)
{rubberNore=0;clearNote=0;C=new Color(0,
255,0);
}
else if(e.getSource()= =b_blue)
{rubberNote=0;clearNote=0;C=new Color(0,
0,255);
}
if(e.getSource()= =b—quit)
{rubberNote=1;clearNote=0;
}
if(e.getSource()= =b—clear)
{clearNote=1;rubberNote=0;repaint();
}
}
}
14[簡答題]
本題的功能是在文本域面板中添加一個帶有行數(shù)的面板。窗口中有一個文本域,在文本域的左側(cè)有一個帶有數(shù)字的面板,該面板上的數(shù)字指示著文本域中的行數(shù)。
import javax.swing.*;
import javax.swing.event.*;
import java.a(chǎn)wt.*;
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.a(chǎn)dd(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",F(xiàn)ont.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 {
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);
}
}
15[簡答題]
本題的功能是展示4種不同的對話框。窗口中有4個按鈕:“消息”、“輸入”、“確定”和“選擇”,單擊任意一個按鈕,就能彈出一個對應(yīng)的對話框。其中,消息對話框只有一個提示信息和一個“確定’’按鈕,輸入對話框有一個供輸入的文本框及“確定”和“撤銷”兩個按鈕;確定對話框中有一個提示信息和三個按鈕“是”、“否”和“撤銷”;而選擇對話框中有一個提示信息和兩個按鈕“確定,,和“取消”。
import javax.swin9.*:
import java.a(chǎn)wt.event.*;
import java.a(chǎn)wt.*;
Public class java3 extends JFrame implements ButtonSelecte ActionListener
{
JButton btnMessage=new JButton("消息");
JButton btnlnput=new JButton("輸入");
JButton btnConfirm=new JButton("確認(rèn)");
JButton btnOption=new JButton("選擇");
public java3()
{
super("java3");
btnMessage.a(chǎn)ddActionListener(this);
btnlnput.a(chǎn)ddActionListener(this);
btnConfirm.a(chǎn)ddActionListener(this);
btnOption.a(chǎn)ddActionListener(this);
getContentPane().setLayout( new FIowLavout
()):
getContentPane().a(chǎn)dd(btnMessage);
getContentPane().a(chǎn)dd(btnlnput):
getContentPane().a(chǎn)dd(btnConfirm):
getContentPane().a(chǎn)dd(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={"確認(rèn)","取消");
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,"確認(rèn)對話框");
else
JOptionPane.showOptionDialog(this,"選擇對話框","選擇",JOptionPane.YES_OPTION,JOptionPane.QUESTION_MESSAGE,null,opt,opt[1]);
}
}
本題的功能是監(jiān)聽對于菜單項(xiàng)和工具條的操作。窗口中有一個菜單“Color”和一個工具體,菜單“Color”中有菜單項(xiàng)“Yellow”、“Blue”、“Red”和“Exit”,每個菜單項(xiàng)都有對應(yīng)的圖形,單擊前三個顏色菜單項(xiàng),主窗口就變成對應(yīng)的顏色,單擊“Exit”則退出程序。工具條上有4個按鈕,分別為三個顏色按鈕和一個退出程序的按鈕,單擊任意一個顏色按鈕,主窗口將變成按鈕對應(yīng)的顏色,單擊退出程序按鈕,則退出程序。
import java.a(chǎn)wt.*;
import java.a(chǎn)wt.event.*;
import java.beans.*;
import javax.swin9.*;
public class java3
{
public static void main(String[]args)
{
ToolBarFrame frame=new ToolBarFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_
ON_CLOSE);
frame.show();
}
}
class ToolBarFrame extends JFrame
{
public ToolBarFrame()
{
setTitle("java3");
setSize(DEFAULT_WIDTH,DEFAUlT_
HElGHT);
Container contentPane=getContentPane();
panel=new JPanel();
contentPane.a(chǎn)dd(panel,BorderLayout.CEN-
TER);
Action blueAction=new ColorAction("Blue".
new ImageIcon("java3-blue-ball.gif"),Color.
BLUE);
Action yellowAction=new ColorAction("
Yellow",
new Imagelcon("java3-yellow-ball.gif"),Col-
or.YELLOW);
Action redAction=new ColorAction("Red".
new Imagelcon("java3-red-ball.gif"),Color.
RED);
Action exitAction=new
AbstractAction("Exit".new Imagelcon("java3-
exit.gif"))
{
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}
};
exitAction.putValue(Action.SH()RT_DESCRIP-
TIoN,"Exit");
JToolBar bar=new JToolBar();
bar.a(chǎn)dd(blueAction);
bar.a(chǎn)dd(yellowAction);
bar.a(chǎn)dd(redAction);
bar.a(chǎn)ddSeparator();
bar.a(chǎn)dd(exitAction);
contentPane.a(chǎn)ddToolBar(bar,BorderLayout.
NoRTH);
JMenu menu=new JMenu("Color"):
menu.a(chǎn)dd(yellowAction);
menu.a(chǎn)dd(blueAction);
menu.a(chǎn)dd(redAction);
menu.a(chǎn)dd(exitAction);
JMenuBar menuBar=new JMenuBar():
menuBar.a(chǎn)dd(menu);
SetJ Menu(menuBar);
}
public static final int DEFAULT_WIDTH=300;
public static final int DEFAULT_HEIGHT
=200;
private JPanel panel;
class ColorAction extends AbstractAction
{
public ColorAction(String name,Icon icon,Color
c)
{
putValue(Action.NAME,name);
putValue(Action.SMALL_ICON,icon);
putValue(Action.SHORT_DESCRIPTION,
name+"background");
putValue("Color",c);
}
public void actionPerformed(ActionEvent evt)
{
Color C=(Color)getValue("Color");
panel.setBackcolor(c);
}
}
}
12[簡答題]
本題中,通過菜單“Connect”顯示一個對話框,單擊“ok”按鈕后,所填寫的內(nèi)容就會傳回到主窗口并顯示出來。
import java.a(chǎn)wt.* ;
import java.a(chǎn)wt.event.*;
import javax.swin9.*;
public class java3 extends JFrame implements ActionL-
istener{
public java3(){
setTitle("java3");
setSize(300,300);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
JMenuBar mbar = new JMenuBar();
setJMenuBar(bar);
JMenu fileMenu=new JMenu("File");
mbar.a(chǎn)dd(fileMenu);
connectltem=new JMenuhem("Connect");
connecthem.a(chǎn)ddActionListener(this);
fileMenu.a(chǎn)dd(connecthem);
exithem=new JMenuhem("Exit");
exithem.a(chǎn)ddActionListener(this);
fileMenu.a(chǎn)dd(exithem);
}
public void actionPerformed(ActionEvent evt){
Object source=evt.getSource();
if(source= =connecthem){
Connectlnfo transfer=new ConnectInfo ("your-
name","pw");
if(dialog= =null)
dialog=new ConnectDialog(this);
if(dialog.showDialog(transfer)){
String uname=transfer.username;
String pwd=transfer.password;
Container contentPane=getContentPane();
contentPane.a(chǎn)dd(new JLabel("username="+
uname+",password="+pwd),"South");
validate();
}
}
else if(source= =exitltem)
System.exit(0);
}
public static void main(String[]args){
JFrame f=new java3();
f.show();
}
private ConnectDialog dialog=null;
private JMenuhem connecthem;
private JMenuhem exithem;
}
class Connectlnfo{
public String username;
public String password;
public Connectlnfo(String U,String p){
username=u;password=P;
}
}
class ConnectDialog extends JDialog implements Ac-
tionListener{
public ConnectDialog(){
super(parent,"Connect",true);
Container contentPane=getContentPane();
JPanel pl=new JPanel();
pl.setLayout(new GridLayout(2,2));
pl.a(chǎn)dd(newJLabel("User name:"));
pl.a(chǎn)dd(username=new JTextField(""));
pl.a(chǎn)dd(new JLabel("Password:"));
pl.a(chǎn)dd(password=new JPasswordField(""));
contentPane.a(chǎn)dd("Center",pl);
Panel p2=new Panel();
okButton=addButton(p2,"ok");
cancelButton=addButton(p2。"Cancel");
contentPane.a(chǎn)dd("South",p2);
setSize(240,120);
}
JButton addButton(Container C,String name){
JButton button=new JButton(name);
button.a(chǎn)ddActionListener(this);
C.a(chǎn)dd(button);
return button;
}
public void actionPerformed(ActionEvent evt){
object source=evt.getSource();
if(source= =okButton){
ok=true:
setVisible(false);
}
else if(source= =cancelButton)
setVisible(false);
}
public void showDialog(Connectlnfo transfer){
username.setText(transfer.username);
password.setText(transfer.password);
ok=false;
show();
if(ok){
transfer.username=username.getText();
transfer.password=new String(password.get-
Password());
}
return ok;
}
private JTextField username ;
private JPasswordField password;
private boolean ok;
private JButton okButton;
private JButton cancelButton;
}
13[簡答題]
本題是一個Applet,功能是用鼠標(biāo)畫不同顏色的圖形。頁面中有5個按鈕“畫紅色圖形”、“畫綠色圖形”、“畫藍(lán)色圖形”、“橡皮”和“清除”,單擊前三個按鈕中的一個,按住鼠標(biāo)左鍵或右鍵在面板中拖動,就能兩出對應(yīng)顏色的線條,單擊“橡皮”按鈕,按住鼠標(biāo)左鍵或右鍵在面板中拖動就能將面板中的圖形擦除掉,單擊“清除”按鈕,就能將面板中所有的圖形清除掉。
import java.a(chǎn)pplet.*;
import java.a(chǎn)wt.*;
import java.a(chǎn)wt.event.*;
public class java3 extends Applet implements ActionListener
{int x=-1,y=-1,rubberNote=0,clearNote=0;
Color C=new Color(255,0,O);
int con=3;
Button b_red,b_blue,b_green,b_clear,b_quit;
public void init()
{
addMouseMotionListener(this);
b_red=new Button("畫紅色圖形");
b_blue=new Button("畫藍(lán)色圖形");
b_green=new Button("畫綠色圖形");
b_quit=new Button("橡皮");
b_clear=new Button("清除");
add(b_red);
add(b_green);
add(b_blue);
add(b_quit);
add(b_clear);
b_red.a(chǎn)ddActionListener(this);
b_green.a(chǎn)ddActionListener(this);
b_blue.a(chǎn)ddActionListener(this);
b_quit.a(chǎn)ddActionListener(this);
b_dear.a(chǎn)ddActionListener(this);
}
public void paint()
(if(x!=-l&&y!=-l&rubberNote= =
0R&clearNote= =0)
{g.setColor(c);
g.filloval(X,Y,con,con);
}
else if(rubberNote= =1&&clearNote= =O)
{g.clearRect(x,Y,10,10);
}
else if(clearNote= =1&&rubberNote= =O)
{g.clearRect(0,0,getSize().width,getSize().
height);
}
}
public void mouseDragged(MouseEvent e)
{x=(int)e.getX();y=(int)e.getY();repaint();
}
public void mouseMoved(MouseEvent e){)
public void update(Graphics g)
{paint(g);
}
public void actionPerformed(Event e)
{if(e.getSource()= =b-red)
{rubberNote=0;clearNote=0;c=new Color
(255,0,0);
}
else if(e.getSource()= =b_green)
{rubberNore=0;clearNote=0;C=new Color(0,
255,0);
}
else if(e.getSource()= =b_blue)
{rubberNote=0;clearNote=0;C=new Color(0,
0,255);
}
if(e.getSource()= =b—quit)
{rubberNote=1;clearNote=0;
}
if(e.getSource()= =b—clear)
{clearNote=1;rubberNote=0;repaint();
}
}
}
14[簡答題]
本題的功能是在文本域面板中添加一個帶有行數(shù)的面板。窗口中有一個文本域,在文本域的左側(cè)有一個帶有數(shù)字的面板,該面板上的數(shù)字指示著文本域中的行數(shù)。
import javax.swing.*;
import javax.swing.event.*;
import java.a(chǎn)wt.*;
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.a(chǎn)dd(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",F(xiàn)ont.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
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);
}
}
15[簡答題]
本題的功能是展示4種不同的對話框。窗口中有4個按鈕:“消息”、“輸入”、“確定”和“選擇”,單擊任意一個按鈕,就能彈出一個對應(yīng)的對話框。其中,消息對話框只有一個提示信息和一個“確定’’按鈕,輸入對話框有一個供輸入的文本框及“確定”和“撤銷”兩個按鈕;確定對話框中有一個提示信息和三個按鈕“是”、“否”和“撤銷”;而選擇對話框中有一個提示信息和兩個按鈕“確定,,和“取消”。
import javax.swin9.*:
import java.a(chǎn)wt.event.*;
import java.a(chǎn)wt.*;
Public class java3 extends JFrame implements ButtonSelecte ActionListener
{
JButton btnMessage=new JButton("消息");
JButton btnlnput=new JButton("輸入");
JButton btnConfirm=new JButton("確認(rèn)");
JButton btnOption=new JButton("選擇");
public java3()
{
super("java3");
btnMessage.a(chǎn)ddActionListener(this);
btnlnput.a(chǎn)ddActionListener(this);
btnConfirm.a(chǎn)ddActionListener(this);
btnOption.a(chǎn)ddActionListener(this);
getContentPane().setLayout( new FIowLavout
()):
getContentPane().a(chǎn)dd(btnMessage);
getContentPane().a(chǎn)dd(btnlnput):
getContentPane().a(chǎn)dd(btnConfirm):
getContentPane().a(chǎn)dd(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={"確認(rèn)","取消");
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,"確認(rèn)對話框");
else
JOptionPane.showOptionDialog(this,"選擇對話框","選擇",JOptionPane.YES_OPTION,JOptionPane.QUESTION_MESSAGE,null,opt,opt[1]);
}
}