在调用Component
和GroupLayout
之后,setHorizontalGroup()
可以setVerticalGroup()
动态添加GroupLayout
组,而无需重置布局?我正在GUI上创建一个项目,帮助学生找到最好的方法来整理他们的每周课程安排。这涉及输入关于类时间等的信息,用于未知数量的类。当然,我不能只是为用户添加任意数量的表单来将信息输入并说“没有人考虑超过这个数量的类”#34;所以用户需要能够自己添加新表格。我可以创建表单,但我无法找到任何方法将其成功添加到窗口中的setHorizontalGroup(...)
,而无需重新调用setVerticalGroup(...)
或Group
之前的 private static void createAndShowGUI() {
//Create window and assign an empty GroupLayout to it
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GroupLayout layout = new GroupLayout(frame.getContentPane());
frame.getContentPane().setLayout(layout);
/*WeekPanel extends JPanel, and is the "form" the user enters data into.
In essence, it's a JPanel with a series of JTextFields.*/
WeekPanel panel1 = new WeekPanel();
WeekPanel panel2 = new WeekPanel();
//The layout code below places two WeekPanels in the window, one on top of the other
layout.setHorizontalGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup()
.addComponent(panel1)
.addComponent(panel2)));
layout.setVerticalGroup(layout.createSequentialGroup()
.addComponent(panel1)
.addComponent(panel2));
/*I would like to add a component to the layout here as proof of concept.*/
frame.pack();
frame.setVisible(true);
}
而不是添加它们。
这是我目前拥有的代码的相关部分(注释易读性):
frame.add([some component])
我已经尝试在frame.pack()
之前致电Group
,但它从未产生任何明显的差异。我查看了GroupLayout的官方文档,我认为无法访问或更改GroupLayout
中的水平/垂直Group
,甚至是Layout
引用的字段Code
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([1, 2, 3, 4, 5], [50, 40, 60, 70, 50])
plt.show()
就此而言。
我完全不知道该怎么做。如果可能的话,我想避免学习一种新型的select max(avg_value)
from
(
SELECT AVG(amount) avg_value FROM orders GROUP BY cust
) tmp
,但是我开始害怕我必须(或者更糟糕的是,那就是我所做的事情)。我试图做的事情是不可能的。)
答案 0 :(得分:2)
如图here所示,您可以使用此方法:
ParallelGroup
和SequentialGroup
。 在下面的示例中,add()
会在布局中附加新的标签和文本字段。建议here的AdjustmentListener
滚动到最后添加的行。
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.AdjustmentEvent;
import javax.swing.AbstractAction;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
/**
* @see https://stackoverflow.com/a/41926375/230513
* @see https://stackoverflow.com/a/14858272/230513
* @see https://stackoverflow.com/a/8504753/230513
* @see https://stackoverflow.com/a/14011536/230513
*/
public class DynamicGroupLayout {
private static final int NUM = 6;
private GroupLayout layout;
private GroupLayout.ParallelGroup parallel;
private GroupLayout.SequentialGroup sequential;
private int i;
private JPanel create() {
JPanel panel = new JPanel();
layout = new GroupLayout(panel);
panel.setLayout(layout);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
parallel = layout.createParallelGroup();
layout.setHorizontalGroup(layout.createSequentialGroup().addGroup(parallel));
sequential = layout.createSequentialGroup();
layout.setVerticalGroup(sequential);
for (int i = 0; i < NUM; i++) {
add();
}
return panel;
}
private void add() {
JLabel label = new JLabel(String.valueOf(i + 1), JLabel.RIGHT);
JTextField field = new JTextField(String.valueOf("String " + (i + 1)));
label.setLabelFor(field);
parallel.addGroup(layout.createSequentialGroup().
addComponent(label).addComponent(field));
sequential.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE).
addComponent(label).addComponent(field));
i++;
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame f = new JFrame("DynamicGroupLayout");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final DynamicGroupLayout dgl = new DynamicGroupLayout();
final JPanel panel = dgl.create();
JScrollPane jsp = new JScrollPane(panel) {
@Override
public Dimension getPreferredSize() {
return new Dimension(320, 240);
}
};
jsp.getVerticalScrollBar().addAdjustmentListener((AdjustmentEvent e) -> {
e.getAdjustable().setValue(e.getAdjustable().getMaximum());
});
f.add(jsp);
JPanel controls = new JPanel();
controls.add(new JButton(new AbstractAction("Add") {
@Override
public void actionPerformed(ActionEvent e) {
dgl.add();
panel.validate();
}
}));
f.add(controls, BorderLayout.SOUTH);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
});
}
}