⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 asd.java

📁 实现java图形界面按钮点击功能并做出现相应处理
💻 JAVA
字号:

 import java.awt.*; 
import java.awt.event.*; 

import javax.swing.*; 

public class asd extends JFrame { 
	JButton button1 = new JButton("微笑"); 
	JButton button2 = new JButton("流泪"); 
	JButton button3 = new JButton("生气");
	JButton button4 = new JButton("退出"); 
	JLabel imgLabel = new JLabel();  
	public asd() { 
			JPanel cp = (JPanel) this.getContentPane(); 
			JPanel buttomPanel = new JPanel(); 
			JPanel imgPanel = new JPanel(); 
			//buttomPanel.setPreferredSize(new Dimension(100, 40)); 
			//button2.setEnabled(false); 
			buttomPanel.add(button1); 
			buttomPanel.add(button2);
			buttomPanel.add(button3); 
			buttomPanel.add(button4); 
			imgPanel.add(imgLabel); 
			button1.addActionListener(new ButtonListener()); 
			button2.addActionListener(new ButtonListener());
			button3.addActionListener(new ButtonListener());
			button4.addActionListener(new ButtonListener());

			cp.add(imgPanel, BorderLayout.CENTER); 
			cp.add(buttomPanel, BorderLayout.SOUTH); 
			this.setSize(400,400); 
			//this.setResizable(false); 
			this.setVisible(true); 
			this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
} 

class ButtonListener implements ActionListener { 
		public void actionPerformed(ActionEvent e) { 
		if(e.getSource() == button1) { 
			imgLabel.setIcon(new ImageIcon("smile.jpg")); 
			//button1.setEnabled(false); 
			//button2.setEnabled(true); 
          } 	
		else if(e.getSource() == button2) { 
			imgLabel.setIcon(new ImageIcon("sad.jpg"));
             } 
        else if(e.getSource() == button3) { 
	        imgLabel.setIcon(new ImageIcon("angry.jpg"));
             }
        else if(e.getSource() == button4) { 
	        System.exit(1);
             } 

} 
} 

public static void main(String args [] ) { 
new asd(); 
} 

}  
 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -