Java中的标签和按钮间距
我还在温习旧的JavaGUI,遇到了一些麻烦。只是整个GUI还很新鲜,我只使用了FlowLayout(),我想我要找的东西不能用它来完成。这不是家庭作业什么的,只是我正在做的事情。不管怎样,我的问题是: 基本上,我希望它看起来像这样Java中的标签和按钮间距,java,swing,user-interface,layout,gridbaglayout,Java,Swing,User Interface,Layout,Gridbaglayout,我还在温习旧的JavaGUI,遇到了一些麻烦。只是整个GUI还很新鲜,我只使用了FlowLayout(),我想我要找的东西不能用它来完成。这不是家庭作业什么的,只是我正在做的事情。不管怎样,我的问题是: 基本上,我希望它看起来像这样 Welcome! Today's Date is: (space) (space) Exit button 我的问题是我对任何布局都不太了解,无法完成这项工作。我一直在阅读和弄乱GridBagLayout,但我无法让它做任何事情,我尝试了另一种方法,按钮和dan
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());
}
多谢各位。我读了很多书,似乎所有的例子都是用来创建带有所有按钮的窗格,这让我发疯 尝试添加一个
框。创建水平支柱(i\u宽度)
看起来您想要使用垂直的BoxLayout。我不知道塔哈·艾哈迈德·汗在想什么, 因为水平支柱加强了两个元素之间的水平空间 此链接应有助于: 这是该页面上第一个示例的源代码的直接链接:
GridBagLayout
在最佳状态下使用。看看吧,你不会后悔的
建议:
使用Netbeans GridBagLayout Designer解决您的问题,然后阅读生成的代码以了解修复方法
免责声明:
编写自定义代码可能非常麻烦。你需要熟悉这一点。在大多数地方,它提供了添加自定义代码的钩子。但我还是觉得很麻烦。您需要自己对其进行排序。像这样的事情
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建议的
盒子布局
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();
}
});
}
}
您可以使用绝对布局,将组件几乎放置在您想要的任何位置@Bartzilla:“…将组件放置在几乎任何你想要的地方。”-1真正的诀窍是将它们放置在需要的地方。一旦你弄明白了背后的逻辑,你就可以在一个定制的布局管理器中表达出来。”…你需要自己对它进行排序。”当然,这最好是在没有强大的automagic IDE的帮助下完成的。在您理解布局之前,向布局问题抛出IDE不是一个解决方案。@Andrew:用工具将其排序,然后阅读生成的代码。。。。另一种方式。我自己讨厌魔法,不想在不知道的情况下做任何事情。不过,我不介意你的否定意见。如果你把“仇恨魔法”这句话用在你的答案中(以某种形式),我会提出撤销否决票。这个理论的问题是,不是我否决了你的回答。我觉得一句话就够了。安德鲁:我已经尽力了。感谢您的建议。+1这是NetBeans GUI编辑器的一个优点:您可以尝试不熟悉的布局。依我看,
GridBagLayout
的自定义布局控件使它更容易理解。如果它有助于解决问题,请联系我们。
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();
}
});
}
}