Bản quyền thuộc về TITV.vn,
vui lòng không đăng tải lại nội dung từ trang này.
Video giải thích chi tiết
https://docs.oracle.com/javase/7/docs/api/java/awt/Font.html
Code chi tiết
package model;
public class LastButtonModel {
private int value;
public LastButtonModel() {
this.value = 1;
}
/**
* @return the value
*/
public int getValue() {
return value;
}
/**
* @param value the value to set
*/
public void setValue(int value) {
this.value = value;
}
public void setValue_1() {
this.setValue(1);
}
public void setValue_2() {
this.setValue(2);
}
public void setValue_3() {
this.setValue(3);
}
public void setValue_4() {
this.setValue(4);
}
}
package view;
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import controller.LastButtonListener;
import model.LastButtonModel;
public class LastButtonView extends JFrame{
private LastButtonModel lastButtonModel;
private JLabel jLabel;
public LastButtonView() {
this.lastButtonModel = new LastButtonModel();
this.init();
}
private void init() {
this.setTitle("Last Button");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(300,300);
JPanel jpanel_Center = new JPanel();
jpanel_Center.setLayout(new GridLayout(2,2));
// Tao listener
LastButtonListener lastButtonListener = new LastButtonListener(this);
// Tao font chu
Font font = new Font("Arial", Font.BOLD, 40);
JButton jButton_1 = new JButton("1");
// Gan font chu
jButton_1.setFont(font);
jButton_1.addActionListener(lastButtonListener);
JButton jButton_2 = new JButton("2");
jButton_2.setFont(font);
jButton_2.addActionListener(lastButtonListener);
JButton jButton_3 = new JButton("3");
jButton_3.setFont(font);
jButton_3.addActionListener(lastButtonListener);
JButton jButton_4 = new JButton("4");
jButton_4.setFont(font);
jButton_4.addActionListener(lastButtonListener);
jpanel_Center.add(jButton_1);
jpanel_Center.add(jButton_2);
jpanel_Center.add(jButton_3);
jpanel_Center.add(jButton_4);
JPanel jpanel_Footer = new JPanel();
jLabel = new JLabel("------");
jLabel.setFont(font);
jpanel_Footer.add(jLabel);
this.setLayout(new BorderLayout());
this.add(jpanel_Center, BorderLayout.CENTER);
this.add(jpanel_Footer, BorderLayout.SOUTH);
this.setVisible(true);
}
public void changeTo_1() {
this.lastButtonModel.setValue_1();
jLabel.setText("Last button: "+this.lastButtonModel.getValue());
}
public void changeTo_2() {
this.lastButtonModel.setValue_2();
jLabel.setText("Last button: "+this.lastButtonModel.getValue());
}
public void changeTo_3() {
this.lastButtonModel.setValue_3();
jLabel.setText("Last button: "+this.lastButtonModel.getValue());
}
public void changeTo_4() {
this.lastButtonModel.setValue_4();
jLabel.setText("Last button: "+this.lastButtonModel.getValue());
}
}
package controller;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import view.LastButtonView;
public class LastButtonListener implements ActionListener{
private LastButtonView lastButtonView;
public LastButtonListener(LastButtonView lastButtonView) {
this.lastButtonView = lastButtonView;
}
@Override
public void actionPerformed(ActionEvent e) {
String nguonSuKien = e.getActionCommand();
if (nguonSuKien.equals("1")) {
this.lastButtonView.changeTo_1();
}else if (nguonSuKien.equals("2")) {
this.lastButtonView.changeTo_2();
}else if (nguonSuKien.equals("3")) {
this.lastButtonView.changeTo_3();
}else if (nguonSuKien.equals("4")) {
this.lastButtonView.changeTo_4();
}
}
}
package test;
import view.LastButtonView;
public class Test {
public static void main(String[] args) {
new LastButtonView();
}
}
Không có nhận xét nào:
Đăng nhận xét