建立一个工程文件 File---new--java project Use default location 选择保存的地址
建立一个可视化图形界面 leelen(工程名称)点击右键 new--other-- Application Window new--folder--images(photo)
下面为关于一个图形界面,按键的监听,和面板排版的一个实例
(引用的照片为images内容,未上传,可在百度云里查找)
package leelen;
import java.awt.EventQueue;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Color;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JSplitPane;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.ImageIcon;
import javax.swing.JInternalFrame;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;
import java.awt.*;
import java.awt.event.*;
import java.text.SimpleDateFormat;
import java.awt.Graphics;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.*;
public class Family {
private JTextArea textArea;
private JFrame frame;
private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //时间显示格式
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Family window = new Family();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Family() {
initialize();
}
boolean firstDigit =true;
/**
* 处理数字键被按下的事件
*
* @param key
*/
private void handleNumber(String key) {
if (firstDigit) {
// 输入的第一个数字
textArea.setText(key);
} else if (!key.equals(".")) {
// 如果输入的不是小数点,则将数字附在结果文本框的后面
textArea.setText(textArea.getText() + key);
}
// 以后输入的肯定不是第一个数字了
if(key.equals("c")){
textArea.setText("");
}
if(key.equals("00#0001")){
textArea.setText("00#0001");
}
firstDigit = false;
firstDigit = false;
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 1000, 720);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Timer timer = new Timer();
timer.schedule(new ShowTime(), new Date(), 1000);
JInternalFrame subFrame = new JInternalFrame();//JInternalFrame 内容窗格是添加子组件的地方
JPanel panel = new JPanel();
panel.setBackground(new Color(86,135,216));
FlowLayout flowLayout = (FlowLayout) panel.getLayout();
flowLayout.setVgap(20);
flowLayout.setHgap(20);
frame.getContentPane().add(panel, BorderLayout.NORTH);
JLabel label_1 = new JLabel("\u5BB6\u5EAD\u5B89\u9632\u76D1\u63A7\u5BA2\u6237\u7AEF");
label_1.setFont(new Font("宋体", Font.PLAIN, 22));
label_1.setForeground(Color.WHITE);
panel.add(label_1);
JLabel label = new JLabel();
label.setForeground(Color.WHITE);
label.setFont(new Font("宋体", Font.PLAIN, 30));
panel.add(label);
TimePanel panel_1 = new TimePanel();
panel_1.setBackground(new Color(86,135,216));
FlowLayout flowLayout_1 = (FlowLayout) panel_1.getLayout();
flowLayout_1.setHgap(15);
flowLayout_1.setVgap(15);
frame.getContentPane().add(panel_1, BorderLayout.SOUTH);
//panel_1.add(new TimePanel(),BorderLayout.SOUTH);
JPanel panel_2 = new JPanel();
FlowLayout flowLayout_2 = (FlowLayout) panel_2.getLayout();
flowLayout_2.setVgap(100);
flowLayout_2.setHgap(45);
frame.getContentPane().add(panel_2, BorderLayout.WEST);
JPanel panel_4 = new JPanel();
panel_4.setBackground(Color.LIGHT_GRAY);
FlowLayout flowLayout_3 = (FlowLayout) panel_4.getLayout();
flowLayout_3.setVgap(200);
flowLayout_3.setHgap(300);
panel_2.add(panel_4);
JPanel panel_3 = new JPanel();
frame.getContentPane().add(panel_3, BorderLayout.CENTER);
JButton btnNewButton = new JButton("");
btnNewButton.setBackground(Color.WHITE);
btnNewButton.setForeground(Color.WHITE);
btnNewButton.setIcon(new ImageIcon("images/photo.png"));
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
panel_3.add(btnNewButton);
JLabel lblNewLabel = new JLabel("\u8BF7\u8F93\u5165\u53F7\u7801\uFF1A\uFF08\u697C\u5B87#\u623F\u53F7\uFF09 \u5982\uFF1A16#2305");
panel_3.add(lblNewLabel);
textArea = new JTextArea(18,18);
textArea.setText(" ");
textArea.setBackground(Color.LIGHT_GRAY);
textArea.setLineWrap(true);
textArea.setTabSize(10);
textArea.setRows(2);
panel_3.add(textArea);
JButton btnNewButton_1 = new JButton("");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleNumber("1");
}
});
JButton btnNewButton_welcome = new JButton("welcome home");
JButton btnNewButton_w = new JButton("\u7269\u4E1A");
btnNewButton_w.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleNumber("00#0001");
}
});
panel_3.add(btnNewButton_w);
panel_3.add(btnNewButton_1);
btnNewButton_1.setBackground(Color.WHITE);
btnNewButton_1.setForeground(Color.WHITE);
btnNewButton_1.setIcon(new ImageIcon("images/1.png"));
btnNewButton_1.setHorizontalAlignment(SwingConstants.LEFT);
panel_3.add(btnNewButton_1);
JButton btnNewButton_2 = new JButton("");
btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleNumber("2");
}
});
btnNewButton_2.setBackground(Color.WHITE);
btnNewButton_2.setForeground(Color.WHITE);
btnNewButton_2.setIcon(new ImageIcon("images/2.png"));
panel_3.add(btnNewButton_2);
JButton btnNewButton_3 = new JButton("");
btnNewButton_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleNumber("3");
}
});
btnNewButton_3.setBackground(Color.WHITE);
btnNewButton_3.setForeground(Color.WHITE);
btnNewButton_3.setIcon(new ImageIcon("images/3.png"));
btnNewButton_3.setHorizontalAlignment(SwingConstants.RIGHT);
panel_3.add(btnNewButton_3);
JButton btnNewButton_4 = new JButton("");
btnNewButton_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleNumber("4");
}
});
btnNewButton_4.setBackground(Color.WHITE);
btnNewButton_4.setForeground(Color.WHITE);
btnNewButton_4.setIcon(new ImageIcon("images/4.png"));
panel_3.add(btnNewButton_4);
JButton btnNewButton_5 = new JButton("");
btnNewButton_5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleNumber("5");
}
});
btnNewButton_5.setBackground(Color.WHITE);
btnNewButton_5.setForeground(Color.WHITE);
btnNewButton_5.setIcon(new ImageIcon("images/5.png"));
panel_3.add(btnNewButton_5);
JButton btnNewButton_6 = new JButton("");
btnNewButton_6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleNumber("6");
}
});
btnNewButton_6.setBackground(Color.WHITE);
btnNewButton_6.setForeground(Color.WHITE);
btnNewButton_6.setIcon(new ImageIcon("images/6.png"));
panel_3.add(btnNewButton_6);
JButton btnNewButton_7 = new JButton("");
btnNewButton_7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleNumber("7");
}
});
btnNewButton_7.setBackground(Color.WHITE);
btnNewButton_7.setForeground(Color.WHITE);
btnNewButton_7.setIcon(new ImageIcon("images/7.png"));
panel_3.add(btnNewButton_7);
JButton btnNewButton_8 = new JButton("");
btnNewButton_8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleNumber("8");
}
});
btnNewButton_8.setBackground(Color.WHITE);
btnNewButton_8.setForeground(Color.WHITE);
btnNewButton_8.setIcon(new ImageIcon("images/8.png"));
panel_3.add(btnNewButton_8);
JButton btnNewButton_9 = new JButton("");
btnNewButton_9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleNumber("9");
}
});
btnNewButton_9.setBackground(Color.WHITE);
btnNewButton_9.setForeground(Color.WHITE);
btnNewButton_9.setIcon(new ImageIcon("images/9.png"));
panel_3.add(btnNewButton_9);
JButton btnNewButton_xing = new JButton("");
btnNewButton_xing.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleNumber("*");
}
});
btnNewButton_xing.setBackground(Color.WHITE);
btnNewButton_xing.setForeground(Color.WHITE);
btnNewButton_xing.setIcon(new ImageIcon("images/xing.png"));
panel_3.add(btnNewButton_xing);
JButton btnNewButton_0 = new JButton("");
btnNewButton_0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleNumber("0");
}
});
btnNewButton_0.setBackground(Color.WHITE);
btnNewButton_0.setForeground(Color.WHITE);
btnNewButton_0.setIcon(new ImageIcon("images/0.png"));
panel_3.add(btnNewButton_0);
JButton btnNewButton_j= new JButton("");
btnNewButton_j.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleNumber("#");
}
});
btnNewButton_j.setBackground(Color.WHITE);
btnNewButton_j.setForeground(Color.WHITE);
btnNewButton_j.setIcon(new ImageIcon("images/jing.png"));
panel_3.add(btnNewButton_j);
JButton btnNewButton_call = new JButton("");
btnNewButton_call.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btnNewButton_call.setBackground(Color.WHITE);
btnNewButton_call.setForeground(Color.WHITE);
btnNewButton_call.setIcon(new ImageIcon("images/call.png"));
panel_3.add(btnNewButton_call);
JButton button = new JButton("");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
button.setBackground(Color.WHITE);
button.setForeground(Color.WHITE);
button.setIcon(new ImageIcon("images/end.png"));
panel_3.add(button);
JButton btnNewButton_c= new JButton("");
btnNewButton_c.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleNumber("c");
}
});
btnNewButton_c.setIcon(new ImageIcon("images/c.png"));
panel_3.add(btnNewButton_c);
}
class ShowTime extends TimerTask {
public void run() {
// 刷新
frame.repaint();
}
}
class TimePanel extends JPanel {
public void paint(Graphics g) {
super.paint(g);
// 显示时间
g.drawString(sdf.format(new Date()), 10,10);
}
}
}