使用网格包布局设计java框架
我必须使用网格袋布局设计JFrame中的按钮。因此,所有系统的定位可能相同。我正在使用以下代码,但我想对按钮进行分组。使用网格包布局设计java框架,java,swing,awt,grid-layout,gridbaglayout,Java,Swing,Awt,Grid Layout,Gridbaglayout,我必须使用网格袋布局设计JFrame中的按钮。因此,所有系统的定位可能相同。我正在使用以下代码,但我想对按钮进行分组。 我想设计按钮,这样可以创建三个组,每个组有4个按钮 import java.awt.GridLayout; import javax.swing.JButton; import javax.swing.JFrame; public class GridLayoutTest { public static void main(String[]
我想设计按钮,这样可以创建三个组,每个组有4个按钮
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class GridLayoutTest {
public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("GridLayout Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridLayout(3, 2));
frame.add(new JButton("Button 1"));
frame.add(new JButton("Button 2"));
frame.add(new JButton("Button 3"));
frame.add(new JButton("Button 4"));
frame.add(new JButton("Button 5"));
frame.add(new JButton("Button 6"));
frame.add(new JButton("Button 7"));
frame.add(new JButton("Button 8"));
frame.pack();
frame.setVisible(true);
}
}
所以,有点像
public class BadLayout04 {
public static void main(String[] args) {
new BadLayout04();
}
public BadLayout04() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new ButtonsPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class ButtonsPane extends JPanel {
public ButtonsPane() {
JPanel group1 = createGroup(1);
JPanel group2 = createGroup(5);
JPanel group3 = createGroup(9);
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
add(group1, gbc);
gbc.gridx = 1;
add(group2, gbc);
gbc.gridx = 0;
gbc.gridy++;
gbc.gridwidth = GridBagConstraints.REMAINDER;
add(group3, gbc);
}
public JPanel createGroup(int index) {
JPanel group = new JPanel(new GridBagLayout());
group.setBorder(new EmptyBorder(4, 4, 4, 4));
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
for (int loop = 0; loop < 4; loop++) {
group.add(new JButton("Button " + (index++)), gbc);
}
return group;
}
}
}
公共类BadLayout04{
公共静态void main(字符串[]args){
新的BadLayout04();
}
公共布局04(){
invokeLater(新的Runnable(){
@凌驾
公开募捐{
试一试{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(ClassNotFoundException |实例化Exception | IllegalacessException |不支持ookandfeelException ex){
}
JFrame=新JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(新的BorderLayout());
frame.add(新按钮平面());
frame.pack();
frame.setLocationRelativeTo(空);
frame.setVisible(true);
}
});
}
公共类按钮面板扩展JPanel{
公共按钮板(){
JPanel group1=createGroup(1);
JPanel group2=createGroup(5);
JPanel group3=createGroup(9);
setLayout(新的GridBagLayout());
GridBagConstraints gbc=新的GridBagConstraints();
gbc.gridx=0;
gbc.gridy=0;
添加(第1组,gbc);
gbc.gridx=1;
添加(第2组,gbc);
gbc.gridx=0;
gbc.gridy++;
gbc.gridwidth=GridBagConstraints.rements;
添加(第3组,gbc);
}
公共JPanel createGroup(int索引){
JPanel group=newjpanel(newgridbaglayout());
分组顺序(新的空顺序(4,4,4,4));
GridBagConstraints gbc=新的GridBagConstraints();
gbc.fill=GridBagConstraints.BOTH;
for(int-loop=0;loop<4;loop++){
添加(新的JButton(“按钮”+(索引++)),gbc);
}
返回组;
}
}
}
所以,像
public class BadLayout04 {
public static void main(String[] args) {
new BadLayout04();
}
public BadLayout04() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new ButtonsPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class ButtonsPane extends JPanel {
public ButtonsPane() {
JPanel group1 = createGroup(1);
JPanel group2 = createGroup(5);
JPanel group3 = createGroup(9);
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
add(group1, gbc);
gbc.gridx = 1;
add(group2, gbc);
gbc.gridx = 0;
gbc.gridy++;
gbc.gridwidth = GridBagConstraints.REMAINDER;
add(group3, gbc);
}
public JPanel createGroup(int index) {
JPanel group = new JPanel(new GridBagLayout());
group.setBorder(new EmptyBorder(4, 4, 4, 4));
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
for (int loop = 0; loop < 4; loop++) {
group.add(new JButton("Button " + (index++)), gbc);
}
return group;
}
}
}
公共类BadLayout04{
公共静态void main(字符串[]args){
新的BadLayout04();
}
公共布局04(){
invokeLater(新的Runnable(){
@凌驾
公开募捐{
试一试{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(ClassNotFoundException |实例化Exception | IllegalacessException |不支持ookandfeelException ex){
}
JFrame=新JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(新的BorderLayout());
frame.add(新按钮平面());
frame.pack();
frame.setLocationRelativeTo(空);
frame.setVisible(true);
}
});
}
公共类按钮面板扩展JPanel{
公共按钮板(){
JPanel group1=createGroup(1);
JPanel group2=createGroup(5);
JPanel group3=createGroup(9);
setLayout(新的GridBagLayout());
GridBagConstraints gbc=新的GridBagConstraints();
gbc.gridx=0;
gbc.gridy=0;
添加(第1组,gbc);
gbc.gridx=1;
添加(第2组,gbc);
gbc.gridx=0;
gbc.gridy++;
gbc.gridwidth=GridBagConstraints.rements;
添加(第3组,gbc);
}
公共JPanel createGroup(int索引){
JPanel group=newjpanel(newgridbaglayout());
分组顺序(新的空顺序(4,4,4,4));
GridBagConstraints gbc=新的GridBagConstraints();
gbc.fill=GridBagConstraints.BOTH;
for(int-loop=0;loop<4;loop++){
添加(新的JButton(“按钮”+(索引++)),gbc);
}
返回组;
}
}
}
您的标题是GridBagLayout,但您的代码使用的是GridBagLayout。它们是非常不同的东西。你在问什么?为什么你必须使用GridBagLayout而只使用GridBagLayout?为什么不嵌套每个使用自己的布局管理器的JPanel?“我想设计按钮,这样可以创建三个组,每个组有4个按钮”,这些组应该如何布局?并排、水平、圆形或三角形?布局应该像上面两组和下面一组。我必须使用gridbaglayout来排列按钮,因为按钮可能有不同的大小。您的标题是gridbaglayout,但您的代码使用gridbaglayout。它们是非常不同的东西。你在问什么?为什么你必须使用GridBagLayout而只使用GridBagLayout?为什么不嵌套每个使用自己的布局管理器的JPanel?“我想设计按钮,这样可以创建三个组,每个组有4个按钮”,这些组应该如何布局?并排地,水平地,在圆形或三角形中?布局应该像上面两组和下面一组。我必须使用gridbaglayout来安排按钮,因为按钮可能有不同的大小。当然,非常感谢,现在我将根据我的要求定制它。问一个问题我有一个主菜单表单,它的主体中也有按钮因此,我在netbeans中使用绝对布局设计了另一个jinternalframe,我在mainmenuform上添加了jinternalframe,但问题是jinternalframe有标题栏,而且它也是可移动的。我可以消除这个毫无意义的问题吗。你有一个例子吗?当然,非常感谢,现在我将根据我的要求定制。还有一个问题我有