我對JPanel並不是很熟悉,有點卡在一件事情上。 我想知道如何在第二行創建第二個JTextfield。 它們看起來在同一行上。第二行的Java Swing多輸入第二個JTextField
import javax.swing.*;
public class JOptionPaneMultiInput {
public static void main(String[] args) {
JTextField xField = new JTextField(5);
JTextField yField = new JTextField(5);
JPanel myPanel = new JPanel();
myPanel.add(new JLabel("x:"));
myPanel.add(xField);
myPanel.add(new JLabel("y:"));
myPanel.add(yField);
int result = JOptionPane.showConfirmDialog(null, myPanel,
"Please Enter X and Y Values", JOptionPane.OK_CANCEL_OPTION);
}
}
首先,您應該學習java中的佈局管理器https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html – Satya
提供ASCII圖形或簡單的GUI *佈局圖。看起來你想要一排標籤/值對。這可以使用'GridBagLayout'或'GroupLayout'實現最好 - 這是一個[使用'GroupLayout']的示例(http://stackoverflow.com/a/21659516/418556)。雖然我也使用嵌套面板創建了效果(在BorderLayout中有兩個'GridLayout'面板).. –
BTW - 'JTextField xField = new JTextField(5);'考慮使用帶有'SpinnerNumberModel'的'JSpinner'相反 - 用戶更喜歡選擇數字。 –