我试图在单击按钮时在面板内部显示一组两个JTextfields(并排)。问题是像Flowlayout这样的东西会让我的文本字段在整个地方移动(它们排成一行或一列),而Gridlayout会使文本字段太大。我已经调查了Springlayout和Grouplayout,但是这些布局似乎没有一种直接的方式来在运行时添加一个组件(加上代码看起来非常麻烦,如下例所示)。
这是我试图用布局实现的一个例子...运行代码并注意当你调整窗口大小时JTextfields的反应(添加字段按钮按钮不起作用,因为我不确定如何使用Grouplayout将组件添加到面板中:
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;
public class GoodLayout extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
private JTextField textField_4;
private JTextField textField_5;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GoodLayout frame = new GoodLayout();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public GoodLayout() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 383, 328);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new BorderLayout(0, 0));
JButton buttonAddFields = new JButton("+ Add Set of Fields +");
buttonAddFields.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Add a set of fields to the panel");
}
});
contentPane.add(buttonAddFields, BorderLayout.SOUTH);
JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.CENTER);
textField = new JTextField();
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setColumns(10);
textField_2 = new JTextField();
textField_2.setColumns(10);
textField_3 = new JTextField();
textField_3.setColumns(10);
textField_4 = new JTextField();
textField_4.setColumns(10);
textField_5 = new JTextField();
textField_5.setColumns(10);
GroupLayout gl_panel = new GroupLayout(panel);
gl_panel.setHorizontalGroup(
gl_panel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel.createSequentialGroup()
.addContainerGap()
.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel.createSequentialGroup()
.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(textField_1, GroupLayout.DEFAULT_SIZE, 518, Short.MAX_VALUE))
.addGroup(gl_panel.createSequentialGroup()
.addComponent(textField_2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(textField_3, GroupLayout.DEFAULT_SIZE, 518, Short.MAX_VALUE))
.addGroup(gl_panel.createSequentialGroup()
.addComponent(textField_4, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(textField_5, GroupLayout.DEFAULT_SIZE, 518, Short.MAX_VALUE)))
.addContainerGap())
);
gl_panel.setVerticalGroup(
gl_panel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel.createSequentialGroup()
.addContainerGap()
.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(textField_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
.addComponent(textField_2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(textField_3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
.addComponent(textField_4, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(textField_5, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addContainerGap(287, Short.MAX_VALUE))
);
panel.setLayout(gl_panel);
}
}
上面代码中的重要部分是actionPerformed方法......当我点击按钮并出现一对文本字段时,我真的很想找到一个看起来不错的简单解决方案(GUI布局方面)。也许这有更好的布局?我使用Eclipse插件WindowBuilder来构建上面的代码,并且我尝试了所有看起来合理的不同布局选项。
有关如何更好地解决此问题的任何想法或技巧都值得赞赏
谢谢,Dan
答案 0 :(得分:2)
不要丢弃GroupLayout
经理 - 它是一个非常强大的布局管理器
并且是为数不多的管理者之一。
但是,它不适用于动态布局结构
布局的方式。我们必须完全重做我们的布局和
做临时计算。
为此,我们可以轻松使用MigLayout
。 MigLayout
是第三方布局
经理,也做得很好。它易于使用且功能强大。
package com.zetcode;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import net.miginfocom.swing.MigLayout;
public class GoodLayout2 extends JFrame {
private JPanel pnl;
public GoodLayout2() {
initUI();
setTitle("MigLayout solution");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void initUI() {
JButton addBtn = new JButton("Add");
addBtn.addActionListener(new AddAction());
pnl = new JPanel(new MigLayout("ins dialog, wrap 2"));
pnl.add(addBtn, "span 2, center");
pnl.add(new JTextField(10));
pnl.add(new JTextField(10), "pushx, growx");
pnl.add(new JTextField(10));
pnl.add(new JTextField(10), "pushx, growx");
pnl.add(new JTextField(10));
pnl.add(new JTextField(10), "pushx, growx");
add(pnl);
pack();
}
private class AddAction extends AbstractAction {
@Override
public void actionPerformed(ActionEvent e) {
pnl.add(new JTextField(10));
pnl.add(new JTextField(10), "pushx, growx");
pnl.doLayout();
pnl.repaint();
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
GoodLayout2 ex = new GoodLayout2();
ex.setVisible(true);
}
});
}
}
我已将按钮放在窗口的北部,因为代码 然后稍微复杂一点。