1

这是我的第一个 GUI 应用程序,我无法使它看起来整洁。我尝试了几种布局并对其进行了修改,例如流、网格、边框。当我运行程序时,一切都混杂在一起。

我希望它看起来像:

Unloaded Measurement      |textfield| |textfield|
Loaded Rider Measurement  |textField| |textfield|
Loaded Bike Measurement   |textField| |Textfield|

                   |Button|
_______________________________________________________________________________________

Race Sag: |TextField|
Free Sag: |TextField|
Race Sag Notes: |       TextField         |
Free Sag Notes: |       TextField         |

这是一个屏幕截图,可帮助您了解我的问题: 截屏

顶部用于用户输入,底部用于计算输出。我希望我已经提供了足够的细节来帮助解决这个问题。我真的很感谢任何提供帮助的人!这是我的代码:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.StringTokenizer;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class Main extends JFrame {

private static final long serialVersionUID = 1L;
private JPanel panel;
private JLabel messageLabel;
private JLabel messageLabel1;
private JLabel messageLabel2;
private JLabel raceSagLabel;
private JLabel freeSagLabel;
private JTextField wholeTextField;
private JTextField wholeTextField1;
private JTextField wholeTextField2;
private JTextField fracTextField;
private JTextField fracTextField1;
private JTextField fracTextField2;
private JTextField raceSagText;
private JTextField freeSagText;
private JTextField noteText;
private JTextField noteText1;
private JButton calcButton;
private final int WINDOW_WIDTH = 575;
private final int WINDOW_HEIGHT = 400;


/*===============================================================================
    Project : test.java - SagCalculator
    Author  : Brian Green
    Date    : Jan 10, 2013
    Abstract:   

  ===============================================================================*/
public static void main(String[] args) {
    @SuppressWarnings("unused")
    Main sc = new Main();
}
public Main(){
    setTitle("Rider Sag Calculator");
    setSize(WINDOW_WIDTH, WINDOW_HEIGHT); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //setLayout();
    buildPanel();
    add(panel);
    setVisible(true);
}
private void buildPanel(){
    messageLabel = new JLabel("Enter Unloaded Stand Measurement");
    messageLabel1 = new JLabel("Enter Loaded with Rider Measurement");
    messageLabel2 = new JLabel("Enter Loaed without Rider Measurement");
    wholeTextField = new JTextField(3);
    fracTextField = new JTextField(3);
    wholeTextField1 = new JTextField(3);
    fracTextField1 = new JTextField(3);
    wholeTextField2 = new JTextField(3);
    fracTextField2 = new JTextField(3);
    calcButton = new JButton("Calculate");
    raceSagLabel = new JLabel("Race Sag: ");
    raceSagText = new JTextField(5);
    freeSagLabel = new JLabel("Free Sag: ");
    freeSagText = new JTextField(5);
    noteText = new JTextField(30);
    noteText1 = new JTextField(30);

    calcButton.addActionListener(new CalcButtonListener());
    panel = new JPanel();

    panel.add(messageLabel);
    panel.add(wholeTextField);
    panel.add(fracTextField);
    panel.add(messageLabel1);
    panel.add(wholeTextField1);
    panel.add(fracTextField1);
    panel.add(messageLabel2);
    panel.add(wholeTextField2);
    panel.add(fracTextField2);
    panel.add(calcButton);  
    panel.add(raceSagLabel);
    panel.add(raceSagText);
    panel.add(freeSagLabel);
    panel.add(freeSagText);
    panel.add(noteText);
    panel.add(noteText1);



}

如果由于某种原因您需要查看更多代码,请告诉我。我很乐意提供。谢谢您的帮助!

我把这一切都解决了!我要感谢@trashgod 的建议: 在此处输入图像描述

4

3 回答 3

2

使用此处的其他一些建议(包括 GridBagLayout)可以实现您想要的布局,但迄今为止最简单的解决方案是使用JGoodies FormLayout,因为它明确设计用于创建需要排列字段和标签的布局.

于 2013-01-12T17:38:02.573 回答
2

Try calling pack() on your JFrame, the layout doesn't get applied immediately when you set it. Alternatively, you can use validate(), which should lay out the component as well. Btw. in your sample code you don't set a layout manager.

于 2013-01-12T15:13:04.143 回答
1

GroupLayout,请参见此处,在带标签的表单上做得很好,并且可以作为子面板进行管理。

图片

于 2013-01-12T17:44:28.250 回答