Java 在GridBagLayout中正确删除元素
我试图通过执行jPanel1.remove(filler)来删除,然后在该位置放置一个新的JLabel,但显然不起作用。我做错了什么Java 在GridBagLayout中正确删除元素,java,swing,jpanel,gridbaglayout,Java,Swing,Jpanel,Gridbaglayout,我试图通过执行jPanel1.remove(filler)来删除,然后在该位置放置一个新的JLabel,但显然不起作用。我做错了什么 谢谢 如果filler只是一个JLabel,那么您就可以这样做 jPanel1 = new JPanel(); GridBagLayout layout = new GridBagLayout(); jPanel1.setLayout(layout); GridBagConstraints gbc = new GridBagConstraints(); f
谢谢 如果
filler
只是一个JLabel,那么您就可以这样做
jPanel1 = new JPanel();
GridBagLayout layout = new GridBagLayout();
jPanel1.setLayout(layout);
GridBagConstraints gbc = new GridBagConstraints();
filler = new JLabel();
gbc.gridx = 1;
gbc.gridy = 1;
gbc.weightx = 1;
gbc.weighty = 1;
jPanel1.add(filler, gbc);
或者,如果要更换不同的组件,更好的方法是创建一个使用卡布局的面板。然后您可以交换这两个组件。有关详细信息,请参见上的Swing教程
另一种选择可能是执行以下操作:
filler.setText("add text here");
至于你为什么会有问题,我只能想象 不要忘记,
filler
组件应该添加到最后一个组件的右/底部
例如
GridBagLayout layout = (GridBagLayout)jPanel1.getLayout();
GridbagConstraint gbc = layout.getConstraint(oldComponent);
jPanel1.remove(oldComponent);
jPanel1.add(newComponent, gbc);
jPanel1.revalidate();
jPanel1.repaint();
我真的不想交换它们。只需拆下一个,然后将另一个放在其他位置。改变一切真的很难。。我不可能用GridBag做这个?这正是CardLayout做的。它在同一位置显示不同的零部件。你读过教程吗?@camickr要了解更多的上下文,请查看OP的“它起作用了…”。。我真的很笨!我添加了权重x和权重y等于1的标签,而不是将它们重置为0。!!非常感谢大家。显然,这个问题与您的最后一个问题()有关。当你不知道整个问题的背景时,花时间回答一个问题会让人恼火。祝你好运,我不知道还有什么信息你可能忘了提到。它奏效了。。我真的很笨!我添加了权重x和权重y等于1的标签,而不是将它们重置为0。!!非常感谢大家。
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
public class GridBagLayoutTest {
public static void main(String[] args) {
new GridBagLayoutTest();
}
public GridBagLayoutTest() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
final TestPane pane = new TestPane();
JButton btn = new JButton("Add");
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
pane.addNewItem();
}
});
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(pane);
frame.add(btn, BorderLayout.SOUTH);
frame.pack();
frame.setSize(300, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private int gridX = 0;
private int gridY = 0;
private JLabel filler;
private int columnCount = 4;
public TestPane() {
setLayout(new GridBagLayout());
filler = new JLabel();
}
public void addNewItem() {
remove(filler);
JLabel label = new JLabel("Cell " + gridX + "x" + gridY);
label.setBorder(new EmptyBorder(10, 10, 10, 10));
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = gridX;
gbc.gridy = gridY;
add(label, gbc);
gridX++;
if (gridX >= columnCount) {
gridX = 0;
gridY++;
}
gbc = new GridBagConstraints();
gbc.gridx = columnCount;
gbc.gridy = gridY + 1;
gbc.weightx = 1;
gbc.weighty = 1;
add(filler, gbc);
revalidate();
repaint();
}
}
}