JAVA實驗-圖形界面設(shè)計_第1頁
JAVA實驗-圖形界面設(shè)計_第2頁
JAVA實驗-圖形界面設(shè)計_第3頁
JAVA實驗-圖形界面設(shè)計_第4頁
JAVA實驗-圖形界面設(shè)計_第5頁
已閱讀5頁,還剩15頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、精選優(yōu)質(zhì)文檔-傾情為你奉上廣州大學學生實驗報告開課學院及實驗室:計算機科學與工程實驗室2014年11月14日學院計算機科學與教育軟件學院年級/專業(yè)/班姓名學號實驗課程名稱Java語言成績實驗項目名稱圖形界面設(shè)計指導老師一、實驗目的實驗十 圖形用戶界面(1)1.了解圖形用戶界面基本組件窗口、按鈕、文本框、選擇框、滾動條等的使用方法,2.了解如何使用布局管理器對組件進行管理,以及如何使用Java 的事件處理機制。實驗十一 圖形用戶界面(2)1.了解圖形用戶界面基本組件窗口、按鈕、文本框、選擇框、滾動條等的使用方法,2.了解如何使用布局管理器對組件進行管理,以及如何使用Java 的事件處理機制。二、

2、實驗器材MacBook Pro一臺操作系統(tǒng):OS X Yosemite編程軟件:eclipse3、 實驗要求實驗十 圖形用戶界面(1)1. 理解Java 的事件處理機制,掌握為不同組件編寫事件處理程序的方法。2. 掌握編寫?yīng)毩⑦\行的窗口界面的方法。3. 了解Java Swing 組件的使用方法。4. 了解對話框的使用方法。實驗十一 圖形用戶界面(2)1. 理解Java 的事件處理機制,掌握為不同組件編寫事件處理程序的方法。2. 掌握編寫?yīng)毩⑦\行的窗口界面的方法。3. 了解Java Swing 組件的使用方法。4. 了解對話框的使用方法。四、實驗過程原始數(shù)據(jù)記錄實驗十 圖形用戶界面(1)1. 如

3、下圖所示,用了三個文本框,第一個文本框給用戶輸入商品單價,第二個則是給用戶輸入商品數(shù)量,第三個用于顯示總金額。代碼:import java.awt.FlowLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JTextField;impo

4、rt javax.swing.SwingConstants;import javax.swing.event.DocumentEvent;import javax.swing.event.DocumentListener;import javax.swing.text.BadLocationException;import javax.swing.text.Document;public class test_2_1_1 extends JFrame implements ActionListenerpublic static void main(String args) / TODO Aut

5、o-generated method stubtest_2_1_1 good = new test_2_1_1();JTextField goodPriceTF;JTextField goodNumTF;JTextField sumPriceTF;JLabel goodPriceTipsLabel;JLabel goodNumTipsLabel;Boolean canCal = false;public test_2_1_1()super("商品價格計算器");setSize(435, 135);setDefaultCloseOperation(JFrame.EXIT_ON

6、_CLOSE);/ 設(shè)置JFrame是否可以改變大小。 setResizable(false); / JFrame打開后居中。 setLocationRelativeTo(getOwner(); FlowLayout flo = new FlowLayout();setLayout(flo);JLabel goodPriceLabel = new JLabel("商品價格",JLabel.RIGHT);JLabel goodNumLabel = new JLabel("商品數(shù)量",JLabel.RIGHT);JLabel sumPriceLabel =

7、new JLabel("商品總額",JLabel.RIGHT);goodPriceTipsLabel = new JLabel("請輸入價格",JLabel.RIGHT);goodNumTipsLabel = new JLabel("請輸入數(shù)量",JLabel.RIGHT);goodPriceTF = new JTextField(20);goodPriceTF.getDocument().addDocumentListener(new DocumentListener() Overridepublic void removeUpda

8、te(DocumentEvent e) / TODO Auto-generated method stubchangeTFTipsLabel(goodPriceTipsLabel, goodPriceTF.getText();Overridepublic void insertUpdate(DocumentEvent e) / TODO Auto-generated method stubchangeTFTipsLabel(goodPriceTipsLabel, goodPriceTF.getText();Overridepublic void changedUpdate(DocumentEv

9、ent e) / TODO Auto-generated method stubchangeTFTipsLabel(goodPriceTipsLabel, goodPriceTF.getText(););goodNumTF = new JTextField(20);goodNumTF.getDocument().addDocumentListener(new DocumentListener() Overridepublic void removeUpdate(DocumentEvent e)/ TODO Auto-generated method stubchangeTFTipsLabel(

10、goodNumTipsLabel, goodNumTF.getText();Overridepublic void insertUpdate(DocumentEvent e) / TODO Auto-generated method stubchangeTFTipsLabel(goodNumTipsLabel, goodNumTF.getText();Overridepublic void changedUpdate(DocumentEvent e) / TODO Auto-generated method stubchangeTFTipsLabel(goodNumTipsLabel, goo

11、dNumTF.getText(););sumPriceTF = new JTextField(20);sumPriceTF.setEditable(false);JButton btn = new JButton("計算");btn.addActionListener(this);add(goodPriceLabel);add(goodPriceTF);add(goodPriceTipsLabel);add(goodNumLabel);add(goodNumTF);add(goodNumTipsLabel);add(sumPriceLabel);add(sumPriceTF

12、);add(btn);setVisible(true);public void actionPerformed(ActionEvent event)if (event.getActionCommand().equals("計算")if (!canCal)showMessage("輸入的數(shù)據(jù)不合法");return;double sum = Double.parseDouble(goodPriceTF.getText() * Double.parseDouble(goodNumTF.getText();sumPriceTF.setText(Double.t

13、oString(sum);/檢測文本是否純數(shù)字public Boolean isNumber(String s)if (s.length() = 0)return false;Boolean isNumber = true;char c = s.toCharArray();for (int i = 0;i < s.length();i+)if (Character.isDigit(ci) = false)isNumber = false;break;return isNumber;/彈出警告public void showMessage(String s)JOptionPane.show

14、MessageDialog(null, s, "警告", JOptionPane.PLAIN_MESSAGE);/修改文本提示labelpublic void changeTFTipsLabel(JLabel aLabel,String str)/if (isNumber(str) = false)canCal = false;aLabel.setText("X ");else if (isNumber(goodPriceTF.getText() | isNumber(goodNumTF.getText()if (isNumber(goodPriceTF

15、.getText() && isNumber(goodNumTF.getText()canCal = true;aLabel.setText(" ");else if (str.length() = 0)canCal = false;if (aLabel = goodPriceTipsLabel)aLabel.setText("請輸入價格");elseaLabel.setText("請輸入數(shù)量");運行結(jié)果:啟動畫面當輸入的字符是數(shù)字,對應(yīng)右邊的框旁邊會提示 ,如果輸入非法字符,則會出現(xiàn) X,此時點擊計算,則會彈出錯誤

16、警告框。輸入正確數(shù)字后,點計算按鈕,得到正確結(jié)果。2. 制作如下圖所示的界面,當用戶點擊單選按鈕時,會在一個標簽上顯示出當前所選定的數(shù)據(jù)庫服務(wù)器類型。代碼:import java.awt.EventQueue;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.ButtonGroup;import javax.swing.JFrame;import javax.swing.JLabel;import javax.s

17、wing.JRadioButton;public class test_2_1_2 implements ActionListenerprivate JFrame frmAsdfasdf;JLabel label;/* * Launch the application. */public static void main(String args) EventQueue.invokeLater(new Runnable() public void run() try test_2_1_2 window = new test_2_1_2();window.frmAsdfasdf.setVisibl

18、e(true); catch (Exception e) e.printStackTrace(););/* * Create the application. */public test_2_1_2() initialize();/* * Initialize the contents of the frame. */private void initialize() frmAsdfasdf = new JFrame();frmAsdfasdf.setTitle("數(shù)據(jù)庫");frmAsdfasdf.setBounds(100, 100, 251, 301);frmAsdf

19、asdf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frmAsdfasdf.getContentPane().setLayout(new GridLayout(0, 1, 0, 0);/創(chuàng)建Radio按鈕JRadioButton rdbtnNewRadioButton_1 = new JRadioButton("SQL");frmAsdfasdf.getContentPane().add(rdbtnNewRadioButton_1);rdbtnNewRadioButton_1.addActionListener(this)

20、;JRadioButton rdbtnNewRadioButton = new JRadioButton("Oracle");frmAsdfasdf.getContentPane().add(rdbtnNewRadioButton);rdbtnNewRadioButton.addActionListener(this);JRadioButton rdbtnNewRadioButton_2 = new JRadioButton("SQLite");frmAsdfasdf.getContentPane().add(rdbtnNewRadioButton_2)

21、;rdbtnNewRadioButton_2.addActionListener(this);/添加按鈕ButtonGroup bg=new ButtonGroup();bg.add(rdbtnNewRadioButton);bg.add(rdbtnNewRadioButton_1);bg.add(rdbtnNewRadioButton_2);label = new JLabel("學習使用JRadioButton控件");frmAsdfasdf.getContentPane().add(label);public void actionPerformed(ActionEv

22、ent event)if (event.getSource() instanceof JRadioButton)label.setText(event.getActionCommand();運行結(jié)果:啟動截圖點擊Oracle選項實驗十一 圖形用戶界面(2)1. 創(chuàng)建如圖所示的菜單代碼:import java.awt.EventQueue;import javax.swing.JFrame;import javax.swing.JMenuBar;import javax.swing.JMenuItem;import javax.swing.JMenu;import java.awt.Compon

23、ent;import javax.swing.Box;import javax.swing.JCheckBoxMenuItem;import javax.swing.JCheckBox;import javax.swing.JRadioButton;import javax.swing.JSeparator;public class test_2_2 private JFrame frmMenu;/* * Launch the application. */public static void main(String args) EventQueue.invokeLater(new Runna

24、ble() public void run() try test_2_2 window = new test_2_2();window.frmMenu.setVisible(true); catch (Exception e) e.printStackTrace(););/* * Create the application. */public test_2_2() initialize();/* * Initialize the contents of the frame. */private void initialize() frmMenu = new JFrame();frmMenu.

25、setTitle("Menu菜單使用");frmMenu.setBounds(100, 100, 349, 219);frmMenu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);JMenuBar menuBar = new JMenuBar();frmMenu.setJMenuBar(menuBar);JMenu mnNewMenu = new JMenu("File");menuBar.add(mnNewMenu);JMenuItem mntmNewMenuItem = new JMenuItem(&q

26、uot;New");mnNewMenu.add(mntmNewMenuItem);JMenuItem mntmNewMenuItem_1 = new JMenuItem("Open");mnNewMenu.add(mntmNewMenuItem_1);JMenuItem mntmNewMenuItem_2 = new JMenuItem("Close");mnNewMenu.add(mntmNewMenuItem_2);JMenu mnNewMenu_1 = new JMenu("Option");menuBar.add(mnNewMenu_1);JMenuItem mntmNewMenuItem_4 = new JMenuItem("Font.");mnNewMenu_1.add(mntmNewMenuItem_4);JMenu mnNewMenu_2 = new JMenu("Color.");mnNewMenu_1.add(mnNewMenu_2);JMenuItem mntmBlack = new JMenuItem("Black");mnNewMenu_2.add

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論