GUI
GUI简介:图形界面编程
GUI核心:Swing 、 AWT
GUI淘汰因素:界面不美观,需要依赖jre环境
AWT
- AWT介绍
java有 java.awt包 使用Frame类创建不同窗口//创建Frame对象 Frame frame = new Frame("first frame"); //设置窗口参数-可见性-位置大小-背景颜色... frame.setVisible(true); //frame.setLocation(200,200); //frame.setSize(400,400); frame.setBounds(200,200,400,400); frame.setBackground(Color.BLACK); //监听关闭窗口结束程序 frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { super.windowClosing(e); System.exit(0); } });
Panel面板
- panel面板
可以看成一个空间,但是不能单独存在//创建panel面板 Panel panel = new Panel(); //面板位置是相对frame中的位置 panel.setBounds(100,20,200,200); panel.setBackground(Color.PINK); //创建Frame对象 Frame frame = new Frame("first frame"); //设置窗口参数-可见性-位置大小-背景颜色... frame.setVisible(true); //frame.setLocation(200,200); //frame.setSize(400,400); frame.setBounds(200,200,400,400); frame.setBackground(Color.gray); //设置布局 frame.setLayout(null); //frame添加面板panel frame.add(panel); //监听关闭窗口结束程序 frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { super.windowClosing(e); System.exit(0); } });
布局管理器
-
流式布局
-
东南西北中
-
表格布局
-
嵌套布局
//面板嵌套 Panel p1 = new Panel(new BorderLayout()); Panel p2 = new Panel(new GridLayout(2,1)); p1.add(p2,BorderLayout.CENTER); //窗口可有多个面板 Frame frame = new Frame("主窗口"); frame.setLayout(new GridLayout(2,1)); frame.add(p1); frame.add(p3);
事件监听
###按钮监听
public class TestActionEvent {
public static void main(String[] args) {
Frame frame = new Frame("主窗口");
frame.setBounds(200,200,400,200);
frame.setBackground(Color.GRAY);
frame.setVisible(true);
Button one = new Button("one");
one.setActionCommand("点击了one按钮");
one.addActionListener(new MyAction());
frame.add(one,BorderLayout.NORTH);
//监听关闭窗口结束程序
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
System.exit(0);
}
});
}
}
//事件监听类
class MyAction implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(e.getActionCommand());
System.out.println(e.getWhen());
System.out.println(e.paramString());
System.out.println(e.getModifiers());
}
}
输入框监听
public class TestText01 {
public static void main(String[] args) {
new MyFrame();
}
}
class MyFrame extends Frame{
public MyFrame() throws HeadlessException {
TextField textField = new TextField();
//设置替换编码
textField.setEchoChar('*');
//添加一个输入框
add(textField,BorderLayout.NORTH);
setBounds(200,200,400,400);
//监听输入框
textField.addActionListener(new MyAction());
setVisible(true);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
System.exit(0);
}
});
}
}
class MyAction implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//获取输入内容
TextField field = (TextField) e.getSource();
System.out.println(field.getText());
field.setText("");//回车清空输入
}
}
画笔paint
继承Frame重写paint方法
鼠标监听
鼠标点击画点
public class TestMouseListener {
public static void main(String[] args) {
new MyFrame("鼠标画点");
}
}
class MyFrame extends Frame {
ArrayList<Point> points = new ArrayList<>();
public MyFrame(String name) {
super(name);
setVisible(true);
setBounds(400, 400, 600, 600);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
//添加鼠标监听
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
Frame frame = (Frame) e.getSource();
points.add(new Point(e.getX(), e.getY()));
frame.repaint();
}
});
}
@Override
public void paint(Graphics g) {
g.setColor(Color.PINK);//设置画笔颜色
//绘画
for (Point p : points) {
g.fill3DRect(p.x, p.y, 10, 10, true);
}
}
}
键盘监听
addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
super.keyTyped(e);
}
//键盘按下
@Override
public void keyPressed(KeyEvent e) {
//监听处理
super.keyPressed(e);
}
@Override
public void keyReleased(KeyEvent e) {
super.keyReleased(e);
}
});
Swing
Swing也分为窗口(JFrame)和面板(JPanel)。
容器(ContentPane)、标签(JLabel)、按钮(JButton)、弹窗(JDialog)、下拉框(JComboBox)、列表(JList)、文本框(JTextField)
JFrame
JFrame jFrame = new JFrame("JFrame窗口");
jFrame.setBounds(100,100,400,200);
jFrame.setVisible(true);
//设置容器背景颜色
Container contentPane = jFrame.getContentPane();
contentPane.setBackground(Color.PINK);
//设置文字
JLabel label = new JLabel("你好啊!");
jFrame.add(label);
label.setHorizontalAlignment(SwingConstants.CENTER);
//关闭窗口
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
弹窗
public class TestDialog {
public static void main(String[] args) {
new MyJFrame();
}
}
class MyJFrame extends JFrame{
public MyJFrame() {
setVisible(true);
setBounds(100,100,400,200);
setLayout(null);
//关闭窗口
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//容器
Container contentPane = this.getContentPane();
contentPane.setBackground(Color.PINK);
JButton button = new JButton("弹窗按钮");
button.setBounds(30,30,200,100);
//按钮监听
button.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
new MyDialog();
}
});
contentPane.add(button);
}
}
class MyDialog extends JDialog{
public MyDialog() {
//弹窗内容
setVisible(true);
setBounds(200,200,400,200);
Container contentPane = this.getContentPane();
contentPane.setBackground(Color.cyan);
contentPane.add(new JLabel("你好!"));
}
}
标签icon、图片
public class TestLabel extends JFrame implements Icon {
private int width;
private int height;
public TestLabel(){}
public TestLabel(int width, int height){
this.width = width;
this.height = height;
}
public void init(){
TestLabel icon = new TestLabel(20, 20);
//icon放入label,也可以放在按钮上
JLabel icon1 = new JLabel("icon", icon, SwingConstants.CENTER);
//图片放入label
JLabel imageIcon = new JLabel("imageIcon");
ImageIcon imageIcon1 = new ImageIcon("src\\image.jpg");
imageIcon.setIcon(imageIcon1);
imageIcon.setHorizontalAlignment(SwingConstants.CENTER);
//label放入容器
Container contentPane = this.getContentPane();
contentPane.add(icon1);
contentPane.add(imageIcon);
contentPane.setLayout(new GridLayout(2,1));
setVisible(true);
setBounds(200,200,400,400);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
//实现Icon接口画icon
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
g.setColor(Color.PINK);
g.fill3DRect(x,y,width,height,true);
}
@Override
public int getIconWidth() {
return width;
}
@Override
public int getIconHeight() {
return height;
}
public static void main(String[] args) {
new TestLabel().init();
}
}
JPanel面板、Scroll面板-带滑动条
public class TestJPanel extends JFrame {
public TestJPanel() {
//获取窗口容器
Container contentPane = this.getContentPane();
contentPane.setLayout(new GridLayout(2,1,10,10));
//JPanel面板
JPanel jPanel = new JPanel(new GridLayout(1, 3));
jPanel.add(new JButton("1"));
jPanel.add(new JButton("2"));
jPanel.add(new JButton("3"));
//Scroll面板---带滑动条
JTextArea jTextArea = new JTextArea(20, 50);
jTextArea.setText("sssssssssssss默认文本qqqqqqqqqqqqqq");
JScrollPane jScrollPane = new JScrollPane(jTextArea);
contentPane.add(jPanel);
contentPane.add(jScrollPane);
setVisible(true);
setBounds(200,200,800,500);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
按钮(JButton)、单选(JRadioButton)、多选(JCheckBox)
//按钮
JButton button = new JButton("按钮");
button.setToolTipText("提示文字");
contentPane.add(button);
//单选框
//同一个分组只能勾选一个,如果不在同一个分组则可以同时选中
ButtonGroup buttonGroup = new ButtonGroup();
JRadioButton jRadioButton1 = new JRadioButton("单选1");
JRadioButton jRadioButton2 = new JRadioButton("单选2");
JRadioButton jRadioButton3 = new JRadioButton("单选3");
buttonGroup.add(jRadioButton1);
buttonGroup.add(jRadioButton2);
buttonGroup.add(jRadioButton3);
jPanel.add(jRadioButton1);
jPanel.add(jRadioButton2);
jPanel.add(jRadioButton3);
//多选
for (int i = 1; i <= 3; i++) {
JCheckBox jCheckBox = new JCheckBox("多选" + i);
jPanel.add(jCheckBox);
}
下拉框(JComboBox)、列表框(JList)
//下拉框
JComboBox jComboBox = new JComboBox();
jComboBox.addItem(null);
jComboBox.addItem("选项1");
jComboBox.addItem("选项2");
jComboBox.addItem("选项3");
jPanel.add(jComboBox);
//列表框
String[] x = {"选择1--","选择2--","选择3--","选择4--"};
JList jList = new JList(x);
jPanel.add(jList);
文本输入框(JTextField)、密码输入框(JPasswordField)
//文本输入框
JTextField jTextField = new JTextField("hello");
JTextField jTextField2 = new JTextField("word");
contentPane.add(jTextField);
contentPane.add(jTextField2);
//密码输入框
JPasswordField jPasswordField = new JPasswordField();
jPasswordField.setText("*");
contentPane.add(jPasswordField);