現在我想擁有對方旁邊的兩個文本字段與上述各描述該文本框做什麼 不同的標籤。到 實現這個我已經把他們放在一個GridLayout(2,2)。這是 最好的方法嗎?這是我知道的只有在另一個組件上直接標註標籤的唯一方法。有沒有更好的辦法?如果只有一個按鈕上方有一個標籤 ,怎麼辦?通過一個 GridLayout(2,1)來定位它是否明智?
我自己,我總是通過嵌套面板與BorderLayout
做到這一點。例如:
JFrame frame = new JFrame("The Title");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panOuter = new JPanel(new BorderLayout());
JPanel panLeft = new JPanel(new BorderLayout());
panLeft.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
JPanel panRight = new JPanel(new BorderLayout());
panRight.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
panOuter.add(panLeft, BorderLayout.WEST);
panOuter.add(panRight, BorderLayout.EAST);
JLabel lblLeft = new JLabel("Label 1", JLabel.CENTER);
JLabel lblRight = new JLabel("Label 2", JLabel.CENTER);
JTextField txtLeft = new JTextField(10);
JTextField txtLright = new JTextField(10);
panLeft.add(lblLeft, BorderLayout.NORTH);
panLeft.add(txtLeft, BorderLayout.CENTER);
panRight.add(lblRight, BorderLayout.NORTH);
panRight.add(txtLright, BorderLayout.CENTER);
frame.setContentPane(panOuter);
frame.pack();
frame.setVisible(true);
需要注意的是,你可以操縱的部件之間的間隙與設置空邊界。此外,您可以使用BorderLayout.LINE_START
和BorderLayout.LINE_END
而不是使用BorderLayout.WEST
和BorderLayout.EAST
,這將增加對RTL語言(例如阿拉伯語)的支持。
這使我對我的下一個問題。 與上面相同的用戶界面的最佳方式是什麼,但是其中的另一個組件(按鈕)居中。 本質上,用戶界面應由兩個帶有 計算按鈕的命名文本字段組成。我這樣做的方法是將上述 組件放在一個面板中,然後將計算按鈕加上帶有GridLayout(2,1)的 周圍面板。問題是, 按鈕變得像它上面的面板一樣大(我假設)。我怎樣才能調整這個,並仍然在 的textfields/labels面板下完全對齊按鈕?
我會通過嵌套面板做到這一點,因爲我先前做的,但現在的底部面板上有一個FlowLayout
佈局管理器以獲取按鈕大小適中:
JFrame frame = new JFrame("The Title");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panOuter = new JPanel(new BorderLayout());
JPanel panLeft = new JPanel(new BorderLayout());
panLeft.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
JPanel panRight = new JPanel(new BorderLayout());
panRight.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
JPanel panBottom = new JPanel(); // default is FlowLayout
panBottom.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
panOuter.add(panLeft, BorderLayout.WEST);
panOuter.add(panRight, BorderLayout.EAST);
panOuter.add(panBottom, BorderLayout.SOUTH);
JLabel lblLeft = new JLabel("Label 1", JLabel.CENTER);
JLabel lblRight = new JLabel("Label 2", JLabel.CENTER);
JTextField txtLeft = new JTextField(10);
JTextField txtLright = new JTextField(10);
JButton btnBottom = new JButton("Press it!");
panLeft.add(lblLeft, BorderLayout.NORTH);
panLeft.add(txtLeft, BorderLayout.CENTER);
panRight.add(lblRight, BorderLayout.NORTH);
panRight.add(txtLright, BorderLayout.CENTER);
panBottom.add(btnBottom);
frame.setContentPane(panOuter);
frame.pack();
frame.setVisible(true);
與文本區域上方的標籤類似。標籤應該很小,但是 對於文本區域有較大的空間。
我會建議你使用TitledBorder
:
JFrame frame = new JFrame("The Title");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panOuter = new JPanel(new BorderLayout());
JPanel panLeft = new JPanel(new BorderLayout());
panLeft.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
JPanel panRight = new JPanel(new BorderLayout());
panRight.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
JPanel panBottom = new JPanel(); // default is FlowLayout
panBottom.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
JPanel panInput = new JPanel(new BorderLayout());
panInput.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
JPanel panConsole = new JPanel(new BorderLayout());
Border outsideBorder = BorderFactory.createEmptyBorder(5, 5, 5, 5);
Border insideBorder = BorderFactory.createTitledBorder("The Console");
Border theBorder = BorderFactory.createCompoundBorder(outsideBorder, insideBorder);
panConsole.setBorder(theBorder);
panInput.add(panLeft, BorderLayout.WEST);
panInput.add(panRight, BorderLayout.EAST);
panInput.add(panBottom, BorderLayout.SOUTH);
panOuter.add(panInput, BorderLayout.NORTH);
panOuter.add(panConsole, BorderLayout.CENTER);
JLabel lblLeft = new JLabel("Label 1", JLabel.CENTER);
JLabel lblRight = new JLabel("Label 2", JLabel.CENTER);
JTextField txtLeft = new JTextField(10);
JTextField txtLright = new JTextField(10);
JButton btnBottom = new JButton("Press it!");
JTextArea txtConsole = new JTextArea(5, 10);
panLeft.add(lblLeft, BorderLayout.NORTH);
panLeft.add(txtLeft, BorderLayout.CENTER);
panRight.add(lblRight, BorderLayout.NORTH);
panRight.add(txtLright, BorderLayout.CENTER);
panBottom.add(btnBottom);
panConsole.add(txtConsole, BorderLayout.CENTER);
frame.setContentPane(panOuter);
frame.pack();
frame.setVisible(true);
第三(文本字段):再次提及UI上面,如果用戶鍵入 許多字符放到第一個文本字段中,將這些字母放在 右邊的文本字段中?如果是的話,我怎麼能阻止呢?
試試上面的代碼,看看它是如何作用:)
第四:如果我追加文本到文本區域,它是已經滿了,會 自動允許用戶滾動?如果不是什麼簡單的方法 使文本區域可滾動?
您需要使用一種叫做JScrollPane
:
JFrame frame = new JFrame("The Title");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panOuter = new JPanel(new BorderLayout());
JPanel panLeft = new JPanel(new BorderLayout());
panLeft.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
JPanel panRight = new JPanel(new BorderLayout());
panRight.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
JPanel panBottom = new JPanel(); // default is FlowLayout
panBottom.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
JPanel panInput = new JPanel(new BorderLayout());
panInput.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
JPanel panConsole = new JPanel(new BorderLayout());
Border outsideBorder = BorderFactory.createEmptyBorder(5, 5, 5, 5);
Border insideBorder = BorderFactory.createTitledBorder("The Console");
Border theBorder = BorderFactory.createCompoundBorder(outsideBorder, insideBorder);
panConsole.setBorder(theBorder);
panInput.add(panLeft, BorderLayout.WEST);
panInput.add(panRight, BorderLayout.EAST);
panInput.add(panBottom, BorderLayout.SOUTH);
panOuter.add(panInput, BorderLayout.NORTH);
panOuter.add(panConsole, BorderLayout.CENTER);
JLabel lblLeft = new JLabel("Label 1", JLabel.CENTER);
JLabel lblRight = new JLabel("Label 2", JLabel.CENTER);
JTextField txtLeft = new JTextField(10);
JTextField txtLright = new JTextField(10);
JButton btnBottom = new JButton("Press it!");
JTextArea txtConsole = new JTextArea(5, 10);
JScrollPane srcPane = new JScrollPane(txtConsole,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
panLeft.add(lblLeft, BorderLayout.NORTH);
panLeft.add(txtLeft, BorderLayout.CENTER);
panRight.add(lblRight, BorderLayout.NORTH);
panRight.add(txtLright, BorderLayout.CENTER);
panBottom.add(btnBottom);
panConsole.add(srcPane, BorderLayout.CENTER);
frame.setContentPane(panOuter);
frame.pack();
frame.setVisible(true);
希望我回答你所有的問題:)
所有這些問題都在Swing教程中解答。請參閱http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html和http://docs.oracle.com/javase/tutorial/uiswing/components/textarea.html。 –