2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > GUI(Graphic User Interface) 图形用户界面

GUI(Graphic User Interface) 图形用户界面

时间:2019-01-27 03:25:32

相关推荐

GUI(Graphic User Interface) 图形用户界面

一、初步认识Swing

JFrame是GUI中的容器

JButton是最常见的组件-按钮

注意:容器对象.setVisible(true);会对所有的组件进行渲染,所以一定要放在最后面

简单的例子见如下代码

package learn;import javax.swing.JFrame;import javax.swing.JButton;public class GUI {public static void main(String[] args) {//主窗体JFrame f=new JFrame("爱情系统");//主窗体设置大小(长宽)f.setSize(600,400);//主窗体设置位置(在Eclipse上显示的位置)f.setLocation(350,350);//主窗体中的组件设置为绝对定位f.setLayout(null);//按钮组件JButton z=new JButton("射手座");JButton w=new JButton("水瓶座");//设置组件的大小和位置//第一个数值是距离左边的距离,第二个数值是距离上边的距离,第三和第四分别是组件的长宽z.setBounds(50,50,150,30);w.setBounds(300,50,150,30);//把按钮加入到主窗体中f.add(z);f.add(w);//关闭窗口的时候,退出程序f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//让窗口变得可见f.setVisible(true);}}

二、事件监听

import java.awt.event.ActionEvent;动作事件

import java.awt.event.ActionListener;动作监听程序

import javax.swing.ImageIcon;图像标志

import javax.swing.JLable;标志

按钮监听见如下代码:

package learn;import java.awt.event.ActionListener;import java.awt.event.ActionEvent;import javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;public class love {public static void main(String[] args) {JFrame f = new JFrame("爱之名");f.setSize(700, 600);f.setLocation(580, 200);f.setLayout(null);final JLabel l = new JLabel();ImageIcon i = new ImageIcon("D:/新建文件夹/sheshou.jpg");l.setIcon(i);l.setBounds(50, 50, i.getIconWidth(), i.getIconHeight());final JLabel ll=new JLabel();ImageIcon ii = new ImageIcon("D:/新建文件夹/shuiping.jpg");ll.setIcon(ii);ll.setBounds(300,50,ii.getIconWidth(),ii.getIconHeight());JButton b = new JButton("水瓶座");b.setBounds(120, 300, 100, 30);JButton bb=new JButton("射手座");bb.setBounds(350,300,100,30);// 给按钮 增加 监听b.addActionListener(new ActionListener() {// 当按钮被点击时,就会触发 ActionEvent事件// actionPerformed 方法就会被执行public void actionPerformed(ActionEvent e) {l.setVisible(false);}});f.add(l);f.add(ll);f.add(b);f.add(bb);f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);f.setVisible(true);}}

键盘监听见如下代码:

import java.awt.event.KeyListener;键盘监听器

import java.awt.event.KeyEvent;键盘事件

keyPressed代表 键被按下

keyReleased 代表键被弹起

keyTyped 代表一个向下弹起的组合动作

keyEvent.getKeyCode() 可以获取当前点下了哪个键

package learn;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;public class learn2 {public static void main(String[] args) {JFrame f = new JFrame("LoL");f.setSize(400, 300);f.setLocation(580, 200);f.setLayout(null);final JLabel l = new JLabel();ImageIcon i = new ImageIcon("D:/新建文件夹/sheshou.jpg");l.setIcon(i);l.setBounds(50, 50, i.getIconWidth(), i.getIconHeight());// 增加键盘监听f.addKeyListener(new KeyListener() {// 键被弹起public void keyReleased(KeyEvent e) {// 39代表按下了 “右键”if (e.getKeyCode() == 39) {// 图片向右移动 (y坐标不变,x坐标增加)l.setLocation(l.getX() + 10, l.getY());}}//键被按下public void keyPressed(KeyEvent e) {// TODO Auto-generated method stub}// 一个按下弹起的组合动作public void keyTyped(KeyEvent e) {}});f.add(l);f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);f.setVisible(true);}}

鼠标监听:

import java.awt.event.MouseEvent;鼠标事件

import java.awt.event.MouseListener;鼠标监听器

mouseReleased 鼠标释放

mousePressed 鼠标按下

mouseExited 鼠标退出

mouseEntered 鼠标进入

mouseClicked 鼠标点击

举例子代码如下,使用mouseEntered,当鼠标进入图片的时候,图片就移动位置

package learn;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.util.Random;import javax.swing.JFrame;import javax.swing.ImageIcon;import javax.swing.JLabel;import javax.swing.JButton;public class ml {public static void main(String[] args) {// TODO Auto-generated method stubfinal JFrame f=new JFrame("来抓住我吧");f.setSize(800,600);f.setLocationRelativeTo(null);f.setLayout(null);final JLabel l=new JLabel();ImageIcon i=new ImageIcon("D:/新建文件夹/sheshou.jpg");l.setIcon(i);l.setBounds(300,300,i.getIconWidth(),i.getIconHeight());f.add(l);l.addMouseListener(new MouseListener(){//释放鼠标public void mouseReleased(MouseEvent e){}//按下鼠标public void mousePressed(MouseEvent e){}//退出鼠标public void mouseExited(MouseEvent e){}//鼠标进入public void mouseEntered(MouseEvent e){Random r=new Random();int x=r.nextInt(f.getWidth() - l.getWidth());int y=r.nextInt(f.getHeight() - l.getHeight());l.setLocation(x,y);}//按下释放组合动作为点击鼠标public void mouseClicked(MouseEvent e){}});f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);f.setVisible(true);}}

适配器

import java.awt.event.MouseAdapter鼠标监听适配器

一般在写监听器的时候,会实现MouseListener。但是MouseListener里面有很多方法都没有用到,比如mouseReleased,mousePressed等等;这个时候就可以使用MouseAdapter,只需要重写必要的方法即可

// MouseAdapter 适配器,只需要重写用到的方法,没有用到的就不用写了l.addMouseListener(new MouseAdapter() {// 只有mouseEntered用到了public void mouseEntered(MouseEvent e) {Random r = new Random();int x = r.nextInt(f.getWidth() - l.getWidth());int y = r.nextInt(f.getHeight() - l.getHeight());l.setLocation(x, y);}});

三、容器

java的图形界面中,容器是用来存放按钮、输入框等组件的

窗体容器有两种,一个是JFrame,一个是JDialog

写法一样,注意区别的是JFrame右上角有最大化最小化按钮,JDialog没有。

模态JDialog:当一个对话框被设置为模态的时候,其背后的父窗体是不能被激活的,除非该对话框被关闭,见如下代码

import javax.swing.JButton;import javax.swing.JDialog;import javax.swing.JFrame;public class TestGUI {public static void main(String[] args) {JFrame f = new JFrame("外部窗体");f.setSize(800, 600);f.setLocation(100, 100);// 根据外部窗体实例化JDialogJDialog d = new JDialog(f);// 设置为模态d.setModal(true);d.setTitle("模态的对话框");d.setSize(400, 300);d.setLocation(200, 200);d.setLayout(null);JButton b = new JButton("一键秒对方基地挂");b.setBounds(50, 50, 280, 30);d.add(b);f.setVisible(true);d.setVisible(true);}}

窗体大小不可变化:通过调用setResizable(false);做到窗体大小不可变化,窗体不能放大缩小

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);// 窗体大小不可变化f.setResizable(false);f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);f.setVisible(true);}}

四、布局器(layout):布局器是用在容器上的,用来决定容器上组件摆放的位置和大小

1、绝对定位:不使用布局器,组件的位置和大小需要单独指定,上面代码用的就是绝对

如:f.setLayout(null);

2、顺序布局器(FlowLayout):设置布局器为FlowLayout,顺序布局器,容器上的组件水平摆放

加入到容器即可,无需单独指定大小和位置,见如下代码

import java.awt.FlowLayout;f.setLayout(new FlowLayout());JButton b=new JButton("大尖牛");f.add(b);

3、设置布局器(BorderLayout):设置布局器为BorderLayout,容器上的组件按照上北 下南 左西 右东 中的顺序摆放,见如下代码

package learn;import javax.swing.JFrame;import javax.swing.JButton;import java.awt.BorderLayout;public class BL {public static void main(String[] args) {// TODO Auto-generated method stubJFrame f=new JFrame("人物");f.setSize(600,400);f.setLocation(300,300);// 设置布局器为BorderLayerout// 容器上的组件按照上北下南左西右东中的顺序摆放f.setLayout(new BorderLayout());JButton b1=new JButton("汤山");JButton b2=new JButton("六神");JButton b3=new JButton("萧炎");JButton b4=new JButton("孟非");JButton b5=new JButton("国风");// 加入到容器的时候,需要指定位置f.add(b1,BorderLayout.NORTH);f.add(b2,BorderLayout.SOUTH);f.add(b3,BorderLayout.WEST);f.add(b4,BorderLayout.EAST);f.add(b5,BorderLayout.CENTER);f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);f.setVisible(true);}}

4、网格布局器(GridLayout):如名字一样,网格布局,见如下代码

package gui;import java.awt.GridLayout;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);// 设置布局器为GridLayerout,即网格布局器// 该GridLayerout的构造方法表示该网格是2行3列f.setLayout(new GridLayout(2, 3));JButton b1 = new JButton("洪七");JButton b2 = new JButton("段智兴");JButton b3 = new JButton("欧阳锋");JButton b4 = new JButton("黄药师");JButton b5 = new JButton("周伯通");f.add(b1);f.add(b2);f.add(b3);f.add(b4);f.add(b5);f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);f.setVisible(true);}}

5、setPreferredSize:

即便 使用 布局器 ,也可以 通过setPreferredSize,向布局器建议该组件显示的大小.

注只对部分布局器起作用,比如FlowLayout可以起作用。 比如GridLayout就不起作用,因为网格布局器必须对齐

package gui;import java.awt.Dimension;import java.awt.FlowLayout;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(new FlowLayout());JButton b1 = new JButton("英雄1");JButton b2 = new JButton("英雄2");JButton b3 = new JButton("英雄3");// 即便 使用 布局器 ,也可以 通过setPreferredSize,向布局器建议该组件显示的大小b3.setPreferredSize(new Dimension(180, 40));f.add(b1);f.add(b2);f.add(b3);f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);f.setVisible(true);}}

做一个计算器吧,见如下代码

package pratice;import javax.swing.JFrame;import javax.swing.JButton;import java.awt.GridLayout;public class JSQ {public static void main(String[] args) {JFrame f=new JFrame("计算器");f.setSize(500,400);f.setLocation(300,300);f.setLayout(new GridLayout(4,5));JButton b[]=new JButton[20];String s[]={"7","8","9","/","sq","4","5","6","*","%","1","2","3","-","1/x","0","+/-",".","+","="};for(int i=0;i<s.length;i++){b[i]=new JButton(s[i]);f.add(b[i]);}f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);f.setVisible(true);}}

六、组件

JAVA的图形界面下有两组控件,一组是awt,一组是swing。一般都是使用swing

七、面板

1、基本面板(JPanel)-import javax.swing.JPanel;

面板和JFrame一样都是容器,不过面板一般用来充当中间容器,把组件放在面板上,然后再把面板放在窗体上。一旦移动一个面板,其上面的组件,就会全部统一跟着移动,采用这种方式,便于进行整体界面的设计

JPanel p1 = new JPanel();// 设置面板大小p1.setBounds(50, 50, 300, 60);// 设置面板背景颜色p1.setBackground(Color.RED);// 这一句可以没有,因为JPanel默认就是采用的FlowLayoutp1.setLayout(new FlowLayout());JButton b1 = new JButton("英雄1");// 把按钮加入面板p1.add(b1);// 把面板加入窗口f.add(p1);

2、ContentPane

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

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而非JFrameSystem.out.println(b.getParent());

3、分板SplitPanel(split是分割的意思),做一个水平分板左边是pList,右边是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,右边是p2JSplitPane sp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, pLeft, pRight);// 设置分割条的位置sp.setDividerLocation(80);// 把sp当作ContentPanef.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 learn;import javax.swing.JFrame;import javax.swing.JTextArea;import javax.swing.JScrollPane;public class scroll {public static void main(String[] args) {JFrame f=new JFrame();f.setSize(400,300);f.setLocation(300,300);f.setLayout(null);//准备文本域JTextArea ta=new JTextArea();for(int i=0;i<10000;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、GardLayerout布局器

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<String> 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 CardLayoutcards = 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() {@Overridepublic void itemStateChanged(ItemEvent evt) {CardLayout cl = (CardLayout) (cards.getLayout());cl.show(cards, (String) evt.getItem());}}); }}

做一个进度条,进度速度慢慢变慢

package pratice;import java.awt.Dimension;import java.awt.FlowLayout;import javax.swing.JFrame;import javax.swing.JProgressBar;public class process {public static void main(String[] args) {JFrame f=new JFrame("进度条");f.setBounds(300, 300, 400, 300);f.setLayout(new FlowLayout());JProgressBar pb=new JProgressBar();pb.setPreferredSize(new Dimension(150,30));pb.setMaximum(100);pb.setValue(0);pb.setStringPainted(true);Thread t1=new Thread(){public void run(){int i=1;int t=10;while(i<=100){t=t+i;try {Thread.sleep(t);} catch (Exception e) {e.printStackTrace();}pb.setValue(i);i=i+1;}}};t1.start();f.add(pb);f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);f.setVisible(true);}}

八、菜单---GUI的菜单分为菜单栏(JMenuBar),菜单(JMenu)和菜单项(JMenuItem)

package pratice;import javax.swing.JFrame;import javax.swing.JMenuBar;import javax.swing.JMenu;import javax.swing.JMenuItem;public class JSB {public static void main(String[] args) {//容器JFrame f=new JFrame("大尖牛的青春记事本");f.setSize(500,400);f.setLocation(300, 300);//菜单栏JMenuBar mb=new JMenuBar();//菜单JMenu m1=new JMenu("0-10");JMenu m2=new JMenu("0-20");JMenu m3=new JMenu("0-30");mb.add(m1);//加入菜单栏mb.add(m2);mb.add(m3);//菜单项String s1[]={"1:我上幼儿园了","2:我上小学了,但是校长觉得我个子太低让我去幼儿园了","3:我改名字了但是不告诉你们"};for(String t1:s1){m1.add(new JMenuItem(t1));}String s2[]={"1:我终于上小学了但是个子...就长了一点","2:这时的我不开心但是我不想说","3:现在的我开始留齐刘海了哈哈哈","我上高中了没什么可说的都滚吧","4:我开始迷恋伤感文段并忠于转发"};for(String t2:s2){m2.add(new JMenuItem(t2));}m2.addSeparator();m2.add(new JMenuItem("感觉这个阶段的我指定有点毛病,可能这就是青春吧"));String s3[]={"1:我遇见了一个超级帅的男孩,可是我不敢追他","2:上大学了,军训把我晒得很黑","3:宿舍舍友聚餐,我借着酒劲涨着胆子告白了","4:我们在一起了生下了猴子"};for(String t3:s3){m3.add(new JMenuItem(t3));}// 把菜单栏加入到frame,这里用的是set而非addf.setJMenuBar(mb);f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);f.setVisible(true);}}

九、工具栏

import javax.swing.JToolBar;

package gui;import java.awt.BorderLayout;import javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JMenuItem;import javax.swing.JToolBar;public class TestGUI {public static void main(String[] args) {JFrame f = new JFrame("LoL");f.setSize(400, 300);f.setLocation(200, 200);addMenu(f);JToolBar tb = new JToolBar();JButton b1 = new JButton(new ImageIcon("e:/project/j2se/1.jpg"));JButton b2 = new JButton(new ImageIcon("e:/project/j2se/2.jpg"));JButton b3 = new JButton(new ImageIcon("e:/project/j2se/3.jpg"));JButton b4 = new JButton(new ImageIcon("e:/project/j2se/4.jpg"));JButton b5 = new JButton(new ImageIcon("e:/project/j2se/5.jpg"));JButton b6 = new JButton(new ImageIcon("e:/project/j2se/6.jpg"));tb.add(b1);tb.add(b2);tb.add(b3);tb.add(b4);tb.add(b5);tb.add(b6);b1.setToolTipText("坑爹英雄");// 禁止工具栏拖动tb.setFloatable(false);// 把工具栏放在north的位置f.setLayout(new BorderLayout());f.add(tb, BorderLayout.NORTH);f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);f.setVisible(true);}private static void addMenu(JFrame f) {JMenuBar mb = new JMenuBar();JMenu mHero = new JMenu("英雄");JMenu mItem = new JMenu("道具");JMenu mWord = new JMenu("符文");JMenu mSummon = new JMenu("召唤师");JMenu mTalent = new JMenu("天赋树");// 菜单项mHero.add(new JMenuItem("近战-Warriar"));mHero.add(new JMenuItem("远程-Range"));mHero.add(new JMenuItem("物理-physical"));mHero.add(new JMenuItem("坦克-Tank"));mHero.add(new JMenuItem("法系-Mage"));mHero.add(new JMenuItem("辅助-Support"));mHero.add(new JMenuItem("打野-Jungle"));mHero.add(new JMenuItem("突进-Charge"));mHero.add(new JMenuItem("男性-Boy"));mHero.add(new JMenuItem("女性-Girl"));mb.add(mHero);mb.add(mItem);mb.add(mWord);mb.add(mSummon);mb.add(mTalent);f.setJMenuBar(mb);}}

十、表格(JTable) 太深的没有看懂,留印象

显示一个Table需要两组数据

1. 一维数组: String[]columnNames 表示表格的标题

2. 二维数组: String[][] heros 表格中的内容

package gui;import java.awt.BorderLayout;import javax.swing.JFrame;import javax.swing.JScrollPane;import javax.swing.JTable;public class TestGUI {public static void main(String[] args) {JFrame f = new JFrame("LoL");f.setSize(400, 300);f.setLocation(200, 200);f.setLayout(new BorderLayout());String[] columnNames = new String[] { "id", "name", "hp", "damage" };String[][] heros = new String[][] { { "1", "盖伦", "616", "100" },{ "2", "提莫", "512", "102" }, { "3", "奎因", "832", "200" } };JTable t = new JTable(heros, columnNames);//默认情况下,表格的标题是不会显示出来了,除非使用了JScrollPaneJScrollPane sp = new JScrollPane(t);// 设置列宽度t.getColumnModel().getColumn(0).setPreferredWidth(10);f.add(sp, BorderLayout.CENTER);f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);f.setVisible(true);}}

十一、日期控件先忽略

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。