setLayout(null)的意思和JLabel设置背景色

来源:互联网 发布:淘宝优惠券模板代码 编辑:程序博客网 时间:2024/04/30 03:10

使用setlayout(null)后,就是不用java提供的一些布局,用像素坐标的形式来布局面板 ,可以用setbounds的形式 来确定哪个组件在什么位置,它不随面板大小的改变而改变。注意:setbounds只有在设置了setlayout(null)后才能启作用。

官方解读地址:Doing Without a Layout Manager(Absolute Positioning)绝对定位

http://docs.oracle.com/javase/tutorial/uiswing/layout/none.html

1、主要内容

通过Swing编写图形用户界面

2、Swing控件编程

用Swing编写图形用户界面主要涉及JFrame, JPanel, JButton等等类。其中,JFrame是界面布局的桌子;JPanel是放置控件的幕布,铺在JFrame上将空间分成若干块;JButton等控件添加到JPanel的指定位置中。

2.1JFrame相关方法

setTitle("")设置窗口名称

setBounds()设置窗口位置和尺寸

setLayout(null)关闭窗口布局管理器使得后面的setBounds生效

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)定义窗口关闭事件

setResizable(false)禁用最大化

getContentPane()返回窗体的contentPane对象

getContentPane().add(JPanel)添加JPanel容器

2.2 JPanel相关方法

setBounds()设置窗口位置和尺寸,在JFrame内的相对位置

setLayout(null)关闭窗口布局管理器使得控件的setBounds生效

add()添加控件

2.3 JButton等控件

setBounds()设置窗口位置和尺寸,在JPanel内的相对位置

 

代码如下:

[java] view plaincopy
  1. public class mainFrame extends JFrame{  
  2.   
  3.     public TextArea revArea;  
  4.     public JButton button1;  
  5.     public JButton button2;  
  6.     public JButton button3;  
  7.     public JButton button4;  
  8.     public JButton button5;  
  9.     public JButton button6;  
  10.       
  11.       
  12.     public mainFrame() {  
  13.           
  14.         this.setTitle("服务器");  
  15.         this.setBounds(300200500500);  
  16.         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  17.         this.setLayout(null);  
  18.         this.setResizable(false);  
  19.           
  20.         JPanel panel_1 = new JPanel();  
  21.         panel_1.setLocation(35020);  
  22.         panel_1.setSize(100300);  
  23.         panel_1.setLayout(null);  
  24.           
  25.         button1 = new JButton("Exp 1");  
  26.         button1.setBounds(0010030);  
  27.         button2 = new JButton("Exp 2");  
  28.         button2.setBounds(05010030);  
  29.         button3 = new JButton("Exp 3");  
  30.         button3.setBounds(010010030);  
  31.         button4 = new JButton("Exp 4");  
  32.         button4.setBounds(015010030);  
  33.         button5 = new JButton("保存");  
  34.         button5.setBounds(020010030);  
  35.         button6 = new JButton("任务取消");  
  36.         button6.setBounds(025010030);  
  37.           
  38.         panel_1.add(button1);  
  39.         panel_1.add(button2);  
  40.         panel_1.add(button3);  
  41.         panel_1.add(button4);  
  42.         panel_1.add(button5);  
  43.         panel_1.add(button6);  
  44.           
  45.           
  46.         this.getContentPane().add(panel_1);  
  47.           
  48.         revArea = new TextArea();  
  49.         revArea.setBounds(1010280440);  
  50.           
  51.         this.getContentPane().add(revArea);  
  52.           
  53.         Event();  
  54.           
  55.     }  


 

3、Swing的并发

当一个Swing控件触发一个长期运行任务时,那么这个长期运行任务就占用了事件分发线程(图形界面的线程),导致按钮无法立刻弹起来。解决方法是在响应触发时分配单独的线程执行长期运行的任务。可以使用单线程的Executor线程池,它自动将待处理任务排队,然后按顺序执行。

[java] view plaincopy
  1. button2.addActionListener(new ActionListener() {  
  2.       
  3.     public void actionPerformed(ActionEvent e) {  
  4.         revArea.append("实验二\n");  
  5.         executor.execute(new Runnable() {  
  6.             public void run() {  
  7.                 c.Experment_2(file);  
  8.             }  
  9.         });  
  10.     }  
  11.       
  12. });  

其中,c.Experment_2()是一个长期运行任务。

[java] view plaincopy
  1. public ExecutorService executor = Executors.newSingleThreadExecutor();  

4、遗留的问题

在实际问题中遇到了如何异步终止线程的问题。

一般的方法是通过外部方法改变control变量的值来终止线程

[java] view plaincopy
  1. while (control) {  
  2.   
  3. }  

但是在有阻塞IO或等待锁时,无法用这种方法来终止。

本文转自:http://blog.csdn.net/zjgtan/article/details/10252759


JLabel设置背景色

由于 Jlabel 默认是透明的,所以直接通过setBackground(color)设置背景色是看不到效果的,如果想看到效果,需要首先设置该JLabel不透明,就可以了。 

Java代码  收藏代码
  1. JLabel tips = new JLabel("正在执行,请稍后...");  
  2. tips.setOpaque(true);//设置组件JLabel不透明,只有设置为不透明,设置背景色才有效  
  3. tips.setBackground(ColorUtil.LU_DOU_SHA);  

  注:opaque [o'pek]  中文意思:不透明的


原创粉丝点击