我试图在Java中实现以下效果:
但是,我不确定使用什么布局以及如何使用.FlowLayout
显然不起作用.GridLayout
将无法工作,因为前4行应该是1列行,但第5行需要有2列.
到目前为止这是我的代码:
public class DepositPanel extends JPanel { private JLabel cashL, checksL; private JTextField cashTF, checksTF; private JButton ok, cancel; DepositPanel() { JPanel depositP = new JPanel(); depositP.setLayout(new FlowLayout(FlowLayout.LEFT, 2, 2)); depositP.setPreferredSize(new Dimension(250, 85)); JTextField cashTF = new JTextField(22); JTextField checksTF = new JTextField(22); JLabel cashL = new JLabel("Cash:"); JLabel checksL = new JLabel("Checks:"); ok = new JButton("OK"); cancel = new JButton("CANCEL"); depositP.add(cashL); depositP.add(cashTF); depositP.add(checksL); depositP.add(checksTF); depositP.add(ok); depositP.add(cancel): } }
Frakcool.. 6
您可以尝试使用Layouts的组合,JPanels
按钮为2,1 ,字段为1,FlowLayout为按钮面板,BoxLayout为字段面板.并将它们添加到框架中.(我做了一个JFrame测试,但你可以将它改成JPanel并将该面板添加到你的JFrame中).请确保只有1个JFrame,请参阅使用多个JFrame,好/坏练习.
例如:
import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.*; public class DepositExample { JFrame frame; JPanel buttonPane, fieldsPanel; JLabel cash, checks; JTextField cashField, checksField; JButton ok, cancel; DepositExample() { frame = new JFrame("Deposit"); buttonPane = new JPanel(); fieldsPanel = new JPanel(); cash = new JLabel("Cash"); checks = new JLabel("Checks"); cashField = new JTextField(""); checksField = new JTextField(""); ok = new JButton("OK"); cancel = new JButton("Cancel"); fieldsPanel.setLayout(new BoxLayout(fieldsPanel, BoxLayout.PAGE_AXIS)); buttonPane.setLayout(new FlowLayout()); fieldsPanel.add(cash); fieldsPanel.add(cashField); fieldsPanel.add(checks); fieldsPanel.add(checksField); buttonPane.add(ok); buttonPane.add(cancel); frame.add(fieldsPanel, BorderLayout.PAGE_START); frame.add(buttonPane, BorderLayout.PAGE_END); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); } public static void main(String args[]) { new DepositExample(); } }
要在组件之间获得更多间距,您可以在下面的评论中按照@LuxxMiner的建议添加EmptyBorders.
您可以尝试使用Layouts的组合,JPanels
按钮为2,1 ,字段为1,FlowLayout为按钮面板,BoxLayout为字段面板.并将它们添加到框架中.(我做了一个JFrame测试,但你可以将它改成JPanel并将该面板添加到你的JFrame中).请确保只有1个JFrame,请参阅使用多个JFrame,好/坏练习.
例如:
import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.*; public class DepositExample { JFrame frame; JPanel buttonPane, fieldsPanel; JLabel cash, checks; JTextField cashField, checksField; JButton ok, cancel; DepositExample() { frame = new JFrame("Deposit"); buttonPane = new JPanel(); fieldsPanel = new JPanel(); cash = new JLabel("Cash"); checks = new JLabel("Checks"); cashField = new JTextField(""); checksField = new JTextField(""); ok = new JButton("OK"); cancel = new JButton("Cancel"); fieldsPanel.setLayout(new BoxLayout(fieldsPanel, BoxLayout.PAGE_AXIS)); buttonPane.setLayout(new FlowLayout()); fieldsPanel.add(cash); fieldsPanel.add(cashField); fieldsPanel.add(checks); fieldsPanel.add(checksField); buttonPane.add(ok); buttonPane.add(cancel); frame.add(fieldsPanel, BorderLayout.PAGE_START); frame.add(buttonPane, BorderLayout.PAGE_END); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); } public static void main(String args[]) { new DepositExample(); } }
要在组件之间获得更多间距,您可以在下面的评论中按照@LuxxMiner的建议添加EmptyBorders.