java绝对定位布局代码 java位置代码

Java怎样把表格放在绝对定位的面板上空布局 , 绝对定位,需要在代码中设置组件的位置和大小 。
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
public class TestWin extends JFrame{
public TestWin() {
Object[][]data= https://www.04ip.com/post/{{1,2,3},{1,2,3}};
String[]name= {"一","二","三"};
【java绝对定位布局代码 java位置代码】JTable table=new JTable(data,name);
JScrollPane scrollPane=new JScrollPane(table);//必须把JTable放在JScrollPane中,否则没有滚动条且表头无法正常显示
scrollPane.setBounds(60,20,300,100);//然后设置scrollPane的bounds,使用null布局,必须在代码中设置组件的位置和大小
JPanel pane=new JPanel(null);
pane.add(scrollPane);//把scrollPane添加到null布局的面板中
this.add(pane);
this.setSize(400, 300);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[]args) {
SwingUtilities.invokeLater(()-new TestWin());
}
}
运行结果如图
java gui中怎么用jpanel实现组件的绝对定位相对定位是利用布局管理器GridBagLayout() 来实现的GridBagLayout 布局是根据GridBagConstraints() 来设定的GridBagConstraints主要有8个重要参数需要掌握非别是
gridx,gridy——设置组件的位置 ,
gridx设置为GridBagConstraints.RELATIVE代表此组件位于之前所加入组件的右边 。
gridy设置为GridBagConstraints.RELATIVE代表此组件位于以前所加入组件的下面 。
建议定义出gridx,gridy的位置以便以后维护程序 。gridx=0,gridy=0时放在0行0列 。
gridwidth,gridheight——用来设置组件所占的单位长度与高度,默认值皆为1 。
你可以使用GridBagConstraints.REMAINDER常量,代表此组件为此行或此列的最后一个组件,而且会占据所有剩余的空间 。
weightx,weighty——用来设置窗口变大时 , 各组件跟着变大的比例 。
当数字越大,表示组件能得到更多的空间 , 默认值皆为0 。
anchor——当组件空间大于组件本身时,要将组件置于何处 。
有CENTER(默认值)、NORTH、NORTHEAST、EAST、SOUTHEAST、WEST、NORTHWEST选择 。
insets——设置组件之间彼此的间距 。
它有四个参数 , 分别是上,左,下,右,默认为(0,0,0,0) 。
ipadx,ipady——设置组件间距,默认值为0 。
GridBagLayout里的各种设置都必须通过GridBagConstraints,因此当我们将GridBagConstraints的参数都设置
好了之后,必须new一个GridBagConstraints的对象出来,以便GridBagLayout使用 。
构造函数:
GirdBagLayout()建立一个新的GridBagLayout管理器 。
GridBagConstraints()建立一个新的GridBagConstraints对象 。
GridBagConstraints(int gridx,int gridy,
int gridwidth,int gridheight,
double weightx,double weighty,
int anchor,int fill, Insets insets,
int ipadx,int ipady)建立一个新的GridBagConstraints对象 , 并指定其参数的值
下面是我的例程:
实现
import javax.swing.*;
import java.util.*;
import java.awt.*;
public class Example extends JFrame{
private void makeComponent(GridBagLayout gbLaout,GridBagConstraints constraints,Component component)
{
gbLaout.setConstraints(component, constraints);
add(component);
}
public static void main(String args[]) {
Example exp=new Example();
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
exp.setLayout(gridbag);
JButton jb1=new JButton("JButton1");
c.gridx=0;
c.gridy=0;
c.gridwidth=1;
c.gridheight=1;
c.weightx=1;
c.weighty=0;
c.anchor=GridBagConstraints.CENTER;
c.fill=GridBagConstraints.HORIZONTAL;
c.insets=new Insets(0,0,0,0);
c.ipadx=0;
c.ipady=0;
exp.makeComponent(gridbag,c,jb1);
JButton jb2=new JButton("JButton2");
c.gridx=1;
exp.makeComponent(gridbag,c,jb2);
JButton jb3=new JButton("JButton2");
c.gridx=2;
exp.makeComponent(gridbag,c,jb3);
JButton jb4=new JButton("JButton2");
c.gridx=3;
exp.makeComponent(gridbag,c,jb4);
JButton jb5=new JButton("JButton5");
c.gridx=0;
c.gridy=1;
c.gridheight=1;
c.gridwidth=4;
exp.makeComponent(gridbag,c,jb5);
JButton jb6=new JButton("JButton6");
c.gridx=0;
c.gridy=2;
c.gridwidth=3;
exp.makeComponent(gridbag,c,jb6);
JButton jb7=new JButton("JButton7");
c.gridx=3;
c.gridy=2;
c.gridwidth=1;
exp.makeComponent(gridbag,c,jb7);
JButton jb8=new JButton("JButton8");
c.gridx=0;
c.gridy=3;
c.gridwidth=1;
c.gridheight=2;
c.fill=GridBagConstraints.BOTH;
exp.makeComponent(gridbag,c,jb8);
JButton jb9=new JButton("JButton9");
c.gridx=1;
c.gridy=3;
c.gridwidth=3;
c.gridheight=1;
c.fill=GridBagConstraints.HORIZONTAL;
exp.makeComponent(gridbag,c,jb9);
JButton jb10=new JButton("JButton10");
c.gridx=1;
c.gridy=4;
c.gridwidth=3;
c.gridheight=1;
c.fill=GridBagConstraints.HORIZONTAL;
exp.makeComponent(gridbag,c,jb10);
exp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
exp.setSize(500,500);
exp.setVisible(true);
}
}
Java中如何给JButton/JLabel定位置?【高分悬赏】setLocation
public void setLocation(int x,
int y)将组件移到新位置 。通过此组件父级坐标空间中的 x 和 y 参数来指定新位置的左上角 。
参数:
x - 父级坐标空间中新位置左上角的 x 坐标
y - 父级坐标空间中新位置左上角的 y 坐标
setLayout会覆盖setLocation行为,
setLocation()不能保证跨平台的界面一致性
setLocation的X,Y坐标不是画面上的,
下边是给你改的代码.用setBounds来设置坐标及大小.
-------------------------------------------------------------
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class GUI {
private static JFrame frame = new JFrame("GUI test");
private static JPanel panel = new JPanel();
private static JLabel label = new JLabel("GUI label test");
private static JButton button = new JButton("Button1");;
public static void main(String[] args) {
frame.setLayout(null);
panel.setLayout(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 768);
panel.add(button);
// button.setLocation(100, 100);
button.setBounds(100, 100, 160, 24);
panel.add(label);
// label.setLocation(50, 50);
panel.setBounds(0, 0, 700, 700);
frame.add(panel);
frame.setVisible(true);
}
}
java绝对定位布局代码的介绍就聊到这里吧 , 感谢你花时间阅读本站内容,更多关于java位置代码、java绝对定位布局代码的信息别忘了在本站进行查找喔 。

    推荐阅读