我目前正致力于改善工作项目,这需要我创建6个不同的GUI。我首先应该说我在GUI设计上非常糟糕。我曾尝试过学习Grid Bag,但确实觉得很困难。
在这个GUI中我使用BorderLayout和Flow Layout,它工作正常。我能够创建GUI并完成目标,但我不禁觉得这种方法不专业,并且如果我的其他GUI最终拥有更多JTextFields和JLabel(其中一些),将会有很多不必要的代码会)。
我想知道是否有任何布局可以减少我在这里的面板数量。我已经探索了网格布局,但我担心所有单元格大小相同。如果有人可以通过让我知道,从他们的经验,这个项目的最佳布局,并可能告诉我如何使用它,我会很感激。我已经尝试过像GridBag布局这样的布局教程,但实际上在我自己的项目中实现它有问题。
主类
package mainClasses;
import gui.AllGUI;
public class TesterClass
{
public static void main(String args[])
{
AllGUI guiALL = new AllGUI();
guiALL.createAllGUI();
}
}
GUI
package gui;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class AllGUI
{
public void createAllGUI(){
JFrame frame = new JFrame("All File Types Selection");
JPanel mainPanel = new JPanel(new BorderLayout());
JPanel panelOne = new JPanel(new BorderLayout());
JPanel panelLine1 = new JPanel(new FlowLayout());
JPanel panelLine2 = new JPanel(new FlowLayout());
JPanel panelLine3 = new JPanel(new FlowLayout());
JPanel panelLine4 = new JPanel(new FlowLayout());
JPanel panelLine5 = new JPanel(new FlowLayout());
JButton confirmButton = new JButton("Confirm");
JLabel groupMessageIdTitle = new JLabel("Group Message Id:");
JLabel isoDateTimeTitle = new JLabel("ISO Creation Date/Time:");
JLabel notificationIdTitle = new JLabel("Notification Id:");
JLabel notificationAcctIdTitle = new JLabel("Notification Account Id:");
JLabel numberOfEntriesTitle = new JLabel("Number of Entries:");
JLabel sumOfAmountsTitle = new JLabel("Sum of Amounts:");
JLabel fileTypeTitle = new JLabel("Camt54 File Type:");
JTextField groupMessageIdText = new JTextField("",10);
JTextField isoDateTimeText = new JTextField("",10);
JTextField notificationIdText = new JTextField("",10);
JTextField notificationAcctIdText = new JTextField("",10);
JTextField numberOfEntriesText = new JTextField("",10);
JTextField sumOfAmountsText = new JTextField("",10);
String[] fileTypes = {"OTC-R Message", "Home-Banking", "Cleared Checks"};
JComboBox fileTypesComboBox = new JComboBox(fileTypes);
panelLine1.add(groupMessageIdTitle);
panelLine1.add(groupMessageIdText);
panelLine1.add(isoDateTimeTitle);
panelLine1.add(isoDateTimeText);
panelLine2.add(notificationIdTitle);
panelLine2.add(notificationIdText);
panelLine2.add(notificationAcctIdTitle);
panelLine2.add(notificationAcctIdText);
panelLine3.add(numberOfEntriesTitle);
panelLine3.add(numberOfEntriesText);
panelLine3.add(sumOfAmountsTitle);
panelLine3.add(sumOfAmountsText);
panelLine4.add(fileTypeTitle);
panelLine4.add(fileTypesComboBox);
panelLine5.add(confirmButton);
panelOne.add(panelLine1,BorderLayout.NORTH);
panelOne.add(panelLine2,BorderLayout.CENTER);
panelOne.add(panelLine3,BorderLayout.SOUTH);
mainPanel.add(panelOne,BorderLayout.NORTH);
mainPanel.add(panelLine4,BorderLayout.CENTER);
mainPanel.add(panelLine5,BorderLayout.SOUTH);
frame.add(mainPanel);
frame.setVisible(true);
frame.setSize(630,210);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
}
答案 0 :(得分:2)
我想这是一个意见,但我认为这个GUI看起来更专业。
标签和文本字段按列对齐。我使用GridBagLayout来创建GUI的表单部分。
这是代码。我将JPFnel中的JFrame代码,JPanel代码和Swing组件分组在一起,因此代码更容易理解。
我将main方法与此代码放在一起,因此我只需要粘贴一个文件。
package com.ggl.testing;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class AllGUI {
private static final Insets normalInsets = new Insets(10, 10, 0, 10);
public static void main(String[] args) {
Runnable runnable = new Runnable() {
@Override
public void run() {
new AllGUI().createAllGUI();
}
};
SwingUtilities.invokeLater(runnable);
}
public void createAllGUI() {
JFrame frame = new JFrame("All File Types Selection");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createMainPanel());
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel createMainPanel() {
JPanel mainPanel = new JPanel(new BorderLayout());
JPanel formPanel = new JPanel(new GridBagLayout());
int gridy = 0;
JLabel groupMessageIdTitle = new JLabel("Group Message Id:");
addComponent(formPanel, groupMessageIdTitle, 0, gridy, 1, 1,
normalInsets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
JTextField groupMessageIdText = new JTextField("", 10);
addComponent(formPanel, groupMessageIdText, 1, gridy, 1, 1,
normalInsets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
JLabel isoDateTimeTitle = new JLabel("ISO Creation Date/Time:");
addComponent(formPanel, isoDateTimeTitle, 2, gridy, 1, 1, normalInsets,
GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);
JTextField isoDateTimeText = new JTextField("", 10);
addComponent(formPanel, isoDateTimeText, 3, gridy++, 1, 1,
normalInsets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
JLabel notificationIdTitle = new JLabel("Notification Id:");
addComponent(formPanel, notificationIdTitle, 0, gridy, 1, 1,
normalInsets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
JTextField notificationIdText = new JTextField("", 10);
addComponent(formPanel, notificationIdText, 1, gridy, 1, 1,
normalInsets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
JLabel notificationAcctIdTitle = new JLabel("Notification Account Id:");
addComponent(formPanel, notificationAcctIdTitle, 2, gridy, 1, 1,
normalInsets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
JTextField notificationAcctIdText = new JTextField("", 10);
addComponent(formPanel, notificationAcctIdText, 3, gridy++, 1, 1,
normalInsets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
JLabel numberOfEntriesTitle = new JLabel("Number of Entries:");
addComponent(formPanel, numberOfEntriesTitle, 0, gridy, 1, 1,
normalInsets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
JTextField numberOfEntriesText = new JTextField("", 10);
addComponent(formPanel, numberOfEntriesText, 1, gridy, 1, 1,
normalInsets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
JLabel sumOfAmountsTitle = new JLabel("Sum of Amounts:");
addComponent(formPanel, sumOfAmountsTitle, 2, gridy, 1, 1,
normalInsets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
JTextField sumOfAmountsText = new JTextField("", 10);
addComponent(formPanel, sumOfAmountsText, 3, gridy++, 1, 1,
normalInsets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
JLabel fileTypeTitle = new JLabel("Camt54 File Type:");
addComponent(formPanel, fileTypeTitle, 0, gridy, 1, 1, normalInsets,
GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);
String[] fileTypes = { "OTC-R Message", "Home-Banking",
"Cleared Checks" };
JComboBox<String> fileTypesComboBox = new JComboBox<>(fileTypes);
addComponent(formPanel, fileTypesComboBox, 1, gridy, 1, 1,
normalInsets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
JPanel confirmPanel = new JPanel();
JButton confirmButton = new JButton("Confirm");
confirmPanel.add(confirmButton);
mainPanel.add(formPanel, BorderLayout.CENTER);
mainPanel.add(confirmPanel, BorderLayout.SOUTH);
return mainPanel;
}
private void addComponent(Container container, Component component,
int gridx, int gridy, int gridwidth, int gridheight, Insets insets,
int anchor, int fill) {
GridBagConstraints gbc = new GridBagConstraints(gridx, gridy,
gridwidth, gridheight, 0.0D, 0.0D, anchor, fill, insets, 0, 0);
container.add(component, gbc);
}
}