Java-GUI编程

GUI编程

1. GUI概念

GUI(Graphical User Interface)即图形用户界面 , 用图形的方式,来显示计算机操作的界面,这样更方便更直观 较于在Java之前的篇章中,脱离了控制台,能够进行用户交互,使程序看上去更加友好

相关包:

  • java.awt : 需要调用本地系统方法实现功能,属于重量级组件
  • java.swing : 在AWT的基础上,建立的一套图形界面系统,其中提供了更多的组件,而且完全由Java实现。增强了移植性,属轻量级组件

GUI继承体系

在这里插入图片描述

2. 容器组件

概述

Java的图形用户界面的基本组成部分是组件(Component),组件是一个以图形化的方式 显示在屏幕上并能与用户进行交互的对象

组件不能独立地显示出来,必须将组件放在一定的容器(container)中才 可以显示出来。 容器可以容纳多个组件,通过调用容器的add(Component comp)方法 向容器中添加组件

Frame和Panel是最常用的容器

常用容器

JFrame

JFrame是Java中的一个类,它是一个容器,允许程序员把其他组件(Swing 的三个基本构造块:标签、按钮和文本字段)添加到它里面,把它们组织起来,并把它们呈现给用户。

JFrame的构造方法

构造方法 说明
JFrame() 构造一个无标题的不可见的新窗口
JFrame(String title) 构造一个标题为 title 的不可见的新窗口

JFrame的常用方法

方法 说明
void remove(Component comp) 从该容器中移除指定组件
void setDefaultCloseOperation(int operation) 设置用户在此窗体上发起 “close” 时默认执行的操作
void setSize(int width, int height) 设置窗口大小
void setLocation(int x,int y) 设置窗口的位置
void setLocationRelativeTo(Component c) 设置窗口相对参数组件的位置,为null居于屏幕中央
void setVisible(boolean b) 设置窗口是否可见,默认不可见
void setResizable(boolean b) 设置窗口是否可调整大小,默认可调整
void setBounds(int x, int y, int width, int height) 设置左上角坐标及窗口大小
void setTitle(String title) 设置窗口标题
void setIconImage(Image image) 设置窗口标题图片
void dispose( 释放窗
package java_gui.framedemo1;
import javax.swing.*;
public class FrameDemo1 extends JFrame {
   
    public static void main(String[] args) {
   

        FrameDemo1 frameDemo1 = new FrameDemo1();
        frameDemo1.setFrame();

    }

    //设置窗口
    public void setFrame(){
   
        /*
            窗体基本特征
         */
        this.setSize(400,200);
        this.setTitle("窗口标题");
        this.setResizable(false);//设置窗口是否可拖拽
        //this.setLocation(400, 100);//
        this.setIconImage(new ImageIcon("p5.jpg").getImage());//这是标题图片
        this.setLocationRelativeTo(null);//设置窗口居中
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        //this.setBounds(100, 100, 400, 400);
        this.setVisible(true);//设置窗体可见,放在所有设置下方
    }
}

运行结果:

在这里插入图片描述

JPanel

JPanel提供面板,是轻量级的容器

面板中可以添加其他组件,也可以设置布局,我们一般使用面板来实现布局嵌套

JPanel的构造方法

构造方法 说明
JPanel() 创建一个新面板
JPanel(LayoutManager layout) 创建一个指定布局的面板
package java_gui.framedemo1;

import javax.swing.*;
import java.awt.*;

public class FrameDemo2 extends JFrame{
   

    public static void main(String[] args) {
   

        FrameDemo2 frameDemo2 = new FrameDemo2();
        frameDemo2.setFrame();

    }

    //设置窗口
    public void setFrame(){
   
        /*
            窗体基本特征
         */
        this.setSize(400,200);
        this.setTitle("窗口标题");
        this.setResizable(false);//设置窗口是否可拖拽
        //this.setLocation(400, 200);//
        this.setIconImage(new ImageIcon("p5.jpg").getImage());//这是标题图片
        this.setLocationRelativeTo(null);//设置窗口居中
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //this.setBounds(100, 100, 400, 400);
        //将面板加到窗口中
        this.add(setPanel(new JPanel()));//this表示窗口

        this.setVisible(true);//设置窗体可见,放在所有设置下方
    }

    //设置面板
    public JPanel setPanel(JPanel jPanel){
   

         /*
            创建面板
         */
        jPanel = new JPanel();
        jPanel.setBackground(Color.green);//设置背景色
        
        return jPanel;//返回创建的面板
    }
}

运行结果:

在这里插入图片描述

3. 常用组件

JButton(按钮)

构造方法

构造方法 说明
JButton() 创建一个按钮
JButton(String text) 创建一个带文本的按钮
JButton(Icon icon) 创建一个带图标的按钮
JButton(String text, Icon icon) 创建一个带文本和图标的按钮
JButton jButton1=new JButton("确定");
JButton jButton2=new JButton("取消");
jPanel.add(jButton1);
jPanel.add(jButton2);

在这里插入图片描述

方法:

方法 说明
void setText(String text) 设置按钮上的文本
String getText() 获得按钮上的文本
void setBackground(Color bg) 设置按钮的背景色
Color getBackground() 获得按钮的背景色
void setEnabled(boolean b) 设置是否启用按钮
void setVisible(boolean b) 设置按钮是否为可见
void setToolTipText(String text) 设置按钮的悬停提示信息
//按钮背景色
jButton1.setBackground(Color.GRAY);
//悬停提示
jButton2.setToolTipText("此按钮已禁用");
//按钮禁用
jButton2.setEnabled
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
javaGUI图形界面 public class login extends JFrame { private JComboBox nameJComboBox; private JPanel userJPanel; private JLabel pictureJLabel; private JButton okJButton,cancelJButton; private JLabel nameJLabel,passwordJLabel,note; private JPasswordField passwordJPasswordField; private String name1; private String password1; private String user; private ImageIcon myImageIcon; public login( ) { createUserInterface(); // 调用创建用户界面方法 } private void createUserInterface() { Container contentPane = getContentPane(); contentPane.setLayout( null ); userJPanel = new JPanel(); userJPanel.setBounds( 35, 120, 300, 96 ); userJPanel.setBorder(BorderFactory.createEtchedBorder() ); //显示一圈边儿 userJPanel.setLayout( null ); contentPane.add( userJPanel ); nameJComboBox = new JComboBox(); nameJComboBox.setBounds( 100, 12, 170, 25 ); nameJComboBox.addItem( "admin" ); nameJComboBox.addItem( "aloie" ); nameJComboBox.setSelectedIndex( 0 ); nameJComboBox.setEditable(true); userJPanel.add( nameJComboBox ); pictureJLabel=new JLabel(); pictureJLabel.setBounds(45,0,380,118); pictureJLabel.setIcon(new ImageIcon("pic.gif")); contentPane.add(pictureJLabel); nameJLabel=new JLabel("姓 名:"); nameJLabel.setBounds(20,12,80,25); userJPanel.add(nameJLabel); passwordJPasswordField=new JPasswordField(); passwordJPasswordField.setBounds(100,60,170,25); userJPanel.add(passwordJPasswordField); passwordJLabel=new JLabel("密 码:"); passwordJLabel.setBounds(20,60,80,25); userJPanel.add(passwordJLabel); note=new JLabel("密码与用户名相同"); note.setBounds(0,295,180,25); add(note); okJButton=new JButton("登 陆"); okJButton.setBounds(60,250,80,25); contentPane.add(okJButton); okJButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { okJButtonActionPerformed(event); } } ); cancelJButton=new JButton("取 消"); cancelJButton.setBounds(210,250,80,25); contentPane.add(cancelJButton); cancelJButton.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent event ) { System.exit(0); //退出登陆 } } ); setTitle( "登陆窗口" ); setSize( 380, 350 ); setResizable( false ); //将最大化按钮设置为不可用 } private void okJButtonActionPerformed( ActionEvent event ) { //okJButton响应事件,检查用户名和密码的匹配 name1= nameJComboBox.getSelectedItem().toString(); if (name1.equals("admin") ) { if (passwordJPasswordField.getText().equals("admin")) { showNewWindow(); setVisible( false); } else { JOptionPane.showMessageDialog( this,"密码错误,拒绝登陆", "密码错误 !", JOptionPane.ERROR_MESSAGE ); } } else if (name1.equals("aloie")) { if ( passwordJPasswordField.getText().equals("aloie") ) { showNewWindow(); setVisible(false); } else { JOptionPane.showMessageDialog( this,"密码错误,拒绝登陆", "密码错误 !", JOptionPane.ERROR_MESSAGE ); } } } public void showNewWindow() { JFrame jf=new JFrame("main Frame"); jf.setSize(500,400); jf.setVisible(true); jf.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } public static void main( String[] args ) { JFrame.setDefaultLookAndFeelDecorated(true); login mylogin = new login( ); mylogin.setVisible( true ); mylogin.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } }
对于Java GUI编程实现计算器,你可以使用Swing和AWT这两种Java GUI工具包之一。 以下是一个简单的Java GUI计算器的实现示例: ```java import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Calculator extends JFrame implements ActionListener { private JButton[] numberButtons; private JButton[] operatorButtons; private JButton clearButton, equalsButton, decimalButton; private JTextField textField; private double num1 = 0, num2 = 0, result = 0; private char operator; public Calculator() { setTitle("Calculator"); // Create the number buttons numberButtons = new JButton[10]; for (int i = 0; i < 10; i++) { numberButtons[i] = new JButton(String.valueOf(i)); } // Create the operator buttons operatorButtons = new JButton[4]; operatorButtons[0] = new JButton("+"); operatorButtons[1] = new JButton("-"); operatorButtons[2] = new JButton("*"); operatorButtons[3] = new JButton("/"); // Create the clear, equals, and decimal buttons clearButton = new JButton("C"); equalsButton = new JButton("="); decimalButton = new JButton("."); // Create the text field textField = new JTextField(10); textField.setEditable(false); // Set the layout manager and add the components setLayout(new GridBagLayout()); GridBagConstraints gc = new GridBagConstraints(); gc.gridx = 0; gc.gridy = 0; gc.gridwidth = 4; add(textField, gc); gc.gridwidth = 1; gc.gridy = 1; for (int i = 0; i < 10; i++) { gc.gridx = i % 3; gc.gridy = i / 3 + 2; add(numberButtons[i], gc); numberButtons[i].addActionListener(this); } gc.gridx = 0; gc.gridy = 5; add(operatorButtons[0], gc); operatorButtons[0].addActionListener(this); gc.gridx = 1; add(operatorButtons[1], gc); operatorButtons[1].addActionListener(this); gc.gridx = 2; add(operatorButtons[2], gc); operatorButtons[2].addActionListener(this); gc.gridx = 3; add(operatorButtons[3], gc); operatorButtons[3].addActionListener(this); gc.gridx = 0; gc.gridy = 6; add(clearButton, gc); clearButton.addActionListener(this); gc.gridx = 1; add(equalsButton, gc); equalsButton.addActionListener(this); gc.gridx = 2; add(decimalButton, gc); decimalButton.addActionListener(this); setSize(200, 200); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } public void actionPerformed(ActionEvent e) { for (int i = 0; i < 10; i++) { if (e.getSource() == numberButtons[i]) { textField.setText(textField.getText() + i); } } if (e.getSource() == decimalButton) { textField.setText(textField.getText() + "."); } if (e.getSource() == clearButton) { textField.setText(""); num1 = 0; num2 = 0; result = 0; operator = ' '; } if (e.getSource() == operatorButtons[0]) { num1 = Double.parseDouble(textField.getText()); operator = '+'; textField.setText(""); } if (e.getSource() == operatorButtons[1]) { num1 = Double.parseDouble(textField.getText()); operator = '-'; textField.setText(""); } if (e.getSource() == operatorButtons[2]) { num1 = Double.parseDouble(textField.getText()); operator = '*'; textField.setText(""); } if (e.getSource() == operatorButtons[3]) { num1 = Double.parseDouble(textField.getText()); operator = '/'; textField.setText(""); } if (e.getSource() == equalsButton) { num2 = Double.parseDouble(textField.getText()); switch (operator) { case '+': result = num1 + num2; break; case '-': result = num1 - num2; break; case '*': result = num1 * num2; break; case '/': result = num1 / num2; break; } textField.setText(Double.toString(result)); } } public static void main(String[] args) { new Calculator(); } } ``` 这个示例应该可以实现一个简单的Java GUI计算器。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值