[英]Spacing Labels and Buttons in Java
我還在復習舊的 Java GUI 並遇到了一個樹樁。 只是整個 GUI 的東西還是新鮮的,我只使用了 FlowLayout(),我想我正在尋找的東西不能用它來完成。 這不是為了家庭作業或任何東西,只是我正在做的事情。 無論如何,我的問題:
基本上,我希望它看起來像這樣
Welcome!
Today's Date is:
(space)
(space)
Exit button
我的問題是我對任何布局的了解都不足以完成這項工作。 我一直在閱讀和弄亂GridBagLayout
,但我無法讓它做任何事情,我嘗試了另一種方法,按鈕和 dang 程序一樣大。 無論如何,這是我擁有的代碼,即使它並不重要。
private void welcomeTab(){
welcomePanel = new JPanel(new FlowLayout());
String currentTime = SimpleDateFormat.getInstance().format(
Calendar.getInstance().getTime());
final JLabel welcomeLabel = new JLabel("Welcome!", JLabel.CENTER);
final JLabel dateLabel = new JLabel ("Today's date is: " + currentTime, JLabel.CENTER);
welcomePanel.add(welcomeLabel);
welcomePanel.add(dateLabel);
welcomePanel.add(createExitButton());
}
謝謝你。 我讀了很多書,似乎所有的例子都是為了創建帶有所有按鈕的窗格,這讓我發瘋了。
像這樣的東西?
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.util.Calendar;
import java.text.SimpleDateFormat;
class WelcomeLayout {
private JPanel welcomePanel;
WelcomeLayout() {
welcomeTab();
welcomePanel.setBorder(new TitledBorder("The Welcome Panel"));
JOptionPane.showMessageDialog(null, welcomePanel);
}
private void welcomeTab() {
welcomePanel = new JPanel(new GridLayout(0,1,1,1));
String currentTime = SimpleDateFormat.getInstance().format(
Calendar.getInstance().getTime());
final JLabel welcomeLabel = new JLabel("Welcome!", JLabel.CENTER);
final JLabel dateLabel = new JLabel ("Today's date is: " + currentTime, JLabel.CENTER);
welcomePanel.add(welcomeLabel);
welcomePanel.add(dateLabel);
// one (kludgy) way to addd space.
welcomePanel.add(new JLabel(""));
welcomePanel.add(new JLabel(""));
welcomePanel.add( createExitButton() );
}
private JComponent createExitButton() {
JButton exit = new JButton("Exit");
// the FlowLayout is to center the JButton;
JPanel exitPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
exitPanel.add(exit);
return exitPanel;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
WelcomeLayout wl = new WelcomeLayout();
}
});
}
}
按照 Talha Ahmed Khan/Zéychin 的建議使用BoxLayout
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.util.Calendar;
import java.text.SimpleDateFormat;
class WelcomeBoxLayout {
private JPanel welcomePanel;
WelcomeBoxLayout() {
welcomeTab();
welcomePanel.setBorder(new TitledBorder("The Welcome Panel"));
JOptionPane.showMessageDialog(null, welcomePanel);
}
private void welcomeTab() {
welcomePanel = new JPanel();
BoxLayout layout = new BoxLayout(welcomePanel, BoxLayout.Y_AXIS);
welcomePanel.setLayout(layout);
String currentTime = SimpleDateFormat.getInstance().format(
Calendar.getInstance().getTime());
final JLabel welcomeLabel = new JLabel("Welcome!", JLabel.CENTER);
final JLabel dateLabel = new JLabel ("Today's date is: " + currentTime, JLabel.CENTER);
welcomePanel.add(welcomeLabel);
welcomePanel.add(dateLabel);
welcomePanel.add( Box.createVerticalStrut(20) );
welcomePanel.add( new JButton("Exit") );
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
WelcomeBoxLayout wl = new WelcomeBoxLayout();
}
});
}
}
嘗試添加一個Box.createHorizontalStrut(i_width)
welcomePanel.add(welcomeLabel);
welcomePanel.add( Box.createHorizontalStrut(10) );
welcomePanel.add(dateLabel);
welcomePanel.add( Box.createHorizontalStrut(10) );
welcomePanel.add(createExitButton());
看起來您想使用垂直 BoxLayout。 我不確定 Talha Ahmed Khan 的想法是什么,因為水平支柱會強制兩個元素之間的水平空間量。
這個鏈接應該有幫助: http://download.oracle.com/javase/tutorial/uiswing/layout/box.html
and here's a direct link to the source for the first example on that page: http://download.oracle.com/javase/tutorial/uiswing/examples/layout/BoxLayoutDemoProject/src/layout/BoxLayoutDemo.java
GridBagLayout
在Netbeans 7.0中處於最佳狀態。 看看,你不會后悔的。
建議:
通過使用Netbeans GridBagLayout Designer解決您的問題,然后 go 閱讀生成的代碼以了解修復。
免責聲明:
編寫自定義代碼可能非常麻煩。 你需要熟悉它。 它在大多數地方都提供了添加自定義代碼的鈎子。 但是我還是覺得很麻煩。 你需要自己排序。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.