




已閱讀5頁,還剩3頁未讀, 繼續(xù)免費閱讀
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
山西大學(xué)計算機與信息技術(shù)學(xué)院實驗報告姓 名 郭彩峰學(xué) 號 專業(yè)班級軟一課程名稱 Java實驗實驗日期2014.12.11成 績指導(dǎo)教師 批改日期實驗名稱實驗 8 圖形界面程序設(shè)計一、實驗?zāi)康恼莆粘S肎UI控制組件及其事件處理。二、實驗內(nèi)容1編程包含一個標簽和一個按鈕,單擊按鈕時,標簽的內(nèi)容在“你好”和“再見”之間切換。程序代碼:import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ChangeGUI extends JFrame private JButton button; private JLabel label; public ChangeGUI() super(Say Hello); JPanel panel = new JPanel(); JPanel panel2 = new JPanel(); setLayout(new GridLayout(2,1,0,5); button = new JButton(OK); button.setBackground(Color.gray); button.setForeground(Color.RED); panel.add(button); button.addActionListener(new OKActionListener(); label = new JLabel(你好); label.setForeground(Color.BLUE); panel2.add(label); add(panel2); add(panel); private class OKActionListener implements ActionListener public void actionPerformed(ActionEvent e) if(label.getText()=你好) label.setText(再見); else label.setText(你好); public static void main(String args) ChangeGUI change = new ChangeGUI(); change.setSize(200, 100); change.setVisible(true); change.setLocationRelativeTo(null); change.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 運行結(jié)果貼圖: 2編程包含一個文本框和一個文本區(qū)域,文本框內(nèi)容改變時,將文本框中的內(nèi)容顯示在文本區(qū)域中;在文本框中按回車鍵時,清空文本區(qū)域的內(nèi)容。程序代碼:import java.awt.*; import javax.swing.*; import javax.swing.border.*; import java.awt.event.*; public class ShowText extends JFrame private JTextField text1; private JTextArea text2; public ShowText() super(Tetx Show); JPanel p1 = new JPanel(); p1.setBackground(Color.BLUE); p1.setBorder(new TitledBorder(文本框); text1 = new JTextField(10); text1.addKeyListener(new TextListener(); p1.add(text1); JPanel p2 = new JPanel(); p2.setBackground(Color.YELLOW); p2.setBorder(new TitledBorder(文本區(qū)域); text2 = new JTextArea(原文本,10,10); text2.setLineWrap(true); text2.setEditable(false); p2.add(text2); setLayout(new GridLayout(2,1,0,5); add(p1); add(p2); setSize(200,200); setVisible(true); this.setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); private class TextListener implements KeyListener public void keyPressed(KeyEvent e) public void keyReleased(KeyEvent e) if(e.getKeyChar()!=KeyEvent.VK_ENTER) text2.setText(text1.getText(); public void keyTyped(KeyEvent e) if(e.getKeyChar()=KeyEvent.VK_ENTER) text2.setText(null); public static void main(String args) JFrame frame = new ShowText(); 運行結(jié)果貼圖: 3編程包含一個復(fù)選按鈕和一個普通按鈕,復(fù)選按鈕選中時,普通按鈕的背景色為青色,未選中時為灰色。程序代碼:import java.awt.*; import javax.swing.*; import java.awt.event.*; public class ChangeButtonColor extends JFrame private JButton button; private JCheckBox checkBox; public ChangeButtonColor() super(改變按鈕顏色); JPanel p1 = new JPanel(); p1.setBackground(Color.RED); setLayout(new GridLayout(2,1); button = new JButton(Hello); button.setSize(20, 20); button.setBackground(Color.GREEN); p1.add(button); JPanel p2 = new JPanel(); p2.setBackground(Color.CYAN); checkBox = new JCheckBox(); checkBox.addItemListener(new checkBoxListener(); p2.add(checkBox); add(p1); add(p2); setSize(200,200); setVisible(true); this.setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); private class checkBoxListener implements ItemListener public void itemStateChanged(ItemEvent e) if(checkBox.isSelected() button.setBackground(Color.CYAN); else button.setBackground(Color.GREEN); public static void main(String args) ChangeButtonColor b = new ChangeButtonColor(); 運行結(jié)果貼圖: 4編程包含兩個按鈕和一個標簽,將發(fā)生單擊事件的按鈕上的文本信息顯示在標簽中。提示:關(guān)鍵代碼如下: b1.addActionListener(new B1(); b2.addActionListener(new B2(); class B1 implements ActionListener public void actionPerformed(ActionEvent e) who.setText(Button 1); class B2 implements ActionListener public void actionPerformed(ActionEvent e) who.setText(Button 2); 程序代碼:import java.awt.*; import javax.swing.*; import java.awt.event.*; public class ShowButtonText extends JFrame private JButton b1; private JButton b2; private JLabel label; public ShowButtonText() super(顯示選中按鈕信息); setLayout(new GridLayout(2,1); JPanel p1 = new JPanel(); p1.setBackground(Color.WHITE); label = new JLabel(標簽); label.setSize(20, 10); label.setBackground(Color.BLUE); p1.add(label); add(p1); JPanel p2 = new JPanel(); p2.setBackground(Color.WHITE); b1 = new JButton(你好); b2 = new JButton(再見); ButtonListener b = new ButtonListener(); b1.addActionListener(b); b2.addActionListener(b); p2.add(b1); p2.add(b2); add(p2); setSize(200,200); setVisible(true); this.setLocationRelativeTo(null); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); private class ButtonListener implements ActionListener public void actionPerformed(ActionEvent e) if(e.getSource()=b1) label.setText(b1.getText(); else if(e.getSource()=b2) label.setText(b2.getText(); public static void main(String args) ShowButtonText s = new ShowButtonText(); 運行結(jié)果貼圖: 5編程確定當前鼠標的位置坐標。程序代碼:import java.awt.*; import javax.swing.*; import java.awt.event.*; public class LocateMouse extends JFrame private JButton location; public LocateMouse() super(尋找鼠標); location = new JButton(顯示鼠標位置); location.setSize(20,10); add(location); location.addMouseMotionListener(new MouseMotionListener() public void mouseDragged(MouseEvent e) public void mouseMoved(MouseEvent e) location.setText(鼠標現(xiàn)在位于(+e.getX()+,+e.getY()+); ); setSize(300,200); setLocationRelativeTo(null); setVisible(true); location.setBackground(Color.RED); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); public static void main(String args) LocateMouse mouse = new LocateMouse(); 運行結(jié)果貼圖: 6編程使用BorderLayout布局方式放置5個按鈕。程序代碼:import java.awt.*; import javax.swing.*; import java.awt.event.*; public class TestBorderLayout extends JFrame private JButton nButton = new JButton(北); private JButton sButton = new JButton(南); private JButton wButton = new JButton(西); private JButton eButton = new JButton(東); private JButton cButton = new JButton(中); public TestBorderLayout() setLayout(new BorderLayout(5,5); add(nButton,BorderLayout.NORTH); add(sButton,BorderLayout.SOUTH); add(wButton,BorderLayout.WEST); add(eButton,BorderLayout.EAST); add(cButton,BorderLayout.CENTER); nButton.setBackground(Color.PINK); sButton.setBackground(Color.PINK); wButton.setBackground(Color.PINK); eButton.setBackground(Color.PINK); cButton.setBackground(Color.PINK); public static void main(String args) TestBorderLayout t = new TestBorderLayout(); t.setSize(250,200); t.setVisible(true); t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); t.setLocationRelativeTo(null); 運行結(jié)果貼圖:7. 編寫程序,實現(xiàn)使用鍵盤上的上下左右箭頭控制界面上圖片的移動。移動到邊界時從界面另一側(cè)出現(xiàn)。移動過程中顯示另一個圖片,停止時恢復(fù)原來的圖片。程序代碼:import java.awt.*; import javax.swing.*; import java.awt.event.*; public class MoveImage extends JFrame private ImageIcon oneIcon = new ImageIcon(image/happy.jpg); private ImageIcon twoIcon = new ImageIcon(image/hello.jpg); private JLabel label; JPanel p; public MoveImage() super(Image Move); setSize(500,500); setLocationRelativeTo(null); label = new JLabel(oneIcon); p = new JPanel(); setContentPane(p); p.setLayout(null); this.addKeyListener(new PanelListener(); label.setBounds(0, 0, 100, 100); p.add(label); p.setBa
溫馨提示
- 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)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 租車怎么寫合同協(xié)議書模板
- 房地產(chǎn)開發(fā)拆遷安置合同示范文本集
- 智能車庫門定制設(shè)計與施工一體化服務(wù)合同
- 培訓(xùn)機構(gòu)成績比較協(xié)議
- 出海貨物運輸安全監(jiān)管合同
- 地質(zhì)測繪數(shù)據(jù)保密與安全協(xié)議
- 廠房租賃居間服務(wù)與物業(yè)管理協(xié)議
- 鋼鐵行業(yè)定制化運輸合同標準范本
- 桉樹撫育施肥與林業(yè)生態(tài)補償機制承包協(xié)議
- 廠房拆遷補償與職工權(quán)益保障及就業(yè)促進協(xié)議
- 羅森加盟合同協(xié)議
- 跨學(xué)科實踐活動05 基于碳中和理念設(shè)計低碳行動方案(活動設(shè)計)-2024-2025學(xué)年九年級化學(xué)跨學(xué)科實踐活動教學(xué)教學(xué)設(shè)計+設(shè)計(人教版2024)
- 2025年中考英語押題預(yù)測卷(徐州專用)(原卷版)
- 前程無憂測評題庫
- 2025-2030中國馬丁靴行業(yè)發(fā)展分析及發(fā)展前景與投資研究報告
- 證券投資學(xué) 課件 第一章 導(dǎo)論
- 锝99mTc替曲膦注射液-藥品臨床應(yīng)用解讀
- 2025年食品生產(chǎn)初級考試試題及答案
- 2025年由民政局策劃的離婚協(xié)議范本
- 《電路分析基礎(chǔ)》模擬試卷 期末考試卷AB卷4套帶答案
- 中職語文職業(yè)模塊期末綜合測試題(三)
評論
0/150
提交評論