英文:
Java dynamically create buttons and pass a parameter to action performed
问题
public class UsersView implements ActionListener {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
public UsersView() {
// 创建 16 个按钮。
for (int i=0; i<16; i++) {
Button button = new Button("点击 " + i);
button.setId(i); // 这里会报错,因为找不到符号 'setId'
panel.add(button);
button.addActionListener(this);
}
panel.setBorder(BorderFactory.createBevelBorder(0, Color.lightGray, Color.yellow));
//panel.setBorder(BorderFactory.createEmptyBorder(300, 300, 100, 300));
panel.setLayout(new GridLayout(4,4)); // 行 列
frame.add(panel, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("应用 GUI");
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
// TODO: 代码应用逻辑
new UsersView();
}
// 在按钮点击时。
@Override
public void actionPerformed(ActionEvent e) {
// 我知道这里还什么都没有(暂时),因为 'setId' 给我报错。
}
}
英文:
I am new to java and i am trying a small project on my own, i want to list the first and lastname of users from a sql database (this all works fine, but i don't just want to list them
I want to list all users in a GUI with a delete button, naturally this delete button will be generated dynamically and i want to pass the userID of the button to the action performed.
like this:
John Doe 'Delete button'
Jane Doe 'Delete button'
In my code below i just generate 16 buttons dynamically (without a users table) instead of passing the userID i am trying to pass the 'i' value of the for loop, but my code does not seem to work
CODE
public class UsersView implements ActionListener {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
public UsersView() {
//Create the 16 buttons.
for (int i=0; i<16; i++) {
Button button = new Button("Click "+i);
button.setId(i); //this gives me and error 'Symbol not find' on the 'setId'
panel.add(button);
button.addActionListener(this);
}
panel.setBorder(BorderFactory.createBevelBorder(0, Color.lightGray, Color.yellow));
//panel.setBorder(BorderFactory.createEmptyBorder(300, 300, 100, 300));
panel.setLayout(new GridLayout(4,4)); //Rows Cols
frame.add(panel, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("App GUI");
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
// TODO code application logic here
new UsersView();
}
//On button click.
@Override
public void actionPerformed(ActionEvent e) {
//I know i have nothing here (yet) that is because the 'setId' gives me an error.
}
}
答案1
得分: 1
你遇到的一个问题是创建一个巨大的动作监听器,然后期望从中委托动作。Java 中一个不错的特性是匿名类,或者自 Java 7 起的 Lambda 表达式。
JButton button = new JButton("Click " + i);
panel.add(button);
int id = i;
button.addActionListener(evt -> {
performActionOnId(id);
});
现在,主类不再是一个动作监听器,而是具有描述性方法的主类。
public void addUser(User a) {
// 仅展示删除按钮。
JButton delete = new JButton("X");
delete.addActionListener(evt -> {
removeUser(a);
// 清理界面。
});
}
这将在创建用户时的某些委托步骤放置在了那里。否则,你将不得不在你的动作执行中进行委托。
public void actionPerformed(ActionEvent evt) {
// 必须是一个新类才能具有 id 属性。
CustomButton b = (CustomButton) evt.getSource();
int id = b.getId();
User u = getUserById(id);
removeUser(u);
}
英文:
One of the issues you're having is creating a monolithic action listener, and then expecting to delegate actions from that. One nice feature of java are anonymous classes, or since java 7 lambdas.
JButton button = new JButton("Click " + i);
panel.add(button);
int id = i;
button.addActionListener( evt->{
performActionOnId( id );
});
Now instead of having the main class be an action listener, the main class has methods that are descriptive.
public void addUser( User a ){
//just showing delete button.
JButton delete = new JButton("X");
delete.addActionListener( evt ->{
removeUser( a );
//clean up gui.
});
}
This puts some of the steps of delegation at the creation of the user. Otherwise you'll have to delegate in your action performed.
public void actionPerformed( ActionEvent evt ){
//has to be a new class to have id attribute.
CustomButton b = (CustomButton)evt.getSource();
int id = b.getId();
User u = getUserById(id);
removeUser(u);
}
答案2
得分: 0
使用JButton
代替Button
。混合使用AWT和Swing组件很少能正常工作。如果您想为组件设置自定义字段,只需对其进行子类化(直接子类化或使用组合的装饰者模式)。例如:
public class IdButton extends JButton {
private int id;
public IdButton(String label, int id) {
super(label);
this.id = id;
}
public int getId() {
return id;
}
}
J/Button
类本身没有set/getId
方法。
英文:
Use a JButton
instead of Button
. Mixing AWT and Swing components rarely works well, if at all. If you want to set a custom field for a component, just subclass it (use direct subclassing or a decorator pattern with composition). E.g.:
public class IdButton extends JButton {
private int id;
public IdButton(String label, int id) {
super(label);
this id = id;
}
public int getId() {
return id;
}
}
The J/Button
classes do not have set/getId
methods on it's own.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论