像这样的东西?

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();
}
});
}
}
BoxLayout
按照 Talha Ahmed Khan/Zéychin的建议使用

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();
}
});
}
}