站内搜索: 请输入搜索关键词

当前页面: 开发资料首页J2SE 专题关于java中GridBagLayout的问题

关于java中GridBagLayout的问题

摘要: 关于java中GridBagLayout的问题


import java.awt.*;
import javax.swing.*;

public class Mytest
{
public static void main(String[] args)
{
MytestFrame frame = new MytestFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

class MytestFrame extends JFrame
{
public MytestFrame()
{
setSize(400, 300);

Container contentPane = getContentPane();
GridBagLayout layout = new GridBagLayout();
GridBagConstraints cons = new GridBagConstraints();

contentPane.setLayout(layout);

JButton button1 = new JButton("Button1");
cons.fill = GridBagConstraints.BOTH;
cons.gridwidth = GridBagConstraints.REMAINDER;

contentPane.add(button1, cons);


JButton button2 = new JButton("Button2");
cons.gridwidth = GridBagConstraints.RELATIVE;
contentPane.add(button2, cons);

JButton button3 = new JButton("Button3");
cons.gridwidth = GridBagConstraints.REMAINDER;
contentPane.add(button3, cons);

JButton button4 = new JButton("Button4");
cons.gridwidth = 1;
contentPane.add(button4, cons);

JButton button5 = new JButton("Button5");
cons.gridwidth = 1;
contentPane.add(button5, cons);

JButton button6 = new JButton("Button6");
cons.gridwidth = GridBagConstraints.REMAINDER;
contentPane.add(button6, cons);
}
}

我想第一排只显示一个,第二排显示两个(长度相等),第三排三个平分。都是按钮,为什么老实现不了呢,请高手解决,真的谢谢了~~~~~


不用高手,我来吧
import java.awt.*;
import javax.swing.*;

public class Mytest
{
public static void main(String[] args)
{
MytestFrame frame = new MytestFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

class MytestFrame extends JFrame
{
public MytestFrame()
{
setSize(400, 300);

Container contentPane = getContentPane();
GridBagLayout layout = new GridBagLayout();
GridBagConstraints cons = new GridBagConstraints();

contentPane.setLayout(layout);

cons.gridx = 1;
cons.gridy = 1;
contentPane.add(new JButton("Button1"), cons);

cons.gridx = 1;
cons.gridy = 2;
contentPane.add(new JButton("Button2"), cons);

cons.gridx = 2;
cons.gridy = 2;
contentPane.add(new JButton("Button3"), cons);

cons.gridx = 1;
cons.gridy = 3;
contentPane.add(new JButton("Button4"), cons);

cons.gridx = 2;
cons.gridy = 3;
contentPane.add(new JButton("Button5"), cons);

cons.gridx = 3;
cons.gridy = 3;
contentPane.add(new JButton("Button6"), cons);
}
}


上面没有说清楚,我是想把几个按钮排成一个正三角形的,谢谢楼上的啦


二楼的做法中只要给每个 按钮中加入 cons.anchor=XXXX 设置它们的 锚点就可以了 锚点的 值api里面有静态的定义 楼主去查一下就知道了 比如第一行的 可以设置成 cons.anchor= GridBagConstraints.CENTER; 第二行的 设置成 cons.anchor= GridBagConstraints.WEST;长度设置按照楼主的方法应该就行了


照楼上的做了,第二行显示还是不正常。
显示结果如下:
http://www.downgle.com/files/0610/test11111.jpg



这样?时间紧 没细看 可能有问题
import java.awt.*;
import javax.swing.*;

public class Mytest
{
public static void main(String[] args)
{
MytestFrame frame = new MytestFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

class MytestFrame extends JFrame
{
public MytestFrame()
{
setSize(400, 300);

Container contentPane = getContentPane();
GridBagLayout layout = new GridBagLayout();
GridBagConstraints cons = new GridBagConstraints();

contentPane.setLayout(layout);

cons.gridx = 3;
cons.gridy = 1;
cons.gridwidth = 3;
contentPane.add(new JButton("Button1"), cons);

cons.gridx = 2;
cons.gridy = 2;
cons.gridwidth = 3;
contentPane.add(new JButton("Button2"), cons);

cons.gridx = 4;
cons.gridy = 2;
cons.gridwidth = 3;
contentPane.add(new JButton("Button3"), cons);

cons.gridx = 1;
cons.gridy = 3;
cons.gridwidth = 3;
contentPane.add(new JButton("Button4"), cons);

cons.gridx = 3;
cons.gridy = 3;
cons.gridwidth = 3;
contentPane.add(new JButton("Button5"), cons);

cons.gridx = 5;
cons.gridy = 3;
cons.gridwidth = 3;
contentPane.add(new JButton("Button6"), cons);
}
}


↑返回目录
前一篇: java重定向后如何再次重定向
后一篇: 我编写了一个弱智级小程序.请前辈们帮我看一下!