在我的JFrame中,我在每行中有两个label
和一个JComboBox
ordred如下:
Label JComboBox Label
**Bouton**
用户可以添加具有相同实体的新行。所以,我添加一个bouton并在用户提交新行时创建。
Label JComboBox Label
**Bouton**
Label JComboBox Label
然而,布顿的位置保持不变。即使创建了新行,如何更改bouton的位置(在页面末尾)?
我的代码如下:
public class Display extends JFrame{
public Display(){
super("Test");
setTitle("Test");
setSize(800,800);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
initComponents();
}
private void initComponents(){
setLayout(new GridLayout(0, 3));
JPanel po = new JPanel();
JLabel label = new JLabel("Resource");
po.add(label);
add(po);
JPanel po2 = new JPanel();
po2.add(new JComboBox<>(new String[] { "option1", "option2", "option3",}));
add(po2);
JPanel po3 = new JPanel();
JLabel label3 = new JLabel("Something");
po3.add(label3);
add(po3);
//new
JPanel PanelBouton = new JPanel();
JButton bouton = new JButton(new AddResourceAction("Add new"));
add(bouton);
PanelBouton.add(bouton);
add(PanelBouton);
PanelBouton = new JPanel();
JLabel vide = new JLabel("");
PanelBouton.add(vide);
add(PanelBouton);
PanelBouton = new JPanel();
vide = new JLabel("");
PanelBouton.add(vide);
add(PanelBouton);
}
class AddResourceAction extends AbstractAction {
public AddResourceAction(String n){
super(n);
}
@Override
public void actionPerformed(ActionEvent e) {
JPanel po = new JPanel();
JLabel label = new JLabel("Resource");
po.add(label);
add(po);
JPanel po2 = new JPanel();
po2.add(new JComboBox<>(new String[] { "option1", "option2", "option3",}));
add(po2);
JPanel po3 = new JPanel();
JLabel label3 = new JLabel("Something");
po3.add(label3);
add(po3);
revalidate();
}
}
public static void main(String[] args) {
/*display panel*/
SwingUtilities.invokeLater(new Runnable() {
@Override public void run() {
new Display().setVisible(true);
}
});
}
}
答案 0 :(得分:2)
创建一个包含两个容器的分割面板。一个容器(A)用于动态按钮,另一个容器用于按钮&#34;添加新的&#34;。将新组件添加到A容器中。
查找以下代码,根据您的具体情况说明此概念。 使用风险自负:)
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Display
extends JFrame
{
Box upperBox = new Box(BoxLayout.X_AXIS);
Box dynamicBox = new Box(BoxLayout.Y_AXIS);
Box staticBox = new Box(BoxLayout.X_AXIS);
public Display()
{
super("Test");
setTitle("Test");
setSize(800,800);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
initComponents();
}
private void initComponents()
{
//This will be the parent panel for other panels.
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
upperBox.add(new JLabel("Resource"));
upperBox.add(new JComboBox<>(new String[] { "option1", "option2", "option3",}));
upperBox.add(new JLabel("Something"));
panel.add(upperBox);
staticBox.add(new JButton(new AddResourceAction("Add new")));
panel.add(dynamicBox); //just add this box now, it will be filled later with components
panel.add(staticBox);
add(panel);
}
class AddResourceAction extends AbstractAction
{
public AddResourceAction(String n)
{
super(n);
}
@Override
public void actionPerformed(ActionEvent e) {
Box box = new Box(BoxLayout.X_AXIS);
box.add(new JLabel("Resource"));
box.add(new JComboBox<>(
new String[] { "option1", "option2", "option3",}));
box.add(new JLabel("Something"));
dynamicBox.add(box);
revalidate();
}
}
public static void main(String[] args)
{
/*display panel*/
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
new Display().setVisible(true);
}
});
}
}