版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
Java語言程序設(shè)計
第九章課后習(xí)題答案
1.編寫一個程序,該程序繪制一個5X9的網(wǎng)絡(luò),使用drawLine方法。
//NetWork類
importjava.awt.Graphics;
importjavax.swing.JFrame;
publicclassNetWorkextendsJFrame{
publicNetWorko{
//設(shè)置窗體大小
this.setSize<130,130>;
//設(shè)置窗體大小不可改變
this.setResizable<false>;
//設(shè)置默認(rèn)關(guān)閉方式,關(guān)閉窗體的同時結(jié)束程序
this.setDefaultCloseOperation<JFrame.EXIT_ON_CLOSE>;
//將窗體顯示出來
this.setVisible<true>;
)
〃橫縱格之間都間隔10像素,起點在(20,40
publicvoidpaint<Graphicsg>{
〃繪制橫向線
for<inti=0;i<=5;i++>{
g.drawLine<20,40+i*10,110,40+i*10>;
}
〃繪制縱向線
for<inti=0;i<=9;i++>{
g.drawLine<20+i*10,40,20+i*10,90>;
)
)
}
//test9_l類
publicclasstest9_l{
publicstaticvoidmain<String[]args>{
newNetWorko;
)
)
運行結(jié)果:
2.編寫一個程序,該程序以不同的顏色隨機(jī)產(chǎn)生三角形,每一個三角形比不同的顏色進(jìn)行填充。
//Triangle類
importjava.awt.Color;
importjava.awt.Graphics;
importjava.util.Random;
importjavax.swing.JFrame;
publicclassTriangleextendsJFrame{
Randomrnd=newRandomo;
//這里定義4個三角形
int[](]x-newint[4][3];
int[][]y=newint[4][3];
int(][)color=newint[4][3];
publicTriangleo{
for<inti=0;i<4;i++>{
for<intj=0;j<3;j++>{
color[i][j]=rnd.nextlnt<255>;
x(ij[j]=rnd.nextInt<i*100-i-100>;
y[i][j]=rnd.next工nt<i*100+100>+50;//加50像素是為了避免頂?shù)酱绑w上沿
}
}
〃窗體標(biāo)題
this隨機(jī)三角形”>;
〃窗體大小
this.setSize<500,500>;
//窗體大小不可變
this.setResizable<false>;
//關(guān)閉窗體的同時結(jié)束程序
this.setDefaultCloseOperation<JFrame.EXIT_ON_CLOSE>;
//顯示窗體
this.setVisible<true>;
}
publicvoidpaint<Graphicsg>{
for<inti=0;i<4;i++>{
g.setColor<newColor<color[i][0]zcolor[i][1]/color[i][2]?;
g.fillPolygon<x[i],y[i],3>;
)
}
}
//test9_2
publicclasstest9_2{
publicstaticvoidmain<String[]args>(
newTriangleo;
)
}
運行結(jié)果:
3.編寫一個Applet,該程序請求用戶輸入圓的半徑,然后顯示該圓的直徑、周長和面積。
//test9_3
importjavax.swing.*;
importjava.awt.*;
importjava.awt.event.ActionEvent;
importjava,awt.event.ActionListener;
publicclasstest9_3extendsJApplet(
2/23
//聲明5個標(biāo)簽
privateJLabeljLabe11;
privateJLabeljLabe12;
privateJLabeljLabel3;
privateJLabeljLabe14;
privateJLabeljLabel5;
//I個單行文本
privateJTextFieldtextOfRadius;
//2個按鈕
privateJButtonjButtonl;
privateJButtonjButton2;
//初始化
publinvnidinito{
try(
java,awt.EventQueue.invokeAndWait<newRunnableo
publicvoidrun<>{
initCompDnents<>;
)
}>;
}catch<Exceptionex>{
ex.printStackTraceo;
)
}
privatevoidinitComponents<>{
//聲明8個組件
jLabel1=newJLabeJX”輸入圓的半徑:",SwingConstants.CENTER>;
jLabel2=newJLabelv”圓的周長:",Swingconstants.CENT3R>;
jLabel3=newJLabeK*'",Swingconstants.CENTER>;
jLabel4=newJLabel〈”圓的面積:“,Swingconstants.CENTZR>;
jLabel5=newJLabeKnu,SwingConstants.CENTER>;
textOfRadius=newJTextFieldc"半徑”;
jButtonl=newJButtone”計算”>;
jButton2=newJButtonc”退出”>;
//按鈕添加監(jiān)聽器
jButtonl.addActionListener<newActionListenerO{
publicvoidactionPerformed<ActionEventevt>{
jButtonlActionPerformed<evt>;
)
//按鈕添加監(jiān)聽器
jButton2.addActionListener<newActionListenerO{
publicvoidactionPerformed<ActionEventevt>{
jButton2ActionPerformed<evt>;
)
3/23
)>;
〃聲明定義內(nèi)容面板,并且改置其布局格式為:4行2列格子
Containerc=getContentPane<>;
c.setLayout<newGridLayout<4,2>>;
〃將8個組件加入到內(nèi)容面板
c.add<jLabell>;
c.add<textOfRadius>;
c.add<jLabe12>;
c.add<jLabe13>;
c.add<jLabel4>;
c.add<jLabe!5>;
c.add<jButtonl>;
c.add<jButton2>;
)
//求周長方法
privateStringRound<doublea>{
doubleperimeter=a*2*3.14;
Strings=newString<String.valueOf<perimeter>>;
returns;
)
//求面積方法
privateStringArea<doublea>{
doublearea=a*a*3.14;
Strings=newStrinq<Strinq.valueOf<area>>;
returns;
)
//點擊"計算”按鈕jButton!.觸發(fā)的方法
privatevoidjButtonlActionPerformed<ActionEventevt>{
//捕獲單文本輸入非數(shù)字的異常
try(
Strings=textOfRadius.getTextO;//獲得單文本字符
doublea=Double.valueOf<s>.floatvalue。;//字符轉(zhuǎn)化為雙精度
jLabe13.setText<Round<a>>;〃標(biāo)簽內(nèi)容為周長
jLabel5.seEText〈Area〈a>>;〃標(biāo)筆內(nèi)容為面積
}catch<NumberFormatExceptionr>{
//單文本為非數(shù)字彈出提示“輸入錯誤”框
JOptionPane.showMessageDialog<this,”請輸入數(shù)字類型","輸入錯誤
“,JOptionPane.WARNING_MESSAGE>;
textOfRadius.setText<H,f>;
)
)
//點擊"退出"按鈕jButton2觸發(fā)的方法
publicvoidjButton2ActionFerformed<ActionEventevt>{
System.exit<0>;
4/23
)
)
運行結(jié)果:
編譯text9_3.java產(chǎn)生字節(jié)碼文件test9_3.class,接下來需要編寫一個
HTML文件text9_3.html來嵌入text9_3.class,代碼如下:
<html>
<appletcode=z,test9_3.class”)
</applet>
</html>
將test9_3.html文件和test9_3.class文件放在同一個目錄下,在瀏覽器中打開
這個test9_3.html文件,實現(xiàn)的效果如下:
4.編寫一個Applet,向其輸入五個數(shù),然后以條形圖(bargraph的形式來表示這些數(shù)。
5.編寫?個繪制圓形的程序,當(dāng)鼠標(biāo)在繪制區(qū)域中單擊時,該正方形的左上角頂點應(yīng)準(zhǔn)確的跟
隨鼠標(biāo)光標(biāo)挪移,重繪該圓形。
//MyJFrame類
importjava.awt.Graphics;
importjava.awt.event.MouseEvent;
importjava.awt.event.MouseListener;
importjavax.swing.JFrame;
publicclassMyJFrameextendsJFrameimplementsMouseListener{
intx=50;
inty=50;
intradius=50;
publicMyJFrame<>{
this.setTit1e<"繪制圓形”>;
this.setSize<200,200>;
this.setResizable<false>;
this.setDefaultCloseOperation<JFrame.EXIT_ON_CLOSE>;
this.addMouseListener<this>;
this.setVisible<true>;
)
publicvoidpaint<Graphicsg>{
g.drawOvaKx,y,radius,radius〉;
}
publicvoidmouseClicked<MouseEvente>{
//TODOAuto-generatedmethodstub
this.x=e.getX<>;
this.y=e.getY<>;
this.repainto;
System.out.println<"x:"+e.getxo+'*\ny:"+e.getY<?;
)
publicvoidmouseEntered<MouseEvente>{
//TODOAuto-generatedmethodstub
5/23
)
publicvoidniout>eExited<MouseEveiit.e>{
//TODOAuto-generatedmethodstub
I
publicvoidmousePressed<McuseEvente>{
//TODOAuto-generatedmethodstub
}
publicvoidmouseReleased<MouseEvente>{
//TODOAuto-generatedmethodstub
)
}
//test9_5
publinnlass?{
publicstaticvoidmain<String[]args>{
newMyJFrameO;
}
}
運行結(jié)果:
6.編寫一個”猜數(shù)“程序:該程序隨機(jī)在1到100的范圍內(nèi)選擇一個供用戶猜測的整數(shù),然后改
程序顯示提示信息,要求用戶輸入一個1到100之間的整數(shù),根據(jù)輸入偏大、偏小、正確,程序
將顯示不同的圖標(biāo)。
//GuessNumber類
importjava.awt.GridLayout;
importjava.awt.event.ActionEvent;
importjava,awt.event.ActionListener;
importjava.util.Random;
importjavax.swing.JButtor.;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JOptionPane;
importjavax.swing.JTextField;
importjavax.swing.Swingconstants;
publicclassGuessNumberextendsJFrameimplementsActionListener{
intnumberOfRandom;//游戲程序隨機(jī)數(shù)
intnumberOfUser;//玩家輸入整數(shù)
JLabellabell;//標(biāo)簽1:”輸入一個1到100之間的整數(shù)”
JLabellabe12;//標(biāo)簽2:“提示”
JLabellabel3;//標(biāo)簽3:提示的內(nèi)容(偏大、偏小、正確
JButtonbuttonOfSure;//確定按鈕
JButtonbuttonOfExit;//退出按鈕
JTextFieldjTextField;//猜數(shù)輸入單行文本框
publicvoidinito{
//窗體的定義
this.setTitlev”這是一個猜數(shù)游戲”>;
6/23
this.setSize<400,120>;
this.setResizable<false>;
this.setLayout<newGridLayout<3,2?;
this.setDefaultCloseOperation<JFrame.EXIT_ON_CLOSE>;
//隨機(jī)數(shù)
Randomrandom=newRandomo;
numberOfRandom=random.nextlnt<l00>;
//各組件的定義
labell=newJLabelv”請輸入一個1到100之間的整數(shù):">;
label2=newJLabe1V"提示:",Swingconstants.CENTER>;
labe13=newJLabelv"猜猜看
buttonOfSure=newJButtonV”確定
this.getRootPaneO.setDefaultButton<buttonOfSure>;//默認(rèn)用戶按下回車鍵
觸發(fā)“確定“按鈕
buttonOfSure.addActionListener<this>;
buttonOfExit=newJButton<11Hi">;
buttonOfExit.addActionListener<this>;
jTextField=newJTextFieldv"輸入”>;
//窗體加入各組件
this.add<labell>;
this.add<jTextField>;
this.add<label2>;
this.add<label3>;
this.add<buttonOfSure>;
this.add<buttonOfExit>;
//顯示窗體
this.setVisible<true>;
System.out.println<numberOfRandom>;//偷偷看看隨機(jī)數(shù),哈哈
}
//事件處理
publicvoideventHandle<>
//捕獲非整型輸入異常
try(
numberOfUser=Integer.parselnt<jTextField.getTexto;//獲得用戶輸入數(shù)
if<numberOfUser<1||numberOfUser>100>{
JOptionPane.showMessageDialog<this,”請輸入1到100之間的整
數(shù)
}else{
if<numberOfUser>numberOfRandom>{
label3.setTextc”偏大
jTextField.requestFocuso;//清空文本框并使舊新狹行焦點
}
if<numberOfUser<numberOfRandom>{
label3.setTextx“偏小”>;
7/23
jTextField.requestFocusO;
}
if<numberOfUser==numberOfRandom>{
labe13.setText<"恭喜你,答對了。u>;
jTextField.requestFocuso;
}
)
}catch<NumberFormatExceptione>{
JOptionPane.showMessageDialog<this,”請輸入整數(shù)","輸入錯誤”,
JOptionPane.WARNING_MESSAGE>;
jTextField.requestFocuso;
)
)
//按鈕觸發(fā)選擇
publicvoidactionPerformed<ActionEvente>{
if<e.getActionCommando=="確定"〉{
this.eventHandleo;
)
if<e.getActionCommando=="退出"》{
System.exit<0>;
}
)
publicstaticvoidmain<String(]args>{
nAwGupssNumbprO.init<>;
}
}
運行結(jié)果:
7.練習(xí)使用JscrollPaneo使用BorderLayout將JFrame布局分為擺布兩塊;左邊又使用
Gridlayout,包含三個按鈕,右邊在兒abel里顯示一幅圖畫,按鈕控制JLabel是否顯示滾動條。
//test9_7
importorderLayout;
importjava.awt.Button;
importjava.awt.Container;
importjava.awt.GridLayout;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjavax.swing.Imageicon;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JOptionPane;
importjavax.swing.JPanel;
importjavax.swing.JScrollPane;
importjavax.swing.ScrollPaneConstants;
publicclasstest97,_extendsJFrameimplementsActionListener{
8/23
privateJPaneljPanel;//左邊panel框
privateButtonbuttonl;
privateButtonbutton2;
privateButtonbutton3;
privateJScrollPanejscro工工Pane;//右邊滾動框
privateJLabellabel;//滾動框中的標(biāo)簽
publicImagelconpicture=null;//標(biāo)簽中的圖標(biāo)
publicbooleanwhetherCroll=true;//用于切換滾動條的顯示
publicvoidinito{
JFramejFrame=newJFrame<”練習(xí)使用Jscrol1Pane”>;
Containerpane=jFrame.getContentPaneo;
this.setDefaultCloseOperation<<JFrame.EXIT_ON_CLOSE>>;
//定義左邊鑲板JPanel框和三個按鈕
jPanel=newJPanel<newGridLayout<3,1,0,30>>;
buttonl=newButton<"滾動
buttonl.addActionListener<this>;
button2=newButtonc”試試”>;
button2.addActionListener<this>;
button3=new8a1。水"退出”>;
button3.addActionListener<this>;
jPanel.add<buttonl>;
jPanel.add<button2>;
jPanel.add<button3>;
//讀取圖片作為圖標(biāo)
picture=newImageIcon<nD:/image/fuck.jpg/n>;
//將圖標(biāo)賦給標(biāo)簽工abel
label=newJLabel<picture>;
//定義滾動框,總是顯示滾動條
jscrollPane=newJScrollPane<label,
ScrollPaneCanstants.VERTICAL_SCROLLBAR_ALWAYS,
ScrollPaneCc-nstants.HORIZONTAL_SCROLLBAR_ALWAYS>;
//將擺布兩邊的中間容器拋卻JFrame中
pane.add<jPanel,BorderLayout.WEST>;
pane.add<jscrollPane,BorderLayout.EAST>;
〃排版顯示
jFrame.packo;
jFrame.setVisible<true>;
)
@Override
publicvoidactionPerformed<ActionEvente>{
//TODOAuto-generatedmethodstub
if<e.getActionCommandO=="滾動"》{
if<whetherCroll>{
jscrollPane.setVerticalScrollBarPolicy<JScrollPane.VERTICAL_SCROLLBAR
9/23
_NEVER>;//垂直不顯示
jscrollPane.setHorizontalScrollBarPolicy<JScrollPane.HORIZONTAL_SCROL
LBAR_NEVER>;//水平不顯示
whetherCroll=false;
}else{
jscrollPane.setVerticalScrollBarPolicy<JScrollPane.VERTICAL_SCROLLBAR
_ALWAYS>;//垂直顯示
jscrollPane.setHorizontalScrollBarPolicy<JScrollPane.HORIZONTAL_SCROL
LBAR_ALWAYS>;//水平顯示
whetherCroll=true;
}
}
if<e.getActionCommandO—"試試”〉{
JOptionPane.showMessageDialog<this,
"oh,,,Fuckyourteacher?yeal......good!*'>;
)
if<e.getActionCommandO=="退出"》{
System.exit<0>;
}
)
publicstaticvoidmain<String(]args>{
newtest9_7<>.inito;
}
)
運行結(jié)果:
顯示滾動條
點擊〃滾動〃按鈕后,沒顯示滾動條
8.練習(xí)使用JListo建立兩個JList,雙擊其中任何一個中的某一項,此項就會跑到此外一個兒ist
中。
//test9_8
importjava.awt.Container;
importjava.awt.GridLayout;
importjava.awt.event.MouseEvent;
importjava.awt.event.MouseListener;
importjavax.swing.DefaultListModel;
importjavax.swing.JFrame;
importjavax.swing.JList;
publicclasstest98.extendsJFrameimplementsMouseListener{
publicJFramejFrame;
publicJListjListl;
publicJListjList2;
publicDefaultListModellistModell;
publicDefaultListModellistModel2;
publicvoidinito{
10/23
JFramejFrame=newJFrame〈”練習(xí)使用List”;
Containerpane-jFrame.getContentPaneo;
pane.setLayout<newGridLayout<l,2>>;
jFrame.setDefaultCloseOperation<JFrame.EXIT_ON_CLOSE
listModell=newDefaultListModelo;
listModell.addElement<nFirst">;
listModell.addElement<,,Second">;
listModell.addElement<,,Third,*>;
listModel2=newDefaultListModelo;
listModel2.addElement<,,one,*>;
listModel2.addElement<*,twou>;
listModel2.addElement<"three">;
jListl=newJList<listMode11>;
jList2=newJList<listModel2>;
jListl.addMouseListener<this>;
jList2.addMouseListener<this>;
pane.add<jListl>;
pane.add<jList2>;
jFrame.packo;
jFrame.setVisible<true>;
}
@Override
publicvoidmouseClicked<McuseEvente>{
//TODOAuto-generatedmethodstub
if<e.getSourceo==jListl>{
intindex=jListl.locationToIndex<e.getPoint<>>;
Objecto=jLiszl.getModelo.getElementAt<index>;
listMode!2.addElement<o>;
)
if<e.getSource<>==jList2>\
intindex=jList2.locationToIndex<e.getPoint<>>;
Objecto=jLis二2.getModelo.getElementAt<index>;
listModell.addElement<o>;
)
)
@Override
publicvoidmousePressed<McuseEvente>{
//TODOAuto-generatedmethodstub
}
0Override
publicvoidmouseReleased<McuseEvente>{
//TODOAuto-generatedmethodstub
}
@Override
11/23
publicvoidmouseEntered<McuseEvente>{
//TODOAuto-generatedmethodstub
@Override
publicvoidmouseExited<MouseEvente>{
//TODOAuto-generatedmethodstub
)
publicstaticvoidmain<String(]args>{
newtest9_8<>.inito;
)
)
運行結(jié)果:
9.練習(xí)使用JComboBoxn包括一個JLable、一個JComboBox,可以通過輸入或者選擇JComboBox
中的某一項來控制兒able中文字的大小。
//test9_9
importjava.awt.Container;
importjava.awt.Font;
importjava.awt.GridLayout;
importjava.awt.event.ItemEvent;
importjava.awt.event.ItenListener;
importjavax.swing.JComboBox;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JOptionPane;
publicclasstest99_.extendsJFrameimplementsItemListener{
publicJFramejFrame;
privateJLabeljLabel;
privateJComboBoxjComboBox;
n
privateString[]fontsize=(8"z“14“,“20“,”28”,"32","40”);
publicvoidinito{
jFrame=newJFramev”練習(xí)ComboBox"〉;
Containercontainer=jFrame.getContentPaneo;
container.setLayout<newGridLayout<2,1>>;
jFrame.setDefaultCloseOperation<JFrame.EXIT_ON_CLOSE>;
jLabel=newJLabelc”你好嗎?n>;
jLabel.setFont<newFonte"”,0,10?;//設(shè)置字體大小
jComboBox=newJComboBox<fontSize>;
jComboBox.setEditable<true>;//設(shè)置復(fù)選框可以輸入
jComboBox.addItemListener<this>;//添力口工temListener監(jiān)聽
//添加兩個組件
container.add<jLabel>;
container.add<jComboBox>;
jFrame.packo;//排版
jFrame.setVisible<true>;//顯示
12/23
)
publicvoidiLeniStaLeChauyIteinEvenLe>{
〃使用這個判斷的目的的是為了避免觸發(fā)事件總是執(zhí)行兩次的問題
if<e.getStateChangeO==ItemEvent.SELECTED>{
try{
Strings=jComboBox.getSelectedltemo.toStringo;
intintFontSize=Integer.parselnt<s>;
jLabel.setFont<newFont<"",0,intFontSize?;
//System.out.println<intFontSize>;
}catch<NumberFormatExceptionr>{
JOptionPane.showMessageDialog<this,”請照入整數(shù)”>;
}
)
}
publicstaticvoidmain<String[]args>{
newtest9_9<>.inito;
)
}
運行結(jié)果:
10.練習(xí)使用JTable.包括姓名、學(xué)號、語文成績、數(shù)學(xué)成績、總分五項,單擊總分會自動將
語文數(shù)學(xué)成績相加。
importjava.awt.event.*;
importjavax.swing.*;
importjavax.swing.table.*;
publicclassTest9_l0extendsJFrameimplementsMouseListener{
privateJTablejTable;
privateObject[][]cellData={{"姓名",newStringv1?林楚金”),
{“學(xué)號”,newString<n202205">},{”語文成績”,new
String<"86">},
{"數(shù)學(xué)成績",newString<u96,,>},{“總分”,newStringc”點擊出
總分”}};
privateString[]columnNames={"coll”,ncol2;
publicTest9_l0<>{
DefaultTableModelmodel=newDefaultTableModeKcellData,
columnNames>{
publicbooleanisCellEditable<introw,intcolumn>{
returnfalse;
}
};
jTable=newJTable<model>;
jTable.addMouseListener<this>;
this.add<jTable>;
this.setTitle〈”表格演示
this.setSize<300,350>;
13/23
this.setLocation<200,200>;
this.setDefaultCloseOperation<JFrame.EXIT_ON_CLOSE>;
this.setvisible<true>;
)
publicvoidmouseClicked<McuseEvente>{
DefaultTableModeltableModel=<DefaultTableMcdel>
jTable.getModelo;
introw=jTable.getSeleciedRowO;
intcolumn=jTable.getSelectedColumno;
if<row==4&&column==l>{
Stringyuwen=<String>tableModel.getValueAt<2,column〉;//
語文成績
Stringshuxue=<String>tableModel.getValueAt<3,column>;//
數(shù)學(xué)成績
Integercount=
Integer.parselnt<yuwen>+Integer.parselnt<shuxue>;
tableModel.setValueAt<count.toStringo,row,column>;
}
}
publicvoidmouseEntered<McuseEvente>{
)
publicvoidmouseExited<MouseEvente>{
}
publicvoidmousePressed<McuseEvente>{
)
publicvoidmouseReleased<MouseEvente>{
}
publicstaticvoidmain<String[]args>{
newTest9_10<>;
)
運行結(jié)果:
11.練習(xí)使用對話框。包括一個兒able和兩個JButton,按任何一個JButton都會產(chǎn)生一個對話
框,按確定后將輸入內(nèi)容在兒able中顯示出來。
//test9_ll
importjava.awt.Container;
importjava.awt.Frame;
importjava.awt.GridLayout;
importjava,awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JOptionPane;
14/23
publicclasstest91l_.extendsFrameimplementsActionListener{
JFramejFrame;
JButtonjButtonl;
JButtonjButton2;
JLabeljLabel;
publicvoidinit<>{
JFramejFrame=newJFrame<”練習(xí)使用對話框“>;
Containerpane=jFrame.getContentPaneO;
pane.setLayout<newGridLayout<3,1>>;
jButtonl=newJButton<"^^ll">;
jButtonl.addActionListener<this>;
jButton2=newJButton<H^^2">;
jButton2.addActionListener<this>;
jLabel=newJLabelo;
jFrame.setDefaultCloseOperation<JFrame.EXIT_ON_CLOSE>;
pane.add<jButtonl>;
pane.add<jButton2>;
pane.add<jLabel>;
jFrame.packo;
jFrame.setvisible<true>;
)
@Override
publicvoidactionPerformed<ActionEvente>{
//TODOAuto-generatedmethodstub
if<e.getActionConunando=="按鈕1"IIe.getActionComir.ando=="按鈕2”>
(
Strings=JOptionPane.showInputDialog<this,,"請輸入”>;
jLabel.setText<s>;
}
)
publicstaticvoidmain<String[]args>{
newtest9_ll<>.inito;
)
}
運行結(jié)果:
<下面的題目有空再做》
12.練習(xí)使用JMenu、JFileChooser%JColorChooser(>通過菜單可以打開文件選擇對話框,來打
開某一指定文本文件,通過菜單可打開顏色選擇對話框控制顯示文本的顏色。
importjavax.swing.*;
importjava.awt.*;
importjava.awt.event.*;
importjava,io.BufferedReader;
importjava.io.FileReader;
importjava.io.lOExceptior.;
15/23
publicclassTest9_l2extendsJFrameimplementsActionListener{
//文本域
privateJTextAreajta=null;
〃菜單欄
privateJMenuBarjmb=null;
〃菜單欄中的一個菜單項
privateJMenujml=null;
//菜單項中的下拉列表
privateJMenuItemjmil=null;
privateJMenuItemjmi2=null;
〃顏色對象選擇
privateJFileChooserjfc=null;
publdeiTftst9_1?.<>{
jta=newJTextAreao;
this.jmb=newJMenuBarO;
this.jml=new選擇”>;
//設(shè)置助記符
this.jml.setMnemonic<*F'>;
this.jmil=newJMenu工tem〈“打'開文件”>;
this.jmi2=newJMenu工tem〈"選擇顏色
//注冊監(jiān)聽
this.jmil.addActionListener<this>;
this.jmil.setActionCommard<HopenFile">;
this.jmi2.addActionListener<this>;
this.jmi2.setActionCommancK'^hooseColor'^;
//把JMenuBar加^JFrame
this.setJMenuBar<jmb>;
//把菜單放到
this.jmb.add<this.jml>;
//把工tem放至jml
this.jml.add<this.jmil>;
this.jml.add<this.jmi2>;
this.add<jta>;
this.setTitleC”記事本/顏色選擇案例”>;
this.setSize<400z300>;
this.setLocation<200,200>;
this.setDefaultCloseOperation<JFrame.EXIT_ON_CLOSE>;
this.setvisible<true>;
)
publicvoidactionPerformed<ActionEvente>{
if<e.getActionCommando.equals<*'openFile">>{
System.out.print"打開
this.jfc=newJFileChoosero;
this.jfc.setDialogTitlev”請選擇文件...">;
16/23
〃打開的時候默認(rèn)的屬性
this.jfc.showOpenDialog<null>;
this.jfc.setVisible<true>;
//得到用戶選擇的文件路徑,絕對路徑
StringfilePath=
this.jfc.getSelectedFileO.getAbsolutePathO;
System,out.println<filePath>;
FileReaderfr=null;
BufferedReaderbr=null;
try(
fr=newFileReader<filePath>;
br=newBufferedReader<fr>;
Strings=n";
Stringsall=nn;
while<<s=br.readLine<>>!=null>{
//bufferedReader遇到換行即一行。。
s+=”\r\n”;//所以后面補上換行號
sall+=s;
)
this.jta.setText<sall>;
}catch<Exceptionel>{
el.printStackTraceO;
}finally(
try(
br.closeo;
fr.closeo;
}catch<I02xceptionel>{
el.printStackTraceo;
1
}
)
if<e.getActionCommando.equals<"chooseColor*'>>{
Colorcolor=newColor<0,0,0>;
color=JColorChooser.showDialog<this,"ChooseColor",color>;
ound<color>;
〃設(shè)置字體顏色
this.jta.setForeground<color>;
}
)
publicstaticvoidmain<String(]args>{
newTest9_12<>;
)
}
運行結(jié)果:
17/23
13.編寫一個圖形用戶界面,包括三個JSlider對象和三個JTexField對象。每一個JSlider代表顏
色中的紅、綠、藍(lán)三部份,它們的值從。到255,在相應(yīng)的JTextF回d中顯示各個JSlider的當(dāng)前
值。用這三個值作為Color類構(gòu)造方法的參數(shù)創(chuàng)建一個新的Color對象,用來填充一個矩形。
importjava.awt.*;
importjavax.swing.*;
importjavax.swing.event.*;
publicclassTest9_l3extendsJFrameimplementsChangeListener{
privateJTextFieldtextFieldl;
privateJTextFieldtextField2;
privateJTextFieldtextField3;
privateJLabellabell;
privateJLabellabel2;
privateJLabellabe13;
privateJSlidersliderl;
privateJSliderslider2;
privateJSliderslider3;
privateMyPanelmyPanel;
publicTest9_l3<>{
this.setTitle<nJSliderDemo'^;
this.setSize<200,200>;
this.setVisible<true>;
this.setDefaultCloseOperation<JFrame.EXIT_ON_CLOSE>;
this.setLayout<newGridLayout<4,3?;
this.labell=newJLabelv"紅”
this.Iabel2=newJLabelc"綠”
this.Iabel3=newJLabelc“藍(lán)"》
this.textFieldlnewJTextField<5>;
this.textField2newJTextField<5>;
this.textField3=newJTextField<5>;
this.sliderl=newJSlide
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025-2030全球顏料和染料噴墨標(biāo)簽打印機(jī)行業(yè)調(diào)研及趨勢分析報告
- 2025年全球及中國跑道人造草坪行業(yè)頭部企業(yè)市場占有率及排名調(diào)研報告
- 2025-2030全球汽車內(nèi)飾材料燃燒試驗機(jī)行業(yè)調(diào)研及趨勢分析報告
- 2025-2030全球大型球軸承(外徑尺寸在200mm以上)行業(yè)調(diào)研及趨勢分析報告
- 2025-2030全球住宅用窗戶防蟲網(wǎng)行業(yè)調(diào)研及趨勢分析報告
- 2025年全球及中國商用儲水式電熱水器行業(yè)頭部企業(yè)市場占有率及排名調(diào)研報告
- 2025-2030全球汽車空調(diào)風(fēng)門執(zhí)行器行業(yè)調(diào)研及趨勢分析報告
- 2025年全球及中國半導(dǎo)體高壓電源行業(yè)頭部企業(yè)市場占有率及排名調(diào)研報告
- 幼兒繪本講述與演繹幼兒繪本的選擇講解
- 2025農(nóng)村房屋贈與合同書
- 2024年全國職業(yè)院校技能大賽高職組(研學(xué)旅行賽項)考試題庫(含答案)
- 十八項核心制度
- 2025社保政策培訓(xùn)
- 電器儀表人員培訓(xùn)課件
- 2025年中小學(xué)春節(jié)安全教育主題班會課件
- 2023年工程制圖習(xí)題集
- 計量經(jīng)濟(jì)學(xué)練習(xí)題
- 2025年全國高考體育單招考試模擬政治試卷試題(含答案詳解)
- 傳統(tǒng)春節(jié)習(xí)俗
- 反走私課件完整版本
- 四年級下冊數(shù)學(xué)知識點總結(jié)
評論
0/150
提交評論