gpt4 book ai didi

java - 如何向在另一个按钮内创建的按钮添加功能?

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

我的问题是我不知道如何添加它的操作,你看,一些按钮已经被编程了,每个按钮都有人(安装者)的名字,它的功能是当你点击时,我会自动复制您的 id ,然后随着时间的推移,新的安装程序将会出现,我希望能够使我的程序适合普通人,然后我想在安装程序之外添加一个按钮,以便他们可以添加越来越多的内容,我需要该人创建的每个按钮都具有与其他按钮类似的功能。

这是我的限制,我不知道你的函数会去哪里或如何去:

private void addInstallerActionPerformed(ActionEvent evt){
JButton newInstaller = new JButton("New Button");
panel.add(newInstaller);
validate();
}

这是我的整个代码:

class Billing {

public static void main(String[] args) {

mainWindow w = new mainWindow();
w.setVisible(true);
w.setSize(1280,720);
w.setLocationRelativeTo(null);

}
}

class mainWindow extends JFrame implements ActionListener{

JPanel panel;
JButton andresTorres, alexanderHernandez, eduardoRuiz, jorgeFley, addInstaller;
public mainWindow(){

panel = new JPanel();
//Instaladores
andresTorres = new JButton("Andres Torres");
alexanderHernandez = new JButton("Alexander Hernandez");
eduardoRuiz = new JButton("Eduardo Ruiz");
jorgeFley = new JButton("Jorge Fley");
addInstaller = new JButton("add Installer");
//End

add(panel);

panel.add(andresTorres);
panel.add(alexanderHernandez);
panel.add(eduardoRuiz);
panel.add(jorgeFley);
panel.add(addInstaller);

setTitle("Billing assistant");
setDefaultCloseOperation(this.EXIT_ON_CLOSE);

andresTorres.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
andresTorresActionPerformed(evt);
}
});


alexanderHernandez.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
alexanderHernandezActionPerformed(evt);
}
});

eduardoRuiz.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
eduardoRuizActionPerformed(evt);
}
});

jorgeFley.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
jorgeFleyActionPerformed(evt);
}
});

addInstaller.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
addInstallerActionPerformed(evt);
}
});
}

private void addInstallerActionPerformed(ActionEvent evt){
JButton newInstaller = new JButton("New Button");
panel.add(newInstaller);
validate();
}

private void andresTorresActionPerformed(ActionEvent evt){
String myString = "here goes your identification";
StringSelection stringSelection = new StringSelection(myString);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(stringSelection, null);
}

private void alexanderHernandezActionPerformed(ActionEvent evt){
String myString = "here goes your identification";
StringSelection stringSelection = new StringSelection(myString);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(stringSelection, null);
}

private void eduardoRuizActionPerformed(ActionEvent evt){
String myString = "here goes your identification";
StringSelection stringSelection = new StringSelection(myString);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(stringSelection, null);
}

private void jorgeFleyActionPerformed(ActionEvent evt){
String myString = "here goes your identification";
StringSelection stringSelection = new StringSelection(myString);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(stringSelection, null);
}

@Override
public void actionPerformed(ActionEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

}

请帮助我。

最佳答案

代码冗余太多,而 OOP 不够——没有足够的独立对象,每个对象都有自己的状态和行为。我推荐:

  • 首先,创建一个 Person 类,该类将人名保存为字符串,以及要复制到与该名称关联的剪贴板的 ID 信息,以及任何其他字段和方法。将字段设置为私有(private)实例字段,为类提供适当的构造函数和 getter(可能还有 setter)。
  • 创建一个 ActionListener 类(或者更好的是一个 AbstractAction,它就像“类固醇”的 ActionListener),在其私有(private)字段中保存单个 Person 对象。通过构造函数参数传递此 Person。在监听器中,添加将此人的 ID 放入剪贴板的代码。

例如最简单的例子:

public class Person {
String name;
String identification;

public Person(String name, String identification) {
this.name = name;
this.identification = identification;
}

public String getName() {
return name;
}

public String getIdentification() {
return identification;
}

}

class PersonAction extends AbstractAction {
private Person person;

public PersonAction(Person person) {
super(person.getName());
this.person = person;
}

@Override
public void actionPerformed(ActionEvent e) {
StringSelection stringSelection = new StringSelection(person.getIdentification());

Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(stringSelection, null);
}
}

然后在安装程序按钮的 ActionListener 中,您可以使用此操作创建按钮并将其添加到需要的位置。例如:

        @Override
public void actionPerformed(ActionEvent e) {

// assuming two JTextFields in the app, nameField and idField
String name = nameField.getText();
String identification = idField.getText();

// create new Person with the name and id information
Person person = new Person(name, identification);

// create a new PersonAction with the Person above
// and pass into a JButton
JButton personButton = new JButton(new PersonAction(person));

// assuming a JPanel called personPanel that holds these JButtons
personPanel.add(personButton); // add the button
personPanel.revalidate(); // tell the panel to relayout components
personPanel.repaint(); // and repaint
}

完整的简单示例:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import javax.swing.*;

@SuppressWarnings("serial")
public class MainWindowApp2 extends JPanel {
private JPanel personPanel = new JPanel(new GridLayout(0, 1, 4, 4));
private JTextField nameField = new JTextField(10);
private JTextField idField = new JTextField(10);

public MainWindowApp2() {
personPanel.add(new JButton(new PersonAction(new Person("Andres Torres", "AT 001"))));
personPanel.add(new JButton(new PersonAction(new Person("Alexander Hernandez", "AH 002"))));
personPanel.add(new JButton(new PersonAction(new Person("Eduardo Ruiz", "ER 003"))));
personPanel.add(new JButton(new PersonAction(new Person("Jorge Fley", "JF 004"))));
personPanel.add(new JButton(new PersonAction(new Person("Miguél Gonzales", "MG 005"))));

JButton addInstallerButton = new JButton(new AbstractAction("Add Installer") {

@Override
public void actionPerformed(ActionEvent e) {
String name = nameField.getText();
String identification = idField.getText();
Person person = new Person(name, identification);

JButton personButton = new JButton(new PersonAction(person));
personPanel.add(personButton);
personPanel.revalidate();
personPanel.repaint();
}
});

JPanel topPanel = new JPanel();
topPanel.add(new JLabel("Name:"));
topPanel.add(nameField);
topPanel.add(new JLabel("ID:"));
topPanel.add(idField);
topPanel.add(addInstallerButton);

JPanel personWrapperPanel = new JPanel(new BorderLayout());
personWrapperPanel.add(personPanel, BorderLayout.PAGE_START);

JScrollPane scrollPane = new JScrollPane(personWrapperPanel);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

setPreferredSize(new Dimension(600, 600));
setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
setLayout(new BorderLayout(4, 4));
add(scrollPane);
add(topPanel, BorderLayout.PAGE_START);
}

private static void createAndShowGui() {
MainWindowApp2 mainPanel = new MainWindowApp2();

JFrame frame = new JFrame("MainWindowApp2");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}

关于java - 如何向在另一个按钮内创建的按钮添加功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51225006/

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