[英]Building UI with Java Swing
How should I go about building this UI using Java Swing without the use of GUI Builders? 我应该如何在不使用GUI生成器的情况下使用Java Swing构建该UI?
I don't need a solution but just some general guidance on how to get started with this. 我不需要解决方案,而只需要一些有关如何入门的一般指导。 Specifically, cues on selecting the best layout(s) and Swing components to complete the job.
具体来说,可以选择最佳的布局和Swing组件来完成工作。
What I have done so far is build a JFrame with label, text field pairs like so: 到目前为止,我所做的是构建带有标签,文本字段对的JFrame,如下所示:
import java.awt.*;
import javax.swing.*;
import javax.swing.border.TitledBorder;
class AdminForm {
AdminForm() {
JFrame f = new JFrame("Panel Example");
JPanel gui = new JPanel(new BorderLayout(2,2));
JPanel labelFields = new JPanel(new BorderLayout(2,2));
labelFields.setBorder(new TitledBorder("BorderLayout"));
JPanel labels = new JPanel(new GridLayout(0,2,1,1));
labels.setBorder(new TitledBorder("GridLayout"));
JPanel fields = new JPanel(new GridLayout(0,2,1,1));
fields.setBorder(new TitledBorder("GridLayout"));
for (int ii=1; ii<8; ii++) {
labels.add(new JLabel("Label " + ii));
// if these were of different size, it would be necessary to
// constrain them using another panel
labels.add(new JTextField(10));
}
labelFields.add(labels, BorderLayout.CENTER);
//labelFields.add(fields, BorderLayout.EAST);
gui.add(labelFields, BorderLayout.NORTH);
//gui.add(guiCenter, BorderLayout.CENTER);
f.add(gui);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
public static void main(String[] args) throws Exception {
//Create the GUI on the event dispatching thread
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run() {
new AdminForm();
}
});
}
}
Concerns: 关注:
I'm not sure I used the best layout configuration to get the labels and the text fields list in place. 我不确定是否使用最佳布局配置来获取标签和文本字段列表。 A better way to do this?
更好的方法呢?
The panel is shrunk when I run this. 当我运行此面板时,面板会缩小。 I think I need to specify a panel size?
我想我需要指定面板尺寸吗?
I need another panel to the right of this obviously. 很明显,我需要在此右侧另外一个面板。 How do I introduce a new panel and get this aligned with what I already have?
如何引入一个新的面板并使它与我现有的面板保持一致?
As you're using Layout Managers you need to know that you can combine them. 使用布局管理器时,您需要知道可以将它们组合在一起。 You might ask "How can I do that?"
您可能会问: “我该怎么做?”
Easy, nest JPanels inside each other, each with their own layout manager. 轻松地将JPanels嵌套在彼此内部,每个都有自己的布局管理器。
For your specific case, I see the following structure: 对于您的特定情况,我看到以下结构:
JFrame (The window)
JPanel (The main pane) - Flow Layout (Default layout of a JPanel)
JPanel (Left pane) - GridLayout (As your image shows, all labels & fields have the same size)
JPanel (Right pane) - GridBagLayout (It's a weird accomodation of the buttons, so I did my best to replicate & give a better look)
As you can see, we have 2 nested JPanels inside another one and each of them has different layouts which help to have different accomodations. 如您所见,我们在另一个JPanel中有2个嵌套的JPanel,它们每个都有不同的布局,这有助于拥有不同的住宿。
The following code produces this output: 以下代码产生此输出:
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class AdminForm {
private static final String[] labels = {"Car Number", "Car No. Plate", "Model", "Capacity", "Date Purchased",
"Insurance Status", "Date Insured", "Insurance Expiry Date", "Availability"};
private JFrame frame;
private JPanel pane;
private JPanel leftPane;
private JPanel rightPane;
private JTextField[] fields;
private JButton searchButton;
private JTextField searchField;
private JButton updateButton;
private JButton deleteButton;
private JButton clearButton;
private JButton nextButton;
private JButton previousButton;
public static void main(String[] args) {
SwingUtilities.invokeLater(new AdminForm()::createAndShowGUI);
}
private void createAndShowGUI() {
frame = new JFrame();
pane = new JPanel();
leftPane = createLeftPane();
rightPane = createRightPane();
pane.add(leftPane);
pane.add(rightPane);
frame.add(pane);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private JPanel createRightPane() {
JPanel panel = new JPanel();
searchButton = new JButton("Search");
searchField = new JTextField(10);
updateButton = new JButton("Update");
deleteButton = new JButton("Delete");
clearButton = new JButton("Clear");
nextButton = new JButton("Next");
previousButton = new JButton("Previous");
panel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(5, 5, 5, 5);
panel.add(searchButton, gbc);
gbc.gridy++;
gbc.gridwidth = 2;
panel.add(searchField, gbc);
gbc.gridy++;
gbc.gridwidth = 1;
panel.add(updateButton, gbc);
gbc.gridy++;
panel.add(deleteButton, gbc);
gbc.gridx++;
panel.add(clearButton, gbc);
gbc.gridx = 0;
gbc.gridy++;
panel.add(nextButton, gbc);
gbc.gridy++;
panel.add(previousButton, gbc);
panel.setBorder(BorderFactory.createLineBorder(Color.GRAY));
return panel;
}
private JPanel createLeftPane() {
JPanel panel = new JPanel();
fields = new JTextField[labels.length];
panel.setLayout(new GridLayout(0, 2, 5, 5));
for (int i = 0; i < fields.length; i++) {
fields[i] = new JTextField(10);
//This makes text fields not editable for certain ones.
switch (i) {
case 4:
case 6:
case 7:
case 8:
fields[i].setEditable(false);
break;
default:
break;
}
panel.add(new JLabel(labels[i]));
panel.add(fields[i]);
}
//Investigate how to change color of border, etc
panel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.GRAY), "Update Data"));
return panel;
}
}
Just play around with the values to have your desired output. 只是玩弄这些值以获得所需的输出。
Give your variables better namings, later on you'll ask "What is f
"? 给变量更好的命名,稍后您会问“什么是
f
”?
JFrame f = new JFrame("Panel Example");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.