這是我的第一個GUI應用程序,我很難讓它看起來整潔。我嘗試了幾種佈局,並與它們配合,例如流量,網格,邊界。當我運行程序時,一切都混亂在一起。我遇到了Java佈局問題
我想它看起來像:
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他的建議:
您的文字圖像需要一些調整,我認爲爲了更好地顯示您所需的GUI。此外,您將需要創建併發布[sscce](http://sscce.org),以便我們更輕鬆地分析,運行和改進您的代碼。 –
當前,您正在使用JPanel的默認佈局:FlowLayout。切換到'GridBagLayout'並使用面板嵌套來簡化您的工作 –
您應該知道,通過嵌套每個都有其自己的佈局的JPanel,可以輕鬆*嵌套*佈局。也許你可以通過讓你的主JPanel使用BoxLayout,然後使用使用GridBagLayout的內部JPanel來達到你想要的效果。 –