Dav*_*vid 2 java layout user-interface swing gridbaglayout
我仍在刷新旧的Java GUI并遇到了一些问题.这只是整个GUI的东西仍然很新鲜,我只使用了FlowLayout(),我猜我正在寻找的东西是不能用它完成的.这不是作业或任何东西,只是我正在做的事情.无论如何,我的问题:
基本上,我希望它看起来像这样
Welcome!
Today's Date is:
(space)
(space)
Exit button
Run Code Online (Sandbox Code Playgroud)
我的问题是我不知道任何布局足以完成这项工作.我一直在阅读和搞乱,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());
}
Run Code Online (Sandbox Code Playgroud)
谢谢.我一直在读这么多,似乎所有的例子都是用所有按钮创建窗格,这让我疯了.
像这样的东西?

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();
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
使用BoxLayoutTalha 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();
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6807 次 |
| 最近记录: |