Java图形界面GUI

Java图形界面GUI


 

设置窗体JFrame对象

package com.Aha.Best;

import javax.swing.ImageIcon;

import javax.swing.JFrame;

public class TestJFrame extends JFrame {

    public static void main(String[] args) {

        TestJFrame tjf = new TestJFrame();

        ImageIcon ii = new ImageIcon("Images/01.jpg");

        tjf.setIconImage(ii.getImage());

        tjf.setTitle("窗体测试记录");

        tjf.setSize(600,600);

        tjf.setLocation(300,200);

        tjf.setVisible(true);

        tjf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

}

15.2设置菜单对象

package com.Aha.Best;

import javax.swing.ImageIcon;

import javax.swing.JFrame;

import javax.swing.JMenu;

import javax.swing.JMenuBar;

import javax.swing.JMenuItem;

public class TestJFrame extends JFrame {

    public static void main(String[] args) {

        TestJFrame tjf = new TestJFrame();

        ImageIcon ii = new ImageIcon("Images/01.jpg");

        tjf.setIconImage(ii.getImage());

        tjf.setTitle("窗体测试记录");

        tjf.setSize(600,600);

        tjf.setLocation(300,200);

        //新建菜单栏jmb

        JMenuBar jmb = new JMenuBar();

        //新建子菜单jm1,jm2

        JMenu jm1 = new JMenu();

        jm1.setText("菜单一");

        JMenu jm2 = new JMenu("菜单二");

        //新建目录下的菜单项jmt1,jmt2,jmt3,jmt4

        JMenuItem jmt1 = new JMenuItem();

        jmt1.setText("操作列一");

        JMenuItem jmt2 = new JMenuItem("操作列二");

        JMenuItem jmt3 = new JMenuItem("操作列三");

        JMenuItem jmt4 = new JMenuItem("操作列四");

       

        tjf.add(jmb);

        jmb.add(jm1);

        jmb.add(jm2);

        jm1.add(jmt1);

        jm1.add(jmt2);

        jm2.add(jmt3);

        jm2.add(jmt4);

       

        tjf.setVisible(true);

        tjf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

}

 

package com.Aha.Best;

import javax.swing.ImageIcon;

import javax.swing.JFrame;

import javax.swing.JMenu;

import javax.swing.JMenuBar;

import javax.swing.JMenuItem;

public class TestJFrame extends JFrame {

    public static void main(String[] args) {

        TestJFrame tjf = new TestJFrame();

        ImageIcon ii = new ImageIcon("Images/01.jpg");

        tjf.setIconImage(ii.getImage());

        tjf.setTitle("窗体测试记录");

        tjf.setSize(600,600);

        tjf.setLocation(300,200);

        //新建菜单栏jmb

        JMenuBar jmb = new JMenuBar();

        //新建子菜单jm1,jm2

        JMenu jm1 = new JMenu();

        jm1.setText("菜单一");

        JMenu jm2 = new JMenu("菜单二");

        //新建目录下的菜单jmt1,jmt2

        JMenuItem jmt1 = new JMenuItem();

        jmt1.setText("操作列一");

        JMenuItem jmt2 = new JMenuItem("操作列二");

       

        tjf.add(jmb);

        jmb.add(jm1);

        jm1.add(jm2);

        jm2.add(jmt1);

        jm2.add(jmt2);

       

        tjf.setVisible(true);

        tjf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

}

15.3选项卡

15.3.1JTabbedPane

15.4设置布局layout

15.4.1空布局

15.4.2流式布局

15.4.3网格布局

15.4分割窗格

15.4.1分割窗格JSplitPane

15.4.2滑块JSlider

15.4.3进度条JProgressBar

15.5设置面板JPannel

import java.awt.GridLayout;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JPasswordField;

import javax.swing.JTextField;

public class Tt extends JFrame{

    public static void main(String[] args) {

       // TODO Auto-generated method stub

       Tt t = new Tt();

       t.setTitle("雷海鸣进销存系统管理");

       t.setLocation(400,300);

       t.setSize(300,180);

       t.setLayout(new GridLayout(3,1));

       //

       JPanel jp1 = new JPanel();

       JLabel jl1 = new JLabel("用    户");

       JTextField jt = new JTextField(10);

       jp1.add(jl1);

       jp1.add(jt);

       JPanel jp2 = new JPanel();

       JLabel jl2 = new JLabel("登    陆");

       JPasswordField jp = new JPasswordField(10);

       jp2.add(jl2);

       jp2.add(jp);

       //

       JPanel jp3 = new JPanel();

       JButton jb1 = new JButton("确 定");

       JButton jb2 = new JButton("取 消");

       jp3.add(jb1);

       jp3.add(jb2);

       //

       t.add(jp1);

       t.add(jp2);

       t.add(jp3);

       t.setVisible(true);

       t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

}

15.6设置标签JLable

import java.awt.Color;

import java.awt.GridLayout;

import javax.swing.JFrame;

import javax.swing.JLabel;

public class Yy extends JFrame {

    public static void main(String[] args) {

       // TODO Auto-generated method stub

       Yy y = new Yy();

       y.setTitle("title");

       y.setBackground(Color.blue);

       y.setBounds(400,300,300,200);

       y.setLayout(new GridLayout());

       //

       JLabel jl = new JLabel("标  签");

       JLabel jl1 = new JLabel("标  签1");

       JLabel jl2 = new JLabel("标  签2");

       JLabel jl3 = new JLabel("标  签3");

       //

       y.add(jl);

       y.add(jl1);

       y.add(jl2);

       y.add(jl3);

       y.setVisible(true);

       y.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

}

15.7设置按钮JButton

import javax.swing.JButton;

import javax.swing.JFrame;

public class TestJButton extends JFrame {

    public static void main(String[] args) {

       TestJButton tjb = new TestJButton();

       tjb.setTitle("title");

       tjb.setSize(300,300);

       JButton jb =new JButton("jbutton");

       tjb.add(jb);

       tjb.setVisible(true);

       tjb.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

}

15.8设置控件

15.8.1文本框JTextField

15.8.2多行文本框JTextArea

15.8.3密码框JPasswordField

15.8.4单选按钮JRadioButton

15.8.5复选框JCheckBox

15.8.6列表框JList

15.9练习

顶层容器

菜单

中间容器

基本组件

package com.AhaBest.swing;

import javax.swing.ImageIcon;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

public class TestSwing extends JFrame {

    public static void main(String[] args) {

        // 创建顶层容器窗体

        TestSwing jf = new TestSwing();

        //创建窗体图标

        ImageIcon ii = new ImageIcon("Images/01.jpg");

        //加载窗体图标

        jf.setIconImage(ii.getImage());

        //设置窗体标题

        jf.setTitle("窗体");

        //设置窗体位置

        jf.setLocation(200,200);

        //设置窗体大小

        jf.setSize(800,600);

        //创建中间容器面板

        JPanel jp = new JPanel();

        //创建基本组件按钮并添加到面板

        JButton jb = new JButton();

        jb.setText("按钮组件");

        jp.add(jb);

        //添加面板容器到窗体

        jf.add(jp);

        //设置窗体可见性

        jf.setVisible(true);

        //设置窗体关闭方式

        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

}

 

布局

流式布局FlowLayout

package com.AhaBest.swing;

import java.awt.FlowLayout;

import javax.swing.ImageIcon;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

public class TestSwing extends JFrame {

    public static void main(String[] args) {

        // 创建顶层容器窗体

        TestSwing jf = new TestSwing();

        //创建窗体图标

        ImageIcon ii = new ImageIcon("Images/01.jpg");

        //加载窗体图标

        jf.setIconImage(ii.getImage());

        //设置窗体标题

        jf.setTitle("窗体");

        //设置窗体位置

        jf.setLocation(200,200);

        //设置窗体大小

        jf.setSize(800,600);

        //创建中间容器面板使用流式布局

        JPanel jp = new JPanel(new FlowLayout());

        //创建基本组件按钮并添加到面板

        JButton jb1 = new JButton();

        jb1.setText("按钮组件1");

        JButton jb2 = new JButton("按钮组件2");

        JButton jb3 = new JButton("按钮组件3");

        JButton jb4 = new JButton("按钮组件4");

        JButton jb5 = new JButton("按钮组件5");

        jp.add(jb1);jp.add(jb2);jp.add(jb3);jp.add(jb4);jp.add(jb5);

        //添加面板容器到窗体 jf.add(jp);

        jf.setContentPane(jp);

        //设置窗体可见性

        jf.setVisible(true);

        //设置窗体关闭方式

        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

}

网格布局GridLayout

package com.AhaBest.swing;

import java.awt.GridLayout;

import javax.swing.ImageIcon;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

public class TestSwing extends JFrame {

    public static void main(String[] args) {

        // 创建顶层容器窗体

        TestSwing jf = new TestSwing();

        //创建窗体图标

        ImageIcon ii = new ImageIcon("Images/01.jpg");

        //加载窗体图标

        jf.setIconImage(ii.getImage());

        //设置窗体标题

        jf.setTitle("窗体");

        //设置窗体位置

        jf.setLocation(200,200);

        //设置窗体大小

        jf.setSize(800,600);

        //创建3行3列的网格布局

        GridLayout gy = new GridLayout(3,3);

        //设置水平与竖直间隙

        gy.setHgap(10);gy.setVgap(10);

        //创建中间容器面板

        JPanel jp = new JPanel(gy);

        //创建基本组件按钮并添加到面板

        JButton jb1 = new JButton();

        jb1.setText("按钮组件1");

        JButton jb2 = new JButton("按钮组件2");

        JButton jb3 = new JButton("按钮组件3");

        JButton jb4 = new JButton("按钮组件4");

        JButton jb5 = new JButton("按钮组件5");

        JButton jb6 = new JButton("按钮组件6");

        JButton jb7 = new JButton("按钮组件7");

        JButton jb8 = new JButton("按钮组件8");

    jp.add(jb1);jp.add(jb2);jp.add(jb3);jp.add(jb4);jp.add(jb5);jp.add(jb6);jp.add(jb7);jp.add(jb8);

        //添加面板容器到窗体 jf.add(jp);

        jf.setContentPane(jp);

        //设置窗体可见性

        jf.setVisible(true);

        //设置窗体关闭方式

        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

}

JLabel,标签。标签主要用于展示 文本 或 图片,也可以 同时显示文本和图片。

package com.AhaBest.swing;

import javax.swing.ImageIcon;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.SwingConstants;

public class TestSwing extends JFrame {

    public static void main(String[] args) {

        // 创建顶层容器窗体

        TestSwing jf = new TestSwing();

        //创建窗体图标

        ImageIcon ii = new ImageIcon("Images/01.jpg");

        //加载窗体图标

        jf.setIconImage(ii.getImage());

        //设置窗体标题

        jf.setTitle("窗体");

        //设置窗体位置

        jf.setLocation(200,200);

        //设置窗体大小

        jf.setSize(800,600);

        //创建中间容器面板

        JPanel jp = new JPanel();

        //只显示文字

        JLabel jl1 = new JLabel();

        jl1.setText("only text");

        jp.add(jl1);

        //显示文字与图片

        JLabel jl = new JLabel();

        jl.setText("text&&Picture");

        jl.setIcon(new ImageIcon("Images/01.jpg"));

        jl.setHorizontalTextPosition(SwingConstants.CENTER);

        jl.setVerticalTextPosition(SwingConstants.BOTTOM);

        jp.add(jl);

        //显示图片

        JLabel jl2 = new JLabel();

        jl2.setIcon(new ImageIcon("Images/01.jpg"));

        jp.add(jl2);

        //添加面板容器到窗体 jf.add(jp);

        jf.setContentPane(jp);

        //设置窗体可见性

        jf.setVisible(true);

        //设置窗体关闭方式

        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

}

JButton(按钮)

转载于:https://www.cnblogs.com/Aha-Best/p/10884537.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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 ); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值