GUI编程详解(gui编程是什么意思)

  本篇文章为你整理了GUI编程详解(gui编程是什么意思)的详细内容,包含有gui编程语言 gui编程是什么意思 gui编程的主要特征是什么? gui程序编写教程 GUI编程详解,希望能帮助你了解 GUI编程详解。

   public static void main(String[] args) {

   MyFrame myFrame1 = new MyFrame(100, 100, 300, 200, Color.black);

   MyFrame myFrame2 = new MyFrame(400, 100, 300, 200, Color.blue);

   MyFrame myFrame3 = new MyFrame(100, 300, 300, 200, Color.cyan);

   MyFrame myFrame4 = new MyFrame(400, 300, 300, 200, Color.GREEN);

  //将一个窗口的属性封装起来

  class MyFrame extends Frame {

   static int id =0;

   public MyFrame(int x,int y,int w,int h,Color color){

   //调用父类的有参构造

   super("Myframe"+(++id));

   //设置坐标位置以及窗口的宽和高

   setBounds(x,y,w,h);

   //设置窗口的背景颜色

   setBackground(color);

   ////设置窗口可见性

   setVisible(true);

  

 

 

  运行结果图

  二、Panel面板

  点击查看代码

  

package com.Tang.gui;

 

  import java.awt.*;

  import java.awt.event.WindowAdapter;

  import java.awt.event.WindowEvent;

  //panel可以看成是一个空间,但是不能单独存在

  public class PanelTest {

   public static void main(String[] args) {

   Frame frame = new Frame();

   Panel panel = new Panel();

   //设置布局

   frame.setLayout(null);

   //坐标

   frame.setBounds(300,300,500,500);

   frame.setBackground(new Color(0, 255, 0));

   //panel设置坐标,相对于frame而言

   panel.setBounds(50,50,400,400);

   panel.setBackground(new Color(211, 0, 255));

   //将面板放入窗口中

   frame.add(panel);

   frame.setVisible(true);

   //监听事件,监听窗口关闭事件,System.exit(0)

   //适配器模式:利用WindowAdapter重写自己需要的方法即可

   frame.addWindowListener(new WindowAdapter() {

   //窗口点击关闭的时候需要做的事情

   @Override

   public void windowClosing(WindowEvent e) {

   //结束程序

   System.exit(0);

  

 

  运行结果图

  三、布局管理器

  1.流式布局

  点击查看代码

  

package com.Tang.gui;

 

  import java.awt.*;

  import java.awt.event.WindowAdapter;

  import java.awt.event.WindowEvent;

  public class FlowLayoutTest {

   public static void main(String[] args) {

   Frame frame = new Frame();

   frame.setSize(200,200);

   Button button1 = new Button("Button1");

   Button button2 = new Button("Button2");

   Button button3 = new Button("Button3");

   //设置布局,FlowLayout由源码可知无参默认是居中模式

  // frame.setLayout(new FlowLayout());

   //在窗口里的按钮统一靠左

   frame.setLayout(new FlowLayout(FlowLayout.LEFT));

   frame.add(button1);

   frame.add(button2);

   frame.add(button3);

   frame.setVisible(true);

   frame.addWindowListener(new WindowAdapter() {

   @Override

   public void windowClosing(WindowEvent e) {

   System.exit(0);

  

 

  2.边界布局

  点击查看代码

  

package com.Tang.gui;

 

  import java.awt.*;

  import java.awt.event.WindowAdapter;

  import java.awt.event.WindowEvent;

  public class BorderLayoutTest {

   public static void main(String[] args) {

   Frame frame = new Frame("Twq");

   Button east = new Button("East");

   Button west = new Button("West");

   Button south = new Button("South");

   Button north = new Button("North");

   Button center = new Button("Center");

   frame.add(east, BorderLayout.EAST);

   frame.add(west,BorderLayout.WEST);

   frame.add(south,BorderLayout.SOUTH);

   frame.add(north,BorderLayout.NORTH);

   frame.add(center,BorderLayout.CENTER);

   frame.setSize(200,300);

   frame.setVisible(true);

   frame.addWindowListener(new WindowAdapter() {

   @Override

   public void windowClosing(WindowEvent e) {

   System.exit(0);

  

 

  运行结果图

  3.表格布局

  点击查看代码

  

package com.Tang.gui;

 

  import java.awt.*;

  import java.awt.event.WindowAdapter;

  import java.awt.event.WindowEvent;

  public class GridLayoutTest {

   public static void main(String[] args) {

   Frame frame = new Frame("Twq");

   Button but1 = new Button("but1");

   Button but2 = new Button("but2");

   Button but3 = new Button("but3");

   Button but4 = new Button("but4");

   Button but5 = new Button("but5");

   Button but6 = new Button("but6");

   //设置表格布局为两行三列,还可在后面继续加参数即上下的间隔

   frame.setLayout(new GridLayout(2,3));

   frame.add(but1);

   frame.add(but2);

   frame.add(but3);

   frame.add(but4);

   frame.add(but5);

   frame.add(but6);

   //不用设置窗口的大小,窗口会根据所添加的东西自动分配空间大小

   frame.pack();

   frame.setVisible(true);

   frame.addWindowListener(new WindowAdapter() {

   @Override

   public void windowClosing(WindowEvent e) {

   System.exit(0);

  

 

  实现下图操作

  点击查看代码

  

package com.Tang.gui;

 

  import java.awt.*;

  import java.awt.event.WindowAdapter;

  import java.awt.event.WindowEvent;

  public class ExDemo {

   public static void main(String[] args) {

   Frame frame = new Frame();

   Panel panel1 = new Panel();

   Panel panel2 = new Panel();

   //首先分为上下两个结构,分别用面板来放置下面的按钮

   frame.setLayout(new GridLayout(2,1));

   frame.add(panel1);

   frame.add(panel2);

   //对于上半部分,使用borderLayout布局,左右放button,中间放面板(以便在中间继续放置按钮)

   Button button1 = new Button("button1");

   Panel panel3 = new Panel();

   Button button4 = new Button("button4");

   panel1.add(button1,BorderLayout.WEST);

   panel1.add(panel3,BorderLayout.CENTER);

   panel1.add(button4,BorderLayout.EAST);

   //对于中间部分的panel3继续采用表格布局,上下方button

   panel3.setLayout(new GridLayout(2,1));

   Button button2 = new Button("button2");

   Button button3 = new Button("button3");

   panel3.add(button2);

   panel3.add(button3);

   //然后对于下半部分,同理左右放置button,中间放置面板

   Button button5 = new Button("button5");

   Panel panel4 = new Panel();

   Button button10 = new Button("button10");

   panel2.add(button5,BorderLayout.WEST);

   panel2.add(panel4,BorderLayout.CENTER);

   panel2.add(button10,BorderLayout.EAST);

   //对于中间部分的panel4继续采用表格布局2行2列

   panel4.setLayout(new GridLayout(2,2));

   Button button6 = new Button("button6");

   Button button7 = new Button("button7");

   Button button8 = new Button("button8");

   Button button9 = new Button("button9");

   panel4.add(button6);

   panel4.add(button7);

   panel4.add(button8);

   panel4.add(button9);

   frame.setSize(400,300);

   frame.setLocation(300,400);

   frame.setVisible(true);

   frame.addWindowListener(new WindowAdapter() {

   @Override

   public void windowClosing(WindowEvent e) {

   System.exit(0);

  

 

  运行结果如下图
 

  四、事件监听

  1.实现当点击按钮时控制台输出aaa

  点击查看代码

  

package com.Tang.gui;

 

  import java.awt.*;

  import java.awt.event.ActionEvent;

  import java.awt.event.ActionListener;

  import java.awt.event.WindowAdapter;

  import java.awt.event.WindowEvent;

  public class ActionEventTest {

   public static void main(String[] args) {

   //按下按钮,触发一些事件

   Frame frame = new Frame();

   Button button = new Button("Twq");

   //因为addActionListener需要一个ActionListener,所以,我们需要构造一个ActionListener

   //构造原则:是接口就实现其方法,是父类就继承

   MyActionListener myActionListener = new MyActionListener();

   button.addActionListener(myActionListener);

   frame.add(button);

   frame.setSize(300,200);

   frame.setVisible(true);

   frame.addWindowListener(new WindowAdapter() {

   @Override

   public void windowClosing(WindowEvent e) {

   System.exit(0);

  class MyActionListener implements ActionListener{

   @Override

   public void actionPerformed(ActionEvent e) {

   System.out.println("aaa");

  

 

  运行结果图

  2.两个按钮实现同一个监听

  点击查看代码

  

package com.Tang.gui;

 

  import java.awt.*;

  import java.awt.event.ActionEvent;

  import java.awt.event.ActionListener;

  import java.awt.event.WindowAdapter;

  import java.awt.event.WindowEvent;

  public class ActionMonitorTest {

   public static void main(String[] args) {

   Frame frame = new Frame();

   Button button1 = new Button("start");

   Button button2 = new Button("stop");

   //如果不显示定义就会走默认的无参构造

   button1.setActionCommand("start新名");

   MyMonitr myMonitr = new MyMonitr();

   button1.addActionListener(myMonitr);

   button2.addActionListener(myMonitr);

   frame.setLayout(new FlowLayout(FlowLayout.CENTER));

   frame.add(button1);

   frame.add(button2);

   frame.setSize(300,300);

   frame.setVisible(true);

   frame.addWindowListener(new WindowAdapter() {

   @Override

   public void windowClosing(WindowEvent e) {

   System.exit(0);

  class MyMonitr implements ActionListener{

   @Override

   public void actionPerformed(ActionEvent e) {

   //e.getActionCommand()获取按钮信息

   System.out.println(e.getActionCommand());

  

 

   4.输入框事件的监听

  点击查看代码

  

package com.Tang.gui;

 

  import java.awt.*;

  import java.awt.event.ActionEvent;

  import java.awt.event.ActionListener;

  import java.awt.event.WindowAdapter;

  import java.awt.event.WindowEvent;

  public class ActionEventTest2 {

   public static void main(String[] args) {

   MyFrame1 myFrame1 = new MyFrame1();

   //关闭事件

   myFrame1.addWindowListener(new WindowAdapter() {

   @Override

   public void windowClosing(WindowEvent e) {

   System.exit(0);

  class MyFrame1 extends Frame {

   public MyFrame1(){

   TextField textField = new TextField();

   MyMoniter2 moniter2 = new MyMoniter2();

   //每次通过回车触发监视器

   textField.addActionListener(moniter2);

  // //在前台实现输入的内容为*,但是后台获取可以正常获取输入的数据

  // textField.setEchoChar(*);

   add(textField);

   pack();

   setVisible(true);

  class MyMoniter2 implements ActionListener{

   @Override

   public void actionPerformed(ActionEvent e) {

   Object source = e.getSource();

   TextField tf = (TextField)source;

   System.out.println(tf.getText());//获取文本框的输入的数据

   tf.setText("");//每次回车之后将文本框内容清空

  

 

  运行结果图

  在前台输入框中不显示输入数据,但是在后台可以正常获取文本框中的数据

  五、简易计算器的实现

  实现如下图操作功能

  点击查看代码

  

package com.Tang.gui;

 

  import java.awt.*;

  import java.awt.event.ActionEvent;

  import java.awt.event.ActionListener;

  import java.awt.event.WindowAdapter;

  import java.awt.event.WindowEvent;

  public class CalculateTest {

   public static void main(String[] args) {

   new MyFrame3();

  class MyFrame3 extends Frame {

   public MyFrame3(){

   //3个文本框

   TextField num1 = new TextField(10);

   TextField num2 = new TextField(10);

   TextField num3 = new TextField(20);

   //1个按钮

   Button button = new Button("=");

   //1个标签

   Label label = new Label("+");

   MyMoniter3 m = new MyMoniter3(num1,num2,num3);

   button.addActionListener(m);

   pack();

   add(num1);

   add(label);

   add(num2);

   add(button);

   add(num3);

   setLayout(new FlowLayout());

   setVisible(true);

   addWindowListener(new WindowAdapter() {

   @Override

   public void windowClosing(WindowEvent e) {

   System.exit(0);

  class MyMoniter3 implements ActionListener{

   //获取三个文本框的值

   private TextField num1,num2,num3;

   public MyMoniter3(TextField num1,TextField num2,TextField num3) {

   this.num1 = num1;

   this.num2 = num2;

   this.num3 = num3;

   @Override

   public void actionPerformed(ActionEvent e) {

   //1.获得加数和被加数

   int i1 = Integer.parseInt(num1.getText());

   int i2 = Integer.parseInt(num2.getText());

   //2.将两个数相加之后的值放入第三个文本框

   num3.setText(""+(i1+i2));

   //3.清除前两个框

   num1.setText("");

   num2.setText("");

  

 

  运行结果图如下

  
 

  代码优化(转换为面向对像)

  点击查看代码

  

public class CalculateTest {

 

   public static void main(String[] args) {

   new Calculator().loadFrame();

  class Calculator extends Frame {

   //属性

   TextField num1,num2,num3;

   //方法

   public void loadFrame(){

   //3个文本框

   num1 = new TextField(10);

   num2 = new TextField(10);

   num3 = new TextField(20);

   //1个按钮

   Button button = new Button("=");

   //1个标签

   Label label = new Label("+");

   MyMoniter3 m = new MyMoniter3(this);

   button.addActionListener(m);

   pack();

   add(num1);

   add(label);

   add(num2);

   add(button);

   add(num3);

   setLayout(new FlowLayout());

   setVisible(true);

   addWindowListener(new WindowAdapter() {

   @Override

   public void windowClosing(WindowEvent e) {

   System.exit(0);

  class MyMoniter3 implements ActionListener{

   //获取计算器这个对象,在一个类中组合另一个类

   Calculator calculator;

   public MyMoniter3(Calculator calculator) {

   this.calculator = calculator;

   @Override

   public void actionPerformed(ActionEvent e) {

   //1.获得加数和被加数

   int i1 = Integer.parseInt(calculator.num1.getText());

   int i2 = Integer.parseInt(calculator.num2.getText());

   //2.将两个数相加之后的值放入第三个文本框

   calculator.num3.setText(""+(i1+i2));

   //3.清除前两个输入框

   calculator.num1.setText("");

   calculator.num2.setText("");

  

 

  代码进一步优化(将监听器转换为内部类)

  点击查看代码

  

public class CalculateTest {

 

   public static void main(String[] args) {

   new Calculator().loadFrame();

  class Calculator extends Frame {

   //属性

   TextField num1,num2,num3;

   //方法

   public void loadFrame(){

   //3个文本框

   num1 = new TextField(10);

   num2 = new TextField(10);

   num3 = new TextField(20);

   //1个按钮

   Button button = new Button("=");

   //1个标签

   Label label = new Label("+");

   MyMoniter3 m = new MyMoniter3();

   button.addActionListener(m);

   pack();

   add(num1);

   add(label);

   add(num2);

   add(button);

   add(num3);

   setLayout(new FlowLayout());

   setVisible(true);

   addWindowListener(new WindowAdapter() {

   @Override

   public void windowClosing(WindowEvent e) {

   System.exit(0);

  //监听器类,内部类最大的好处就是可以畅通无阻的访问外部内的属性和方法

   class MyMoniter3 implements ActionListener{

   @Override

   public void actionPerformed(ActionEvent e) {

   //1.获得加数和被加数

   int i1 = Integer.parseInt(num1.getText());

   int i2 = Integer.parseInt(num2.getText());

   //2.将两个数相加之后的值放入第三个文本框

   num3.setText(""+(i1+i2));

   //3.清除前两个框

   num1.setText("");

   num2.setText("");

  

 

  1.画笔的简单使用

  点击查看代码

  

public class PaintTest {

 

   public static void main(String[] args) {

   new Mypaint().loadFrame();

  class Mypaint extends Frame {

   public void loadFrame(){

   setVisible(true);

   setBounds(200,200,400,400);

   addWindowListener(new WindowAdapter() {

   @Override

   public void windowClosing(WindowEvent e) {

   System.exit(0);

   //重写画笔方法

   @Override

   public void paint(Graphics g) {

   //设置画笔颜色

   g.setColor(Color.red);

   //画一个空心圆

   g.drawOval(100,100,200,200);

   //画一个实心圆

   g.fillOval(150,150,100,100);

   //养成喜欢:画笔用完,将它还原为最初的颜色

  

 

  运行结果图:

  2.鼠标监听

  目的:想要实现鼠标画画

  点击查看代码

  

package com.Tang.gui;

 

  import java.awt.*;

  import java.awt.event.MouseAdapter;

  import java.awt.event.MouseEvent;

  import java.awt.event.WindowAdapter;

  import java.awt.event.WindowEvent;

  import java.util.ArrayList;

  import java.util.Iterator;

  public class MouseistenerTest {

   public static void main(String[] args) {

   new MyFrame4("画图");

  class MyFrame4 extends Frame {

   //画画需要画笔,需要监听鼠标当前的位置

   //用集合数组存储鼠标当前点击的位置

   ArrayList points;

   public MyFrame4(String title){

   super(title);

   setBounds(100,100,300,300);

   points = new ArrayList();

   //针对窗口的鼠标的监听器

   addMouseListener(new MouseListenered());

   setVisible(true);

   setSize(300,300);

   addWindowListener(new WindowAdapter() {

   @Override

   public void windowClosing(WindowEvent e) {

   System.exit(0);

   @Override

   public void paint(Graphics g) {

   //画画,监听鼠标的事件

   Iterator iterator = points.iterator();

   while(iterator.hasNext()){

   //将当前获取到的数组中的点转换为一个点类

   Point point = (Point)iterator.next();

   //设置鼠标点击点的颜色

   g.setColor(Color.CYAN);

   //让当前画笔获得鼠标点击的位置

   g.fillOval(point.x,point.y,10,10);

   //将获取到的鼠标的点存储到数组里

   public void addPaint(Point point){

   points.add(point);

   //若采用实现MouseListener接口的话,就必须要重写其接口内的所有方法

   //所以可以采用适配器模式去重写自己需要的方法,

   class MouseListenered extends MouseAdapter {

   //鼠标有按下,弹起,按住不放

   @Override

   public void mousePressed(MouseEvent e) {

   //在窗口上获取当前鼠标的位置

   MyFrame4 frame =(MyFrame4) e.getSource();

   frame.addPaint(new Point(e.getX(),e.getY()));

   //每次点击鼠标都需要重新画一遍

   frame.repaint();//刷新

  

 

  运行结果图

  3.窗口监听

  目的:实现当 离开当前窗口后 窗口标题设置为“人呢?快回来”,回到当前窗口则窗口标题设置为“欢迎回来”

  点击查看代码

  

 

 

  public class WindowListennerTest {

   public static void main(String[] args) {

   new MyWindowListener();

  class MyWindowListener extends Frame{

   public MyWindowListener(){

   setBounds(100,100,300,300);

   setVisible(true);

   addWindowListener(new WindowAdapter() {

   @Override

   public void windowClosing(WindowEvent e) {

   System.exit(0);

   @Override

   public void windowDeactivated(WindowEvent e) {

   MyWindowListener mw =(MyWindowListener)e.getSource();

   mw.setTitle("人呢?快回来");

   @Override

   public void windowActivated(WindowEvent e) {

   MyWindowListener mw =(MyWindowListener)e.getSource();

   mw.setTitle("欢迎回来");

  

 

  运行结果图

  4.键盘监听

  目的:实现当在窗口中按下键盘的上键,则输出你按下了上键

  点击查看代码

  

public class KeyListenerTest {

 

   public static void main(String[] args) {

   new KeyFrame();

  class KeyFrame extends Frame {

   public KeyFrame(){

   setVisible(true);

   setSize(300,300);

   addKeyListener(new KeyAdapter() {

   @Override

   public void keyPressed(KeyEvent e) {

   int keyCode = e.getKeyCode();

   if(keyCode == KeyEvent.VK_UP){

   System.out.println("你按下了上键");

  

 

  运行结果图

  七、GUI-Swing窗体

  1.JFrame窗体

  看其源码可知JFrame和Frame 还是有很大联系

  点击查看代码

  

 

 

  public class JFrameTest {

   //init()用于窗口的初始化操作

   public void init(){

   //顶级窗口

   JFrame jf = new JFrame("这是一个JFrame窗口");

   JLabel label = new JLabel("欢迎进入Twq的博客,欢迎点赞加关注");

   jf.add(label);

   //让标签居中显示

   label.setHorizontalAlignment(SwingConstants.CENTER);

   //由于JFrame设计到容器的概念,所有东西要放在容器里去实现

   //jf.setBackground(Color.gray);这样写并不能实现背景的设置

   jf.getContentPane().setBackground(Color.cyan);

   //窗口关闭

   jf.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

   jf.setBounds(100,100,300,300);

   jf.setVisible(true);

   public static void main(String[] args) {

   new JFrameTest().init();

  

 

  运行结果图

  点击查看代码

  

 

 

  //主窗口

  public class DialogTest extends JFrame{

   public DialogTest() {

   Container container = this.getContentPane();

   JButton button = new JButton("点击出现弹窗");

   button.setSize(200,50);

   add(button);

   //添加按钮监听事件

   button.addActionListener(new ActionListener() {

   @Override

   public void actionPerformed(ActionEvent e) {

   new MyDialog();//当按钮被点击后新建一个窗口

   setLayout(null);

   setBounds(80,80,300,300);

   setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

   setVisible(true);

   public static void main(String[] args) {

   new DialogTest();

  //弹窗窗口

  class MyDialog extends JDialog{

   public MyDialog() {

   setBounds(100,100,400,300);

   JLabel label = new JLabel("如果觉得文章内容不错可以点赞加关注呦");

   label.setSize(300,50);

   this.add(label);

   Container container = this.getContentPane();

   label.setHorizontalAlignment(SwingConstants.CENTER);

   container.setBackground(Color.CYAN);

   //弹窗默认就可以进行关闭操作,不需要在进行关闭操作

   //setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

   setVisible(true);

  

 

  运行结果图

  3.标签之Icon

  (1)图标Icon

  点击查看代码

  

 

 

  public class IconTest extends JFrame implements Icon {

   private int width,height;

   public IconTest() {

   public IconTest(int width,int height) {

   this.width = width;

   this.height = height;

   public void init(){

   IconTest iconTest = new IconTest(20,20);

   //图标放在标签上,也可以放在按钮上

   JLabel label = new JLabel("这是一个图标",iconTest,SwingConstants.CENTER);

   Container container = getContentPane();

   container.add(label);

   setBounds(100,100,300,300);

   setVisible(true);

   setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

   @Override

   public void paintIcon(Component c, Graphics g, int x, int y) {

   g.fillOval(x,y,width,height);

   @Override

   public int getIconWidth() {

   return this.width;

   @Override

   public int getIconHeight() {

   return this.height;

   public static void main(String[] args) {

   new IconTest().init();

  

 

  运行结果图

  
 

  (2)图片Icon

  点击查看代码

  

package com.Tang.gui.swing;

 

  import javax.swing.*;

  import java.awt.*;

  import java.net.URL;

  public class ImageIconTest extends JFrame {

   public ImageIconTest(){

   //获取图片地址

   URL url = ImageIconTest.class.getResource("tx的副本.jpg");

   JLabel label = new JLabel("这是一个图片图标");

   //图片图标获取图片的路径

   ImageIcon imageIcon = new ImageIcon(url);

   //将图片放入标签中

   label.setIcon(imageIcon);

   Container container = getContentPane();

   container.add(label);

   setBounds(100,100,300,200);

   setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

   setVisible(true);

   public static void main(String[] args) {

   new ImageIconTest();

  

 

  运行结果图

  (1)普通面板

  点击查看代码

  

package com.Tang.gui.swing;

 

  import javax.swing.*;

  import java.awt.*;

  public class JPanneltest extends JFrame {

   public JPanneltest(){

   Container container = this.getContentPane();

   container.setLayout(new GridLayout(2,1,10,10));

   JPanel jP = new JPanel(new GridLayout(1,2));

   JPanel jP1 = new JPanel(new GridLayout(1,2));

   jP.add(new JButton("1"));

   jP.add(new JButton("2"));

   jP1.add(new JButton("3"));

   jP1.add(new JButton("4"));

   container.add(jP);

   container.add(jP1);

   this.setBounds(200,200,300,300);

   this.setVisible(true);

   //设置关闭窗口操作

   this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

   public static void main(String[] args) {

   new JPanneltest();

  

 

  运行结果图:图中两个面板之间的空隙即为容器设置的布局方式间隔的10

  (2)JScroll(滚轮)面板

  点击查看代码

  

 

 

  public class ScrollPanelTest extends JFrame {

   public ScrollPanelTest() {

   Container container = this.getContentPane();

   //文本域:在文本框中输入的时候可以进行换行操作

   JTextArea textArea = new JTextArea();

   //设置滚动面板,并将文本域放到面板中

   JScrollPane scrollPane = new JScrollPane(textArea);

   //将滚动面板添加到窗口中

   container.add(scrollPane);

   //设置窗口的初始状态

   setVisible(true);

   setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

   setBounds(200,200,300,300);

   public static void main(String[] args) {

   new ScrollPanelTest();

  

 

  运行结果图

  (1)图片按钮

  目的:实现将一张图片变成图标,并放在按钮上,并且当鼠标放在按钮上停留时显示文字

  点击查看代码

  

 

 

  public class ButtonImageTest extends JFrame {

   public ButtonImageTest() {

   Container container = this.getContentPane();

   URL url = ButtonImageTest.class.getResource("tx的副本.jpg");

   Icon icon = new ImageIcon(url);

   JButton button = new JButton();

   //将图片放入按钮中

   button.setIcon(icon);

   //当鼠标在按钮上停留的时候就会显示下方设置的文字

   button.setToolTipText("图片按钮");

   container.add(button);

   setVisible(true);

   setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

   setBounds(100,100,200,200);

   public static void main(String[] args) {

   new ButtonImageTest();

  

 

  运行结果图

  (2)单选框按钮

  目的:实现单选框只能选择一个选项的功能

  点击查看代码

  

public class JButtonTest02 extends JFrame {

 

   public JButtonTest02() {

   Container container = getContentPane();

   JRadioButton radioButton1 = new JRadioButton("男");

   JRadioButton radioButton2 = new JRadioButton("女");

   JRadioButton radioButton3 = new JRadioButton("中性");

   //由于单选框只能选择一个,将三个按钮放入一个组中就可以实现只选一个

   ButtonGroup group = new ButtonGroup();

   //将三个按钮放入一个组中

   group.add(radioButton1);

   group.add(radioButton2);

   group.add(radioButton3);

   container.add(radioButton1,BorderLayout.NORTH);

   container.add(radioButton2,BorderLayout.CENTER);

   container.add(radioButton3,BorderLayout.SOUTH);

   setVisible(true);

   setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

   setBounds(100,100,200,200);

   public static void main(String[] args) {

   new JButtonTest02();

  

 

  运行结果图

  (3)复选框按钮

  目的:实现多项选择的功能

  点击查看代码

  

public class JButtonTest02 extends JFrame {

 

   public JButtonTest02() {

   Container container = getContentPane();

   JRadioButton radioButton1 = new JRadioButton("男");

   JRadioButton radioButton2 = new JRadioButton("女");

   JRadioButton radioButton3 = new JRadioButton("中性");

   //实现复选框按钮

   JCheckBox checkBox1 = new JCheckBox("篮球");

   JCheckBox checkBox2 = new JCheckBox("足球");

   JCheckBox checkBox3 = new JCheckBox("羽毛球");

   container.add(checkBox1,BorderLayout.NORTH);

   container.add(checkBox2,BorderLayout.CENTER);

   container.add(checkBox3,BorderLayout.SOUTH);

   setVisible(true);

   setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

   setBounds(100,100,200,200);

   public static void main(String[] args) {

   new JButtonTest02();

  

 

  运行结果图

  (4)下拉框选项

  点击查看代码

  

 

 

  public class JButtonTest02 extends JFrame {

   public JButtonTest02() {

   Container container = getContentPane();

   //创建一个下拉框

   JComboBox statue = new JComboBox();

   //提供下拉选项

   statue.addItem(null);

   statue.addItem("正在热映");

   statue.addItem("已下架");

   statue.addItem("即将上映");

   container.add(statue);

   setVisible(true);

   setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

   setBounds(100,100,200,200);

   public static void main(String[] args) {

   new JButtonTest02();

  

 

  运行结果图

  (5)列表框(如QQ好友列表)

  点击查看代码

  

public class JButtonTest02 extends JFrame {

 

   public JButtonTest02() {

   Container container = getContentPane();

   //实现列表框

   Vector vector = new Vector();

   vector.add("张三");

   vector.add("李四");

   vector.add("王五");

   vector.add("赵六");

   //将vector集合中的内容放入列表框中

   JList jList = new JList(vector);

   container.add(jList);

   setVisible(true);

   setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

   setBounds(100,100,200,200);

   public static void main(String[] args) {

   new JButtonTest02();

  

 

  运行结果图

  以上就是GUI编程详解(gui编程是什么意思)的详细内容,想要了解更多 GUI编程详解的内容,请持续关注盛行IT软件开发工作室。

郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。

留言与评论(共有 条评论)
   
验证码: