摆动组件之间的新线 [英] New Line Between Swing Components

查看:102
本文介绍了摆动组件之间的新线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试多次搜索如何多次格式化Java swing元素,但似乎找不到任何东西.我已经尝试学习网格袋布局,但是它太复杂了.有人可以告诉我如何使用Swing来间隔部件吗?谢谢!

I have tried searching multiple time on how to format java swing elements multiple times, but I can't seem to find anything. I have tried to learn the grid bag layout but it is all too complicated. Could someone please tell me how to space swing components with Swing? Thanks!

编辑

代码:

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

public class Format_Swing_Elemnts {
 public static void main(String[] args) {
JLabel title = new JLabel("Bridge Settings:");
title.setFont(font);
JLabel l1 = new JLabel("Number of Bars: "); 
JLabel l2 = new JLabel("Deck Elevation: "); 
JLabel l3 = new JLabel("Height of arch: "); 
JLabel l4 = new JLabel("Height of Pier: "); 
JLabel l5 = new JLabel("Cabel Anchorage: ");    
JLabel l6 = new JLabel("Concrete Type: ");  
final JLabel lM1 = new JLabel("meters");
final JLabel lM2 = new JLabel("meters");
final JLabel lM3 = new JLabel("meters");
final JRadioButton p1RB1 = new JRadioButton("Standard Abutments");
final JRadioButton p1RB2 = new JRadioButton("Arch Abutments");
final JRadioButton p2RB1 = new JRadioButton("No Pier (one span)");
final JRadioButton p2RB2 = new JRadioButton("Pier (two spans)");
final JRadioButton p3RB1 = new JRadioButton("No Cable Anchorage");
final JRadioButton p3RB2 = new JRadioButton("One Cable Anchorage");
final JRadioButton p3RB3 = new JRadioButton("Two Cable Anchorage");
final JRadioButton p4RB1 = new JRadioButton("Meduim Strength Concrete");
final JRadioButton p4RB2 = new JRadioButton("High Strength Concrete");
ButtonGroup p1 = new ButtonGroup();
p1.add(p1RB1);
p1.add(p1RB2);
ButtonGroup p2 = new ButtonGroup();
p2.add(p2RB1);
p2.add(p2RB2);
ButtonGroup p3 = new ButtonGroup();
p3.add(p3RB1);
p3.add(p3RB2);
p3.add(p3RB3);
ButtonGroup p4 = new ButtonGroup();
p4.add(p4RB1);
p4.add(p4RB2);

final SpinnerModel model = new SpinnerNumberModel(0, 0, 1000, 1);
final JSpinner numberBars = new JSpinner(model);
final JSpinner deckElevation = new JSpinner(new SpinnerNumberModel(24, 0, 24, 4));
final JSpinner archHeight = new JSpinner(new SpinnerNumberModel(4, 4, 24, 4));
final JSpinner pierHeight = new JSpinner(new SpinnerNumberModel(0, 0, 24, 4));
JFormattedTextField tf1 =       ((JSpinner.DefaultEditor)deckElevation.getEditor()).getTextField();
tf1.setEditable(false);
JFormattedTextField tf2 =  ((JSpinner.DefaultEditor)archHeight.getEditor()).getTextField();
tf2.setEditable(false);
JFormattedTextField tf3 = ((JSpinner.DefaultEditor)pierHeight.getEditor()).getTextField();
tf3.setEditable(false);

JButton quit = new JButton("Quit");
JButton continueB = new JButton("Continue");

JPanel format2 = new JPanel();

format2.add(title);
//New line
format2.add(l1);
format2.add(numberBars);
//New line
format2.add(l2);
format2.add(deckElevation);
format2.add(lM1);
//New line
format2.add(p1RB1);
format2.add(p1RB2);
format2.add(l3);
format2.add(archHeight);
format2.add(lM2);
//New line
format2.add(p2RB1);
format2.add(p2RB2);
format2.add(l4);
format2.add(pierHeight);
format2.add(lM3);
//New line
format2.add(l5);
format2.add(p3RB1);
format2.add(p3RB2);
format2.add(p3RB3);
//New line
format2.add(l6);
format2.add(p4RB1);
format2.add(p4RB2);
//New line
format2.add(quit);
format2.add(continueB);

JFrame part2Window = new JFrame("Part 2 - Adjust your settings");
part2Window.add(format2);
part2Window.setSize(290, 365);
part2Window.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
part2Window.setVisible(true);
part2Window.setLocationRelativeTo(null);
part2Window.setResizable(false);
part2Window.setAlwaysOnTop(true);
 }
}

推荐答案

我还发现了使用网格布局的一种简单方法,该方法比Mig布局简单得多.对不起,麻烦了!

I have also found an easier way using the Grid Layout that is much simpler than the Mig Layout. Sorry for the trouble!

这篇关于摆动组件之间的新线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆