码农公社  210.net.cn  210是何含义?10月24日是程序员节,1024 =210、210既

210

之意。

Swing 四种常见面板

示例 1 : 基本面板

JPanel即为基本面板

面板和JFrame一样都是容器,不过面板一般用来充当中间容器,把组件放在面板上,然后再把面板放在窗体上。

一旦移动一个面板,其上面的组件,就会全部统一跟着移动,采用这种方式,便于进行整体界面的设计

package gui;

import java.awt.Color;

import java.awt.FlowLayout;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

public class TestGUI {

public static void main(String[] args) {

JFrame f = new JFrame("LoL");

f.setSize(400, 300);

f.setLocation(200, 200);

f.setLayout(null);

JPanel p1 = new JPanel();

// 设置面板大小

p1.setBounds(50, 50, 300, 60);

// 设置面板背景颜色

p1.setBackground(Color.RED);

// 这一句可以没有,因为JPanel默认就是采用的FlowLayout

p1.setLayout(new FlowLayout());

JButton b1 = new JButton("英雄1");

JButton b2 = new JButton("英雄2");

JButton b3 = new JButton("英雄3");

// 把按钮加入面板

p1.add(b1);

p1.add(b2);

p1.add(b3);

JPanel p2 = new JPanel();

JButton b4 = new JButton("英雄4");

JButton b5 = new JButton("英雄5");

JButton b6 = new JButton("英雄6");

p2.add(b4);

p2.add(b5);

p2.add(b6);

p2.setBackground(Color.BLUE);

p2.setBounds(10, 150, 300, 60);

// 把面板加入窗口

f.add(p1);

f.add(p2);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setVisible(true);

}

}

示例 2 : ContentPane

JFrame上有一层面板,叫做ContentPane

平时通过f.add()向JFrame增加组件,其实是向JFrame上的 ContentPane加东西

package gui;

import javax.swing.JButton;

import javax.swing.JFrame;

public class TestGUI {

public static void main(String[] args) {

JFrame f = new JFrame("LoL");

f.setSize(400, 300);

f.setLocation(200, 200);

f.setLayout(null);

JButton b = new JButton("一键秒对方基地挂");

b.setBounds(50, 50, 280, 30);

f.add(b);

// JFrame上有一层面板,叫做ContentPane

// 平时通过f.add()向JFrame增加组件,其实是向JFrame上的 ContentPane加东西

// f.add等同于f.getContentPane().add(b);

f.getContentPane().add(b);

// b.getParent()获取按钮b所处于的容器

// 打印出来可以看到,实际上是ContentPane而非JFrame

System.out.println(b.getParent());

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setVisible(true);

}

}

示例 3 : SplitPanel

创建一个水平JSplitPane,左边是pLeft,右边是pRight

package gui;

import java.awt.Color;

import java.awt.FlowLayout;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JSplitPane;

public class TestGUI {

public static void main(String[] args) {

JFrame f = new JFrame("LoL");

f.setSize(400, 300);

f.setLocation(200, 200);

f.setLayout(null);

JPanel pLeft = new JPanel();

pLeft.setBounds(50, 50, 300, 60);

pLeft.setBackground(Color.RED);

pLeft.setLayout(new FlowLayout());

JButton b1 = new JButton("盖伦");

JButton b2 = new JButton("提莫");

JButton b3 = new JButton("安妮");

pLeft.add(b1);

pLeft.add(b2);

pLeft.add(b3);

JPanel pRight = new JPanel();

JButton b4 = new JButton("英雄4");

JButton b5 = new JButton("英雄5");

JButton b6 = new JButton("英雄6");

pRight.add(b4);

pRight.add(b5);

pRight.add(b6);

pRight.setBackground(Color.BLUE);

pRight.setBounds(10, 150, 300, 60);

// 创建一个水平JSplitPane,左边是p1,右边是p2

JSplitPane sp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, pLeft, pRight);

// 设置分割条的位置

sp.setDividerLocation(80);

// 把sp当作ContentPane

f.setContentPane(sp);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setVisible(true);

}

}

示例 4 : JScrollPanel

使用带滚动条的面板有两种方式

1在创建JScrollPane,把组件作为参数传进去

JScrollPane sp = new JScrollPane(ta);

2希望带滚动条的面板显示其他组件的时候,调用setViewportView

sp.setViewportView(ta);

package gui;

import javax.swing.JFrame;

import javax.swing.JScrollPane;

import javax.swing.JTextArea;

public class TestGUI {

public static void main(String[] args) {

JFrame f = new JFrame("LoL");

f.setSize(400, 300);

f.setLocation(200, 200);

f.setLayout(null);

//准备一个文本域,在里面放很多数据

JTextArea ta = new JTextArea();

for (int i = 0; i

ta.append(String.valueOf(i));

}

//自动换行

ta.setLineWrap(true);

JScrollPane sp = new JScrollPane(ta);

f.setContentPane(sp);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setVisible(true);

}

}

示例 5 : TabbedPanel

package gui;

import java.awt.Color;

import java.awt.FlowLayout;

import javax.swing.ImageIcon;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JTabbedPane;

public class TestGUI {

public static void main(String[] args) {

JFrame f = new JFrame("LoL");

f.setSize(400, 300);

f.setLocation(200, 200);

f.setLayout(null);

JPanel p1 = new JPanel();

p1.setBounds(50, 50, 300, 60);

p1.setBackground(Color.RED);

p1.setLayout(new FlowLayout());

JButton b1 = new JButton("英雄1");

JButton b2 = new JButton("英雄2");

JButton b3 = new JButton("英雄3");

p1.add(b1);

p1.add(b2);

p1.add(b3);

JPanel p2 = new JPanel();

JButton b4 = new JButton("英雄4");

JButton b5 = new JButton("英雄5");

JButton b6 = new JButton("英雄6");

p2.add(b4);

p2.add(b5);

p2.add(b6);

p2.setBackground(Color.BLUE);

p2.setBounds(10, 150, 300, 60);

JTabbedPane tp = new JTabbedPane();

tp.add(p1);

tp.add(p2);

// 设置tab的标题

tp.setTitleAt(0, "红色tab");

tp.setTitleAt(1, "蓝色tab");

ImageIcon i = new ImageIcon("e:/project/j2se/j.png");

tp.setIconAt(0,i );

tp.setIconAt(1,i );

f.setContentPane(tp);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setVisible(true);

}

}

示例 6 : CardLayerout

CardLayerout 布局器 很像TabbedPanel ,在本例里面上面是一个下拉框,下面是一个CardLayerout 的JPanel

这个JPanel里有两个面板,可以通过CardLayerout方便的切换

package gui;

import java.awt.BorderLayout;

import java.awt.CardLayout;

import java.awt.event.ItemEvent;

import java.awt.event.ItemListener;

import javax.swing.JButton;

import javax.swing.JComboBox;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JTextField;

public class TestGUI {

public static void main(String[] args) {

JFrame f = new JFrame("CardLayerout");

JPanel comboBoxPane = new JPanel();

String buttonPanel = "按钮面板";

String inputPanel = "输入框面板";

String comboBoxItems[] = { buttonPanel, inputPanel };

JComboBox cb = new JComboBox<>(comboBoxItems);

comboBoxPane.add(cb);

// 两个Panel充当卡片

JPanel card1 = new JPanel();

card1.add(new JButton("按钮 1"));

card1.add(new JButton("按钮 2"));

card1.add(new JButton("按钮 3"));

JPanel card2 = new JPanel();

card2.add(new JTextField("输入框", 20));

JPanel cards; // a panel that uses CardLayout

cards = new JPanel(new CardLayout());

cards.add(card1, buttonPanel);

cards.add(card2, inputPanel);

f.add(comboBoxPane, BorderLayout.NORTH);

f.add(cards, BorderLayout.CENTER);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setSize(250, 150);

f.setLocationRelativeTo(null);

f.setVisible(true);

cb.addItemListener(new ItemListener() {

@Override

public void itemStateChanged(ItemEvent evt) {

CardLayout cl = (CardLayout) (cards.getLayout());

cl.show(cards, (String) evt.getItem());

}

});

}

}

java swing桌面_Java图形界面swing面板相关推荐

  1. java swing 工具栏_Java图形界面Swing工具栏的使用

    码农公社  210.net.cn  210是何含义?10月24日是程序员节,1024 =210.210既 210 之意. Swing如何使用工具栏JToolBar 工具栏用于存放常用的按钮 步骤 1 ...

  2. java swing 获当前日期_Java图形界面Swing下日期控件

    码农公社  210.net.cn  210是何含义?10月24日是程序员节,1024 =210.210既 210 之意. Java自学-图形界面 日期控件 两种Swing下的日期控件 DatePick ...

  3. java 错误弹窗_JAVA图形界面问题(采用弹窗报错)

    展开全部 JOptionPane.showMessageDialog("null", "你的输入不合法!"); 这种方法e68a8432313133353236 ...

  4. java内部邮件系统_java 图形界面 邮件系统

    将后台的邮件系统使用javaGUI编程来实现,让我们可以在桌面端直接控制邮件的收发功能. 一.实现邮箱的登陆功能 邮件系统使用smtp协议发送邮件,使用POP3协议或者IMAP协议来收取邮件.SMTP ...

  5. [网络安全课设]基于JAVA的系统端口扫描软件设计实现(java代码+IDEA+UI图形界面+实验报告)

    链接::l基于JAVA的系统端口扫描软件设计实现(java代码+IDEA+UI图形界面+实验报告) 系统端口扫描软件设计实现 设计目的和任务 参照superscan.nmap等端口扫描软件的运行情况, ...

  6. 用Java开发桌面应用程序(Swing开发GUI程序)

    前面学过了面向对象基本概念.面向对象基本特征.Java基本包和API:异常.多线程.IO等. 基本概念 Swing:java中的一个包,负责开发GUI程序 GUI:图形用户界面,一般指可视化桌面系统中 ...

  7. java图形界面详解_JAVA 图形界面开发基础详解

    与C的win32一样,JAVA也有自己的图形界面开发,将在此篇博客中对基础部分进行讲解. 1.Java提供的图形界面类有哪些? Java提供了两套图形界面 (1)AWT组建(基础) AWT组件是jdk ...

  8. Intellij插件之~图形界面Swing UI Designer

    资料 Java Swing 介绍 JavaFX快速入门 Java Swing 图形界面开发简介 GUI Designer Basics scrcpy ScrcpyController Services ...

  9. java图形界面美化_Java图形界面设计——substance皮肤

    http://jianweili007-163-com.iteye.com/blog/1141358 ------------------------------------------------- ...

最新文章

  1. 数字汽车钥匙的安全性增强技术
  2. Centos7 更新pip和scipy
  3. ios CGRec用法
  4. linux中有fd set函数吗,LINUX下FD_SET介绍
  5. ReportViewer教程(15)-矩阵报表-6
  6. Android系统源码分析--Context
  7. [Ubuntu] fg、bg让你的进程在前后台之间切换
  8. BJT与MOSFET与IGBT的区别
  9. Python实验-小黑屋
  10. 如何基于深度学习实现商品识别技术|图普科技
  11. Netty空闲检测之读空闲
  12. c语言 北京时间转换utc时间_utc时间如何转换为北京时间
  13. 数据结构笔记(持续更新)
  14. c624芯片组的服务器,技嘉另类的服务器主板C422芯片组
  15. 地图可视化工具有哪些,基于地图的数据可视化软件
  16. 【SLAM学习】(二)相机原理
  17. js新框架 svelte
  18. git提交代码常用命令
  19. SEO常用MATE整理
  20. 计算机二级(二)仅学习

热门文章

  1. Linux下touch命令详解及C/C++代码实现
  2. Spannable、Spanned、Editable用法及差别
  3. windows脚本记录端口访问日志
  4. 会员消费占比高达96%,孩子王究竟是怎么做到的?
  5. Nik Collection 3(合集)
  6. 一个ACM渣渣关于找工作的胡扯
  7. 瑞云服务云赋能天正电气,助力数字化服务体验“落地”
  8. 【平面设计基础】09:横幅banner的设计
  9. 新手 Python-机器学习 四部曲资源汇总
  10. 让数据说话 QQ邮箱中转站速度评测/应用