如何与动态添加的 swing 组件进行交互?
How to interact with a dynamically added swing component?
下面的代码片段将在按下 "button" 时创建一个新的 Button。我想知道是否有任何方法可以为这个新按钮指定名称、Action Listener 或任何其他属性以便使用它。
button.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
panel.add(new JButton("Hello"));
frame.revalidate();
frame.repaint();
}
});
我正在寻找的示例:按下按钮时,创建一个新按钮,该按钮具有名称并在单击时执行操作。 (最好我可以在多次点击时动态制作多个按钮)
调用test();进入第一个按钮的 actionPerformed()。
每次单击第一个按钮都会动态创建另一个按钮并单独命名。这些按钮中的每一个还可以动态创建单独命名的按钮。
//The counter is used to name the buttons individually
//It doesn´t matter wich of the created buttons will call test()
//any new button will have a different name than the other buttons
public int counter;
public void test() {
JButton btn = new JButton();
//Now you can edit the properties
//btn.setLocation
//btn.setSize
//btn.setVisible
//btn...
btn.setText("Hello");
btn.setName("Button" + counter);
panel.add(btn);
//Add of listener is untested, but it should work like this
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
test();
}
});
counter ++;
}
您必须先创建 JButton
:
JButton but = new JButton();
but.setText("Button");
然后添加侦听器。在重写的 actionPerformed()
中实现单击按钮时发生的操作:
but.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Actions
}
});
最后添加到面板:
panel.add(but);
方法如下:
JTextField textfield= new JTextField();
button.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
JButton btn=new JButton();
btn.setText(textfield.getText().toString());
textfield.setText("");
btn.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
//the action on the button
}
});
panel.add(btn);
frame.revalidate();
frame.repaint();
}
});
简单,使用 AbstractAction:
Icon myIcon = null;
JButton btn = new JButton(new AbstractAction("ClickMe", myIcon) {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showInputDialog("hello");
}
});
panel.add(btn);
干杯!
下面的代码片段将在按下 "button" 时创建一个新的 Button。我想知道是否有任何方法可以为这个新按钮指定名称、Action Listener 或任何其他属性以便使用它。
button.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
panel.add(new JButton("Hello"));
frame.revalidate();
frame.repaint();
}
});
我正在寻找的示例:按下按钮时,创建一个新按钮,该按钮具有名称并在单击时执行操作。 (最好我可以在多次点击时动态制作多个按钮)
调用test();进入第一个按钮的 actionPerformed()。
每次单击第一个按钮都会动态创建另一个按钮并单独命名。这些按钮中的每一个还可以动态创建单独命名的按钮。
//The counter is used to name the buttons individually
//It doesn´t matter wich of the created buttons will call test()
//any new button will have a different name than the other buttons
public int counter;
public void test() {
JButton btn = new JButton();
//Now you can edit the properties
//btn.setLocation
//btn.setSize
//btn.setVisible
//btn...
btn.setText("Hello");
btn.setName("Button" + counter);
panel.add(btn);
//Add of listener is untested, but it should work like this
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
test();
}
});
counter ++;
}
您必须先创建 JButton
:
JButton but = new JButton();
but.setText("Button");
然后添加侦听器。在重写的 actionPerformed()
中实现单击按钮时发生的操作:
but.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Actions
}
});
最后添加到面板:
panel.add(but);
方法如下:
JTextField textfield= new JTextField();
button.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
JButton btn=new JButton();
btn.setText(textfield.getText().toString());
textfield.setText("");
btn.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
//the action on the button
}
});
panel.add(btn);
frame.revalidate();
frame.repaint();
}
});
简单,使用 AbstractAction:
Icon myIcon = null;
JButton btn = new JButton(new AbstractAction("ClickMe", myIcon) {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showInputDialog("hello");
}
});
panel.add(btn);
干杯!