Java——GUI(图形用户界面)-浅记

本文详细介绍了如何在Java中使用JFrame创建窗口,包括设置窗口的基本属性、添加各种Swing组件(如文本框、按钮等)、以及常用的布局管理器如FlowLayout、BorderLayout、CardLayout和GridLayout的使用方法。同时涵盖了事件处理的相关内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

JFrame

1、创建窗口

2、基本组件

3、常用布局

1、FlowLayout布局

2、BorderLayout布局(默认)

3、CardLayout布局

4、GridLayout布局

5、null布局

6、布局嵌套

4、处理事件


JFrame

一定要先继承这个类

1、创建窗口

//构造一个窗体
//      JFrame frame=new JFrame("123");
        //设置标题
        this.setTitle("学生在线");
        //窗体的初始位置
//      this.setLocationRelativeTo(null);居中显示有问题,会显示在右下角
//      this.setLocation(500,300 );//固定值我的是居中,到别的电脑不一定居中
        //居中显示
        Toolkit t=Toolkit.getDefaultToolkit();//获取屏幕分辨率
        Dimension s=t.getScreenSize();//获取屏幕的宽高
        this.setLocation((s.width-width)/2, (s.height-height)/2);
        
        //窗体的大小
        this.setSize(width,height);
        //窗体是否可见
        this.setVisible(true);
        //窗口不可变
        this.setResizable(false);
        //当窗口关闭时结束程序
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

2、基本组件

1、文本框: jcomponent的子类jtextfield创建
        函数:JTextFiled(名字);
        方法:public string getText(int num);
             public void setText(string t);
​
2、文本区:jcomponent的子类jtextArea创建
        函数:JTextArea(名字);
        方法:public string getText(int a);
             public void setText(string t);
             
3、按钮:jcomponent的子类jtext创建
        函数:JButton();Jbutton( string text)
        方法:public void addActionListener(ActionListener  I)
        
4、标签:JLable
        函数:Jlble(名字);
        方法:public string getText(int a);
             public void setText(string t);
5、选择框:JCheckBox
        函数:JCheckBox();
        方法:public void addltemListener( ltem.. i)
             public void addActionListener(Ac.. i)
            litemListener接口中:getltemSelectable()相当于getSource()
                                getStateChange();若被选中selected
            
6、单选按钮:JRadioButton
        函数:JRadioButton()
        方法:addltemListener( ltem.. i)
        
7、下拉列表:JcomboBox
        函数:JcomboBox()
        方法:getSelectedIndex();
​
​
8、密码框:JPasswordField
        函数:JPasswordField()
        方法:getText()
             setText()
             setEchoChar(char c)使用该方法重新设置回显字符
             getpassword()方法返回实际的密码

3、常用布局

1、FlowLayout布局

1、创建布局对象(顺序布局)

FlowLayout flow=new FlowLayout();

2、容器frame使用布局对象

frame.setLayout(flow)

3、con可以使用contaniner类提供的add方法将组件顺序添加到容器

2、BorderLayout布局(默认)

创建布局(五大块)

BorderLayout b=new BorderLayout();

使用

与flow布局差不多
但它可以将组件指定位置添加
​
frame.add(b,BorderLayout.CENTER)

3、CardLayout布局

1、创建对象

CardLayout  card=new CardLayout()

2、为容器设置布局

frame.setLayout(card)

3、容器调用add将组件b加入容器

add(string name,Component b);

4、布局对象card用CardLayout提供的show()方法,显示name组件

card.show(frame,name)

4、GridLayout布局

1、创建布局对象

GridLayout grid=new GridLayout(10,8);

2、使用GridLayout布局将组件添加到容器

5、null布局

6、布局嵌套

4、处理事件

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 ); } }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

arjunna

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值