Java interface buttons adaptive window size

How about the components on the Java window adaptive window size of the problem:

And the establishment of a panel window

JFrame J=new JFrame("这是窗口");
JPanel p=new JPanel();
J.add(p);
//界面设置
J.setBounds(300, 200, 400, 500);
J.setVisible(true);
J.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Create a text box and a button

JButton button=new JButton("这是按钮");
JJTextArea area=new JTextArea("这是文本框");
p.add(button);
p.add(area);

 Note that the focus here, is to set the size of the component to take over the method I used is to add a listener for the window, listening to the window size, and size of the component to be defined in accordance with the size of the window. As a result, we found that when the listener changes the window size will change the setBounds () method parameters, the size can be achieved and the window assembly can be consistent.

                J.addComponentListener(new ComponentListener() {
			//当窗口大小改变时
			public void componentResized(ComponentEvent e) {
                                //获取窗口的大小
			        Dimension d = J.getSize();

                                //设置组件的大小
                                button.setBounds(0, 0, d.width, height-20);
                                area.setBounds(0, height-50, d.width, height-20);
			}

			//当窗口位置改变时
			public void componentMoved(ComponentEvent e) {
				// TODO Auto-generated method stub
				
			}

			//当窗口可见时
			public void componentShown(ComponentEvent e) {
				// TODO Auto-generated method stub
				
			}

			//当窗口不可见时
			public void componentHidden(ComponentEvent e) {
				// TODO Auto-generated method stub
				
			}
		});

 

 

 

Published 88 original articles · won praise 47 · views 10000 +

Guess you like

Origin blog.csdn.net/dai_ma_dong/article/details/103180555