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

📄 testactionevent.java

📁 这个也是初学GUI的必看程序,是事件驱动的例子,可以显示哪个按键被点击了
💻 JAVA
字号:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class TestActionEvent extends JFrame implements ActionListener{
	
	private JButton jbtOk = new JButton("OK");
	private JButton jbtCancel = new JButton("Cancel");
	MessagePanel p3 = new MessagePanel();
	
	public TestActionEvent (){
		
		setTitle("TestActionEvent");
		
		getContentPane().setLayout(new GridLayout(2,1));
		
		  
		 JPanel p2 = new JPanel();/*contains two buttons*/
		 p2.setLayout(new FlowLayout(FlowLayout.CENTER,1,5));
		 p2.add(jbtOk);
		 p2.add(jbtCancel);
		 
		 
		 
		 getContentPane().add(p3);
		 getContentPane().add(p2);
		 
		
		 
		
		jbtOk.addActionListener(this);
		jbtCancel.addActionListener(this);
		
		
		
	}
	
	public static void main(String[] args){
		
		TestActionEvent frame = new TestActionEvent();
		
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		frame.setSize(200,200);
		frame.setVisible(true);
	}


 
	
	public void actionPerformed(ActionEvent e){
		
		Object a = e.getSource();
		
		if(a==jbtOk){
			
		p3.setMessage("OK is clicked");	
			
		}
		
		else if(a==jbtCancel){
		p3.setMessage("Cancel is clicked")	;
			
		}
	}

}

class MessagePanel extends JPanel {
  /** The message to be displayed */
  public String message = "";

  /** The x coordinate where the message is displayed */
  private int xCoordinate = 20;

  /** The y coordinate where the message is displayed */
  private int yCoordinate = 20;

  /** Indicate whether the message is displayed in the center */
  private boolean centered;

  /** The interval for moving the message horizontally and vertically */
  private int interval = 10;

  /** Default constructor */
  public MessagePanel() {
  }

  /** Constructor with a message parameter */
  public MessagePanel(String message) {
    this.message = message;
  }

  /** Return message */
  public String getMessage() {
    return message;
  }

  /** Set a new message */
  public void setMessage(String message) {
    this.message = message;
    repaint();
  }

  /** Return xCoordinator */
  public int getXCoordinate() {
    return xCoordinate;
  }

  /** Set a new xCoordinator */
  public void setXCoordinate(int x) {
    this.xCoordinate = x;
    repaint();
  }

  /** Return yCoordinator */
  public int getYCoordinate() {
    return yCoordinate;
  }

  /** Set a new yCoordinator */
  public void setYCoordinate(int y) {
    this.yCoordinate = y;
    repaint();
  }

  /** Return centered */
  public boolean isCentered() {
    return centered;
  }

  /** Set a new centered */
  public void setCentered(boolean centered) {
    this.centered = centered;
    repaint();
  }

  /** Return interval */
  public int getInterval() {
    return interval;
  }

  /** Set a new interval */
  public void setInterval(int interval) {
    this.interval = interval;
    repaint();
  }

  /** Paint the message */
  protected void paintComponent(Graphics g) {
    super.paintComponent(g);

    if (centered) {
      // Get font metrics for the current font
      FontMetrics fm = g.getFontMetrics();

      // Find the center location to display
      int stringWidth = fm.stringWidth(message);
      int stringAscent = fm.getAscent();
      // Get the position of the leftmost character in the baseline
      xCoordinate = getWidth() / 2 - stringWidth / 2;
      yCoordinate = getHeight() / 2 + stringAscent / 2;
    }

    g.drawString(message, xCoordinate, yCoordinate);
  }

  /** Move the message left */
  public void moveLeft() {
    xCoordinate -= interval;
    repaint();
  }

  /** Move the message right */
  public void moveRight() {
    xCoordinate += interval;
    repaint();
  }

  /** Move the message up */
  public void moveUp() {
    yCoordinate -= interval;
    repaint();
  }

  /** Move the message down */
  public void moveDown() {
    yCoordinate -= interval;
    repaint();
  }

  /** Override get method for preferredSize */
  public Dimension getPreferredSize() {
    return new Dimension(200, 30);
  }
} 

⌨️ 快捷键说明

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