1

我刚刚学习 Java 的时间很短,出于某种原因,这件小事让我一直坚持我目前的项目。我有一个jframe完整的更改/删除按钮,但我需要弄清楚如何让它们工作。我需要它在编码中实际更改它,并在实际代码中将其删除或永久更改。

package admin;

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.*;
import java.awt.event.*;

public class Project3 extends JFrame {

    private JPanel contentPane;
    private JTable table_1;
    private JScrollPane scrollPane;
    String[] columnNames = {"Restaurant", "Dish", "Type", "Price", "Rating"};
    Object[][] data = {
        {"Nemo", "Vesuvio", "Pizza", new String("65kr"), new Integer(7)},
        {"John", "Doe", "Rowing", new Integer(3), new Boolean(true)},
        {"Sue", "Black", "Knitting", new Integer(2), new Boolean(false)},
        {"Jane", "White", "Speed reading", new Integer(20),
            new Boolean(true)},
        {"Joe", "Brown", "Pool", new Integer(10), new Boolean(false)}};

    /**
    * Launch the application.
    */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            public void run() {
                try {
                    Project3 frame = new Project3();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
    * Create the frame.
    */
    public Project3() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 800, 600);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);
        JPanel panel = new JPanel();
        contentPane.add(panel, BorderLayout.CENTER);
        panel.setLayout(null);
        scrollPane = new JScrollPane();
        scrollPane.setBounds(139, 162, 469, 420);
        panel.add(scrollPane);
        table_1 = new JTable(data, columnNames);
        scrollPane.setViewportView(table_1);
        table_1.setFillsViewportHeight(true);
        table_1.setModel(new DefaultTableModel(new Object[][]{
                {"Nemo", "blah", "Pizza", "65kr", new Integer(7)},
                {"Nemo", "blah", "Rowing", "1 000 000kr", Boolean.TRUE},
                {"Sue", "blah", "Knitting", new Integer(2), Boolean.FALSE},
                {"Jane", "blah", "Speed reading", new Integer(20), Boolean.TRUE},
                {"Joe", "Brown", "Pool", new Integer(10), Boolean.FALSE},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},},
            new String[]{
                "Restaurant", "Dish", "Type", "Price", "Rating"}));
        JButton btnChangeadd = new JButton("Change/Add");
        btnChangeadd.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                // CALL CHANGE METHOD HERE
            }
        });
        btnChangeadd.setFont(new Font("Lucida Grande", Font.PLAIN, 16));
        btnChangeadd.setBounds(143, 104, 175, 42);
        panel.add(btnChangeadd);
        JLabel lblNewLabel = new JLabel("Welcome RestaurantID");
        lblNewLabel.setFont(new Font("Lucida Grande", Font.PLAIN, 24));
        lblNewLabel.setBounds(264, 56, 260, 36);
        panel.add(lblNewLabel);
        JButton button = new JButton("+");
        button.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                // CALL SOMETHING HERE IF YOU WANT
            }
        });
        button.setBounds(66, 248, 40, 29);
        panel.add(button);
        JButton button_1 = new JButton("-");
        button_1.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
            }
        });
        button_1.setBounds(66, 289, 40, 29);
        panel.add(button_1);
        JButton btnDelete = new JButton("Delete");
        btnDelete.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                //DELETE METHOD
            }
        });
        btnDelete.setFont(new Font("Lucida Grande", Font.PLAIN, 16));
        btnDelete.setBounds(344, 104, 191, 42);
        panel.add(btnDelete);
        JButton btnLogOut = new JButton("Log out");
        btnLogOut.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
            }
        });
        btnLogOut.setBounds(667, 6, 117, 29);
        panel.add(btnLogOut);
    }
}
4

2 回答 2

2

我认为您正在寻找 ActionListener 而不是 MouseListener。您可以像添加 MouseListener 一样将 actionlistener 添加到 JButton。

button.addActionListener(new ActionListener(){  
    public void actionPerformed(ActionEvent e){  
        //code you want to run when the button gets pressed  
    }  
 }  

单击按钮时将运行 actionPerformed 方法。这是编写动作监听器的好教程:http: //docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html

在那个教程中,他们做的事情有点不同。他们有一个扩展 ActionListener 并实现 actionPerformed 方法的类。如果以这种方式完成,那么您将整个类作为 actionlistener 添加到按钮。因此,如果您的按钮是在扩展 actionlistener 的类中创建的,您可以这样做:
button.addActionListener(this);
这允许您为多个按钮使用一个 actionPerformed 方法。要知道在 actionPerformed 方法中单击了哪个按钮,您可以像这样调用 ActionEvent 对象的 getSource 方法:
if(e.getSource() == button)
如果您需要更多关于让类扩展 ActionListener 的说明,请告诉我。

编辑:
绝对误解了你的要求。不过,我仍然建议使用动作监听器,而不是使用鼠标监听器处理按钮按下的方式。如果您澄清要如何更改数据,我可能会提供帮助。

于 2012-11-24T14:04:10.440 回答
0

如果要删除单击的 btn,请执行以下操作:

面板.remove(); 
面板.验证();
panel.repaint();
此外,您可以通过 e.getSource() 内部操作执行方法访问单击的源。

于 2012-11-24T13:05:50.547 回答