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

📄 ihm.java

📁 a program in java to control the automatic door in the garage
💻 JAVA
字号:
package be.ihm;

import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JToggleButton;

import be.garage.PorteGarage;
import be.garage.PorteGarage.EtatPorte;

public final class Ihm extends JFrame {
	private static final long serialVersionUID = 2604010145707300694L;

	private JToggleButton obstacle;

	private JButton telecommande;

	private JButton boutonPorte;

	private GridBagLayout layout;

	private GridBagConstraints c;

	private Container conteneur;

	private PorteGarage porte;

	public Ihm(PorteGarage porte) {
		super();

		assert porte != null;

		conteneur = getContentPane();
		layout = new GridBagLayout();
		c = new GridBagConstraints();
		conteneur.setLayout(layout);
		this.porte = porte;

		setSize(300, 200);
		traiterFin();
	}

	private void traiterFin() {
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
	}

	public void updateEtat() {
		if (porte.getEtat() == EtatPorte.OUVERTE) {
			boutonPorte.setText("Porte ouverte");
			boutonPorte.setBackground(Color.GREEN);
		} else if (porte.getEtat() == EtatPorte.OUVRANTE) {
			boutonPorte.setText("Porte en cours d'ouverture");
			boutonPorte.setBackground(Color.ORANGE);
		} else if (porte.getEtat() == EtatPorte.FERMANTE) {
			boutonPorte.setText("Porte en cours de fermeture");
			boutonPorte.setBackground(Color.ORANGE);
		} else if (porte.getEtat() == EtatPorte.FERMEE) {
			boutonPorte.setText("Porte ferm閑");
			boutonPorte.setBackground(Color.RED);
		} else {
			assert false;
		}
	}

	public void init(ActionListener listener) {
		assert listener != null;
		obstacle = new JToggleButton("Obstacle");
		telecommande = new JButton("T閘閏ommande");
		boutonPorte = new JButton();
		boutonPorte.setEnabled(false);
		c.fill = GridBagConstraints.BOTH;
		c.weightx = 1;
		c.weighty = 1;

		// ent阾e des colonnes
		c.gridy = 0;
		c.gridx = 0;
		layout.setConstraints(obstacle, c);
		c.gridy = 0;
		c.gridx = 1;
		layout.setConstraints(telecommande, c);
		c.gridy = 1;
		c.gridx = 0;
		c.gridwidth = 2;
		c.gridheight = 1;
		layout.setConstraints(boutonPorte, c);
		telecommande.addActionListener(listener);
		obstacle.addActionListener(listener);
		conteneur.add(telecommande);
		conteneur.add(obstacle);
		conteneur.add(boutonPorte);

		updateEtat();
		setVisible(true);
		repaint();
	}

	public void paint(Graphics g) {
		obstacle.repaint();
		telecommande.repaint();
		boutonPorte.repaint();
	}

	/**
	 * @return Returns the obstacle.
	 */
	public JToggleButton getBoutonObstacle() {
		return obstacle;
	}

	/**
	 * @return Returns the clic.
	 */
	public JButton getBoutonTelecommande() {
		return telecommande;
	}
}

⌨️ 快捷键说明

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