本文介绍了在Java中动态生成按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试动态生成一个表单。基本上,我想加载要购买的项目列表,并为每个项目生成一个按钮。我可以确认这些按钮是使用调试器生成的,但它们没有被显示。这是在 JPanel
的子类之内: private void generate() {
JButton b = new JButton(height test);
int btnHeight = b.getPreferredSize()。height;
int pnlHeight = this.getPreferredSize()。height;
int numButtons = pnlHeight / btnHeight;
setLayout(new GridLayout(numButtons,1));
迭代器<饮料> it = DrinkMenu.iterator(); (int i = 0; i< = numButtons; ++ i){
if(!it.hasNext()){
break;
}
final喝dr = it.next();
b = new DrinkButton(dr);
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
order.addDrink(dr);
}})
add(b);
}
revalidate();
}
DrinkButton
是一个 JButton
的子类。任何想法?
解决方案
在我的电脑上工作...
public class Panel扩展JPanel {
public Panel(){
setLayout(new java.awt.GridLayout(4,4)); (int i = 0; i JButton b = new JButton(String.valueOf(i));
b.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent e){
// ...
}
});
add(b);
}
}
public static void main(String [] args){
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run(){
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(new Dimension(300,300));
frame.add(new Panel());
frame.setVisible(true);
}
});
}
}
据我所知,您的版本一直工作虽然我不得不删除你的喝酒代码。从这个例子开始(它显示漂亮的4x4网格的按钮),并确定你的代码有什么问题。
I am trying to dynamically generate a form. Basically, I want to load a list of items for purchase, and generate a button for each. I can confirm that the buttons are being generated with the debugger, but they aren't being displayed. This is inside a subclass of JPanel
:
private void generate() {
JButton b = new JButton("height test");
int btnHeight = b.getPreferredSize().height;
int pnlHeight = this.getPreferredSize().height;
int numButtons = pnlHeight / btnHeight;
setLayout(new GridLayout(numButtons, 1));
Iterator<Drink> it = DrinkMenu.iterator();
for (int i = 0; i <= numButtons; ++i) {
if (!it.hasNext()) {
break;
}
final Drink dr = it.next();
b = new DrinkButton(dr);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
order.addDrink(dr);
}});
add(b);
}
revalidate();
}
DrinkButton
is a subclass of JButton
. Any ideas?
解决方案
Works on my computer...
public class Panel extends JPanel {
public Panel() {
setLayout(new java.awt.GridLayout(4, 4));
for (int i = 0; i < 16; ++i) {
JButton b = new JButton(String.valueOf(i));
b.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
//...
}
});
add(b);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run(){
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(new Dimension(300, 300));
frame.add(new Panel());
frame.setVisible(true);
}
});
}
}
As far as I remember your version was working as well, although I had to remove your "drinking" code. Start from this example (it shows nice 4x4 grid of buttons) and determine what is wrong with your code.
这篇关于在Java中动态生成按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!