当前位置: 技术问答>java相关
Java中如何把控件放到自己指定的位置和大小呀,
来源: 互联网 发布时间:2015-05-04
本文导语: 要具体一点哦,本人是初学者,所以有原码最好哦! | 把Frame的layout设置为XYLayout即可把控件放到自己指定的位置和大小。 下面是Jbuilder 代码: package test; import java.awt.*; import java.awt.event.*; ...
要具体一点哦,本人是初学者,所以有原码最好哦!
|
把Frame的layout设置为XYLayout即可把控件放到自己指定的位置和大小。
下面是Jbuilder 代码:
package test;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
public class Frame1 extends JFrame {
JPanel contentPane;
XYLayout xYLayout1 = new XYLayout();
JList jList1 = new JList();
JTextField jTextField1 = new JTextField();
JTextField jTextField2 = new JTextField();
JComboBox jComboBox1 = new JComboBox();
/**Construct the frame*/
public Frame1() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
/**Component initialization*/
private void jbInit() throws Exception {
//setIconImage(Toolkit.getDefaultToolkit().createImage(Frame1.class.getResource("[Your Icon]")));
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(xYLayout1);
this.setSize(new Dimension(400, 300));
this.setTitle("Frame Title");
jTextField1.setText("jTextField1");
jTextField2.setText("jTextField2");
contentPane.add(jList1, new XYConstraints(52, 63, 86, 32));
contentPane.add(jTextField1, new XYConstraints(223, 58, 124, 31));
contentPane.add(jTextField2, new XYConstraints(234, 169, 100, 27));
contentPane.add(jComboBox1, new XYConstraints(107, 134, -1, -1));
}
/**Overridden so we can exit when window is closed*/
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
}
怎么样?
下面是Jbuilder 代码:
package test;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
public class Frame1 extends JFrame {
JPanel contentPane;
XYLayout xYLayout1 = new XYLayout();
JList jList1 = new JList();
JTextField jTextField1 = new JTextField();
JTextField jTextField2 = new JTextField();
JComboBox jComboBox1 = new JComboBox();
/**Construct the frame*/
public Frame1() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
/**Component initialization*/
private void jbInit() throws Exception {
//setIconImage(Toolkit.getDefaultToolkit().createImage(Frame1.class.getResource("[Your Icon]")));
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(xYLayout1);
this.setSize(new Dimension(400, 300));
this.setTitle("Frame Title");
jTextField1.setText("jTextField1");
jTextField2.setText("jTextField2");
contentPane.add(jList1, new XYConstraints(52, 63, 86, 32));
contentPane.add(jTextField1, new XYConstraints(223, 58, 124, 31));
contentPane.add(jTextField2, new XYConstraints(234, 169, 100, 27));
contentPane.add(jComboBox1, new XYConstraints(107, 134, -1, -1));
}
/**Overridden so we can exit when window is closed*/
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
}
怎么样?
|
楼上的,如果setLayout(null)后,每添加一个component前都要现调用
compononet.setBounds(x,y,w,h);
ok?
compononet.setBounds(x,y,w,h);
ok?
|
如果不熟悉java的布局器,你可以把layout设为null或xylayout
|
setLayout(null);
|
把Frame的layout设置为XYLayout即可把控件放到自己指定的位置和大小。
或者把Frame的layout设置为null!
如楼上!
或者把Frame的layout设置为null!
如楼上!
|
最好的方法是把控件放上去以后,自己修改系统生成的代码,在程序中控制位置和大小。
|
用gridbaglayout布局吧,也挺灵活的