首页 > 代码库 > GUI
GUI
GUI的概念
GUI(Graphical User Interface)即图形用户界面,它能够使应用程序看上去更加友好;
AWT、Swing以及SWT
AWT(Abstract Windows Toolkit)是Java语言中最原始的GUI工具包,相关API位于java.awt包中。AWT是一个非常有限的GUI工具包,比如树、表格等都不支持。
原Sun公司希望Java 语言能够成为一种“一次编写,处处运行”的环境。意思是可以在一台机器上开发和测试Java代码(例如在 Windows平台),然后不经测试就可以在其他平台(如Linux平台)运行。对于大部分情况来说,Java 技术都可以成功实现这种特点,
然而AWT却无法实现。AWT运行时,每个组件都要依赖于当前平台的GUI对等体(peer)控件,因此,AWT GUI 的外观和行为就会依赖当前平台。
java.awt包是Java内置的包,属于Java基础类库(JFC)的一部分,其中包括以下内容:
便于用户输入的一组丰富的界面组件;
将组件放置在适当位置的几种布局管理器;
事件处理模型;
图形和图像工具等等。
要使用到该包中的类,则必须显式地声明如下语句:
import java.awt.*;
基本Swing组件
JFrame(窗体,框架)
JPanel(面板,容器)
JButton(按钮)
JLabel(标签)
JTextField(文本框)
JTextArea(文本域)
GUI 编写的简单计算器
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JOptionPane;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;
import javax.swing.JTextField;
import java.awt.Font;
public class Jsq extends JFrame {
private JPanel contentPane;
private JTextField textField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Jsq frame = new Jsq();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Jsq() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 464, 320);
contentPane = new JPanel();
contentPane.setForeground(new Color(152, 251, 152));
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
JButton btnNewButton = new JButton("1");
btnNewButton.setForeground(Color.MAGENTA);
btnNewButton.setFont(new Font("宋体", Font.PLAIN, 16));
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+"1");
}
});
JButton button = new JButton("4");
button.setForeground(Color.MAGENTA);
button.setFont(new Font("宋体", Font.PLAIN, 16));
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+"4");
}
});
JButton button_1 = new JButton("7");
button_1.setForeground(Color.MAGENTA);
button_1.setFont(new Font("宋体", Font.PLAIN, 16));
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textField.setText(textField.getText()+"7");
}
});
JButton button_2 = new JButton("2");
button_2.setForeground(Color.MAGENTA);
button_2.setFont(new Font("宋体", Font.PLAIN, 16));
button_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+"2");
}
});
JButton button_3 = new JButton("5");
button_3.setForeground(Color.MAGENTA);
button_3.setFont(new Font("宋体", Font.PLAIN, 16));
button_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "alert", "alert", JOptionPane.ERROR_MESSAGE);
textField.setText(textField.getText()+"5");
}
});
JButton button_4 = new JButton("8");
button_4.setForeground(Color.MAGENTA);
button_4.setFont(new Font("宋体", Font.PLAIN, 16));
button_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+"8");
}
});
JButton button_5 = new JButton("3");
button_5.setForeground(Color.MAGENTA);
button_5.setFont(new Font("宋体", Font.PLAIN, 16));
button_5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+"3");
}
});
JButton button_6 = new JButton("6");
button_6.setForeground(Color.MAGENTA);
button_6.setFont(new Font("宋体", Font.PLAIN, 16));
button_6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+"6");
}
});
JButton button_8 = new JButton("9");
button_8.setForeground(Color.MAGENTA);
button_8.setFont(new Font("宋体", Font.PLAIN, 16));
button_8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+"9");
}
});
JButton btnNewButton_1 = new JButton("0");
btnNewButton_1.setFont(new Font("宋体", Font.PLAIN, 16));
btnNewButton_1.setForeground(Color.MAGENTA);
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textField.setText(textField.getText()+"0");
}
});
JButton btnNewButton_2 = new JButton(".");
btnNewButton_2.setFont(new Font("宋体", Font.PLAIN, 16));
btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textField.setText(textField.getText()+".");
}
});
btnNewButton_2.setForeground(Color.MAGENTA);
JButton button_11 = new JButton("=");
button_11.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// textField.setText(textField.getText()+"=");
//获取文本框的内容
String str =textField.getText();
byte [] b= new byte [str.length()];
b=str.getBytes();
int endIndex=str.length();
for(int i=0;i<str.length();i++ ){
if(b[i]==‘+‘){
int index1=str.indexOf("+");
String stra = str.substring(0,index1);
double c=Double.parseDouble(stra);
//获取第二个数字
String strb=str.substring(index1+1,endIndex);
double x=Double.parseDouble(strb);
//加法计算
double jia= c+x;
textField.setText(Double.toString(jia));
}
if(b[i]==‘-‘){
int index1=str.indexOf("-");
String stra = str.substring(0,index1);
double c=Double.parseDouble(stra);
//获取第二个数字
String strb=str.substring(index1+1,endIndex);
double x=Double.parseDouble(strb);
//减法计算
double jia= c-x;
textField.setText(Double.toString(jia));
}
if(b[i]==‘*‘){
int index1=str.indexOf("*");
String stra = str.substring(0,index1);
double c=Double.parseDouble(stra);
//获取第二个数字
String strb=str.substring(index1+1,endIndex);
double x=Double.parseDouble(strb);
//乘法计算
double jia= c*x;
textField.setText(Double.toString(jia));
}
if(b[i]==‘/‘){
int index1=str.indexOf("/");
String stra = str.substring(0,index1);
double c=Double.parseDouble(stra);
//获取第二个数字
String strb=str.substring(index1+1,endIndex);
double x=Double.parseDouble(strb);
//除法计算
double jia= c/x;
textField.setText(Double.toString(jia));
}
}
}
});
button_11.setForeground(Color.BLACK);
textField = new JTextField();
textField.setColumns(10);
JButton button_7 = new JButton("+");
button_7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+"+");
}
});
button_7.setForeground(Color.BLACK);
JButton button_9 = new JButton("-");
button_9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+"-");
}
});
button_9.setForeground(Color.BLACK);
JButton button_10 = new JButton("*");
button_10.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+"*");
}
});
button_10.setForeground(Color.BLACK);
JButton button_12 = new JButton("/");
button_12.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+"/");
}
});
button_12.setForeground(Color.BLACK);
JButton btnMc = new JButton("CE");
btnMc.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(" ");
}
});
btnMc.setForeground(Color.BLACK);
JButton btnC = new JButton("C");
btnC.setForeground(Color.BLACK);
JButton button_15 = new JButton("\u2190");
button_15.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(" ");
}
});
button_15.setForeground(Color.BLACK);
JButton button_16 = new JButton("%");
button_16.setForeground(Color.BLACK);
GroupLayout gl_contentPane = new GroupLayout(contentPane);
gl_contentPane.setHorizontalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup()
.addContainerGap()
.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
.addComponent(textField, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 417, Short.MAX_VALUE)
.addGroup(Alignment.LEADING, gl_contentPane.createSequentialGroup()
.addComponent(btnNewButton_1, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addPreferredGap(ComponentPlacement.RELATED)
.addGap(18)
.addComponent(btnNewButton_2, GroupLayout.PREFERRED_SIZE, 93, GroupLayout.PREFERRED_SIZE)
.addGap(18)
.addComponent(button_11, GroupLayout.PREFERRED_SIZE, 69, GroupLayout.PREFERRED_SIZE))
.addGroup(Alignment.LEADING, gl_contentPane.createSequentialGroup()
.addComponent(btnNewButton, GroupLayout.PREFERRED_SIZE, 69, GroupLayout.PREFERRED_SIZE)
.addGap(18)
.addComponent(button_2, GroupLayout.PREFERRED_SIZE, 69, GroupLayout.PREFERRED_SIZE)
.addGap(18)
.addComponent(button_5, GroupLayout.PREFERRED_SIZE, 69, GroupLayout.PREFERRED_SIZE)
.addGap(18)
.addComponent(button_7, GroupLayout.PREFERRED_SIZE, 69, GroupLayout.PREFERRED_SIZE)
.addGap(18)
.addComponent(btnMc, GroupLayout.PREFERRED_SIZE, 69, GroupLayout.PREFERRED_SIZE))
.addGroup(Alignment.LEADING, gl_contentPane.createSequentialGroup()
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
.addGroup(gl_contentPane.createSequentialGroup()
.addComponent(button, GroupLayout.PREFERRED_SIZE, 69, GroupLayout.PREFERRED_SIZE)
.addGap(18)
.addComponent(button_3, GroupLayout.PREFERRED_SIZE, 69, GroupLayout.PREFERRED_SIZE)
.addGap(18)
.addComponent(button_6, GroupLayout.PREFERRED_SIZE, 69, GroupLayout.PREFERRED_SIZE))
.addGroup(gl_contentPane.createSequentialGroup()
.addComponent(button_1, GroupLayout.PREFERRED_SIZE, 69, GroupLayout.PREFERRED_SIZE)
.addGap(18)
.addComponent(button_4, GroupLayout.PREFERRED_SIZE, 69, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(button_8, GroupLayout.PREFERRED_SIZE, 69, GroupLayout.PREFERRED_SIZE)))
.addGap(18)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addComponent(button_9, GroupLayout.PREFERRED_SIZE, 69, GroupLayout.PREFERRED_SIZE)
.addGap(18)
.addComponent(btnC, GroupLayout.PREFERRED_SIZE, 69, GroupLayout.PREFERRED_SIZE))
.addGroup(gl_contentPane.createSequentialGroup()
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addComponent(button_10, GroupLayout.PREFERRED_SIZE, 69, GroupLayout.PREFERRED_SIZE)
.addComponent(button_12, GroupLayout.PREFERRED_SIZE, 69, GroupLayout.PREFERRED_SIZE))
.addGap(18)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addComponent(button_16, GroupLayout.PREFERRED_SIZE, 69, GroupLayout.PREFERRED_SIZE)
.addComponent(button_15, GroupLayout.PREFERRED_SIZE, 69, GroupLayout.PREFERRED_SIZE))))))
.addGap(211))
);
gl_contentPane.setVerticalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addComponent(textField, GroupLayout.PREFERRED_SIZE, 33, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(btnNewButton, GroupLayout.PREFERRED_SIZE, 45, GroupLayout.PREFERRED_SIZE)
.addComponent(button_2, GroupLayout.PREFERRED_SIZE, 45, GroupLayout.PREFERRED_SIZE)
.addComponent(button_5, GroupLayout.PREFERRED_SIZE, 45, GroupLayout.PREFERRED_SIZE))
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(button_7, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE)
.addComponent(btnMc, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE)))
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(20)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(button, GroupLayout.PREFERRED_SIZE, 45, GroupLayout.PREFERRED_SIZE)
.addComponent(button_3, GroupLayout.PREFERRED_SIZE, 45, GroupLayout.PREFERRED_SIZE)
.addComponent(button_6, GroupLayout.PREFERRED_SIZE, 45, GroupLayout.PREFERRED_SIZE))
.addGap(18)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addComponent(button_1, GroupLayout.PREFERRED_SIZE, 45, GroupLayout.PREFERRED_SIZE)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(button_4, GroupLayout.PREFERRED_SIZE, 45, GroupLayout.PREFERRED_SIZE)
.addComponent(button_8, GroupLayout.PREFERRED_SIZE, 45, GroupLayout.PREFERRED_SIZE))))
.addGroup(gl_contentPane.createSequentialGroup()
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(button_9, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE)
.addComponent(btnC, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(ComponentPlacement.UNRELATED)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(button_10, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE)
.addComponent(button_15, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(ComponentPlacement.RELATED, 7, Short.MAX_VALUE)
.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
.addComponent(button_12, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE)
.addComponent(button_16, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE))))
.addGap(18)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(btnNewButton_2, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE)
.addComponent(btnNewButton_1, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE)
.addComponent(button_11, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE))
.addContainerGap(28, Short.MAX_VALUE))
);
contentPane.setLayout(gl_contentPane);
}
}
GUI