gpt4 book ai didi

java - 使用 ScrollPane java 的动态按钮

转载 作者:行者123 更新时间:2023-12-02 07:07:36 25 4
gpt4 key购买 nike

我想创建动态按钮(例如 15 个),但我可以在屏幕上看到它们(450,450),我尝试使用滚动 Pane ,但没有结果。这个想法很简单,创建 X 按钮并可以在屏幕中垂直滚动(按钮位于屏幕中央)

这是代码,但我不知道谁能让滚动 Pane 正常工作..总是有相同的结果(我不包括滚动 Pane 代码)

public class Consulta2 extends JFrame {

private JPanel contentPane;
private JLabel label;
private JButton boton;


public Consulta2() {




setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(450, 450);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);


int x=50;
for (int i=0;i<10;i++){


JButton boton = new JButton("oli");

GroupLayout gl_contentPane1 = new GroupLayout(contentPane);
gl_contentPane1.setHorizontalGroup(
gl_contentPane1.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane1.createSequentialGroup()
.addGap(163)
.addComponent(boton)
.addContainerGap(172, Short.MAX_VALUE))
);
gl_contentPane1.setVerticalGroup(
gl_contentPane1.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane1.createSequentialGroup()
.addGap(50+x)
.addComponent(boton)
.addContainerGap(229, Short.MAX_VALUE))
);
contentPane.setLayout(gl_contentPane1);

x=x+50;
//contentPane.add(boton);


setVisible(true);
}

label = new JLabel("New label");
GroupLayout gl_contentPane = new GroupLayout(contentPane);
gl_contentPane.setHorizontalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(183)
.addComponent(label))
);
gl_contentPane.setVerticalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(68)
.addComponent(label))
);
contentPane.setLayout(gl_contentPane);

Dimension tamFrame=this.getSize();//para obtener las dimensiones del frame
Dimension tamPantalla=Toolkit.getDefaultToolkit().getScreenSize(); //para obtener el tamanio de la pantalla
setLocation((tamPantalla.width-tamFrame.width)/2, (tamPantalla.height-tamFrame.height)/2); //para posicionar
setVisible(true);



}


public void mostrar()
{
setVisible(true);
}


public void setText(String string) {
//JLabel label = new JLabel();
label.setText(string);

}
}

http://imageshack.us/photo/my-images/94/45985657.jpg/

最佳答案

我很快在 JFrame 中完成了这个。它对我有用。

    // the panel
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridBagLayout());
buttonPanel.setSize(new Dimension(400, 300)); // whatever

// the scrollpane
JScrollPane pane = new JScrollPane();
pane.setSize(new Dimension(400, 300)); // whatever

// GridBagConstraint for button
GridBagConstraints constraint = new GridBagConstraints();
constraint.anchor = GridBagConstraints.CENTER;
constraint.fill = GridBagConstraints.NONE;
constraint.gridx = 0;
constraint.gridy = GridBagConstraints.RELATIVE;
constraint.weightx = 1.0f;
constraint.weighty = 1.0f;

int sizeOfButtons = 50;
for(int i = 0; i < sizeOfButtons; i++) {
JButton button = new JButton();

button.setText("Button #" + i);

// other attributes you will set

buttonPanel.add(button, constraint);
}

pane.setViewportView(buttonPanel);
this.rootPane.add(pane); // or other panel etc.
pane.updateUI();

关于java - 使用 ScrollPane java 的动态按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15913623/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com