實(shí)驗(yàn)8-圖形界面程序設(shè)計(jì)_第1頁(yè)
實(shí)驗(yàn)8-圖形界面程序設(shè)計(jì)_第2頁(yè)
實(shí)驗(yàn)8-圖形界面程序設(shè)計(jì)_第3頁(yè)
實(shí)驗(yàn)8-圖形界面程序設(shè)計(jì)_第4頁(yè)
實(shí)驗(yàn)8-圖形界面程序設(shè)計(jì)_第5頁(yè)
已閱讀5頁(yè),還剩3頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

山西大學(xué)計(jì)算機(jī)與信息技術(shù)學(xué)院實(shí)驗(yàn)報(bào)告姓 名 郭彩峰學(xué) 號(hào) 專業(yè)班級(jí)軟一課程名稱 Java實(shí)驗(yàn)實(shí)驗(yàn)日期2014.12.11成 績(jī)指導(dǎo)教師 批改日期實(shí)驗(yàn)名稱實(shí)驗(yàn) 8 圖形界面程序設(shè)計(jì)一、實(shí)驗(yàn)?zāi)康恼莆粘S肎UI控制組件及其事件處理。二、實(shí)驗(yàn)內(nèi)容1編程包含一個(gè)標(biāo)簽和一個(gè)按鈕,單擊按鈕時(shí),標(biāo)簽的內(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); 運(yùn)行結(jié)果貼圖: 2編程包含一個(gè)文本框和一個(gè)文本區(qū)域,文本框內(nèi)容改變時(shí),將文本框中的內(nèi)容顯示在文本區(qū)域中;在文本框中按回車鍵時(shí),清空文本區(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(); 運(yùn)行結(jié)果貼圖: 3編程包含一個(gè)復(fù)選按鈕和一個(gè)普通按鈕,復(fù)選按鈕選中時(shí),普通按鈕的背景色為青色,未選中時(shí)為灰色。程序代碼: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(); 運(yùn)行結(jié)果貼圖: 4編程包含兩個(gè)按鈕和一個(gè)標(biāo)簽,將發(fā)生單擊事件的按鈕上的文本信息顯示在標(biāo)簽中。提示:關(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(標(biāo)簽); 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(); 運(yùn)行結(jié)果貼圖: 5編程確定當(dāng)前鼠標(biāo)的位置坐標(biāo)。程序代碼:import java.awt.*; import javax.swing.*; import java.awt.event.*; public class LocateMouse extends JFrame private JButton location; public LocateMouse() super(尋找鼠標(biāo)); location = new JButton(顯示鼠標(biāo)位置); location.setSize(20,10); add(location); location.addMouseMotionListener(new MouseMotionListener() public void mouseDragged(MouseEvent e) public void mouseMoved(MouseEvent e) location.setText(鼠標(biāo)現(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(); 運(yùn)行結(jié)果貼圖: 6編程使用BorderLayout布局方式放置5個(gè)按鈕。程序代碼: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); 運(yùn)行結(jié)果貼圖:7. 編寫程序,實(shí)現(xiàn)使用鍵盤上的上下左右箭頭控制界面上圖片的移動(dòng)。移動(dòng)到邊界時(shí)從界面另一側(cè)出現(xiàn)。移動(dòng)過(guò)程中顯示另一個(gè)圖片,停止時(shí)恢復(fù)原來(lái)的圖片。程序代碼: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. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論