Java swing选项卡中有表格,表格中有按钮,按钮按不到

牧头 发布于 02/09 10:56
阅读 109
收藏 0

贴出源码

package com.java.gui;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.image.ImageConsumer;
import java.net.URL;

public class JTabbedPaneDemo extends JPanel {
    private JTabbedPane jTabbedPane = new JTabbedPane();
    private String[] tabNames = {"选项卡1", "选项卡2", "表格"};
    private ImageIcon icon = createImageIcon("middle.png");

    public JTabbedPaneDemo() {
        layoutComponents();
    }

    private void layoutComponents() {
        int i = 0;
        JPanel jpanelFirst = new JPanel();
        jTabbedPane.addTab(tabNames[i++], icon, jpanelFirst, "fitst");
        jTabbedPane.setMnemonicAt(0, KeyEvent.VK_0);

        JLabel jLabel1 = new JLabel("选项卡1:");
        jpanelFirst.add(jLabel1);

        JTextField jTextField1 = new JTextField(20);
        jTextField1.setText("测试选项卡1");
        jpanelFirst.add(jTextField1);

        JButton jButton1 = new JButton("提交");
        jButton1.addActionListener((ActionEvent e) -> {
            JFrame jFrame = new JFrame();
            JLabel jLabel = new JLabel();
            jLabel.setIcon(icon);
            jFrame.add(jLabel);
            JOptionPane.showMessageDialog(jFrame, "提交成功!!!", "选项卡1提交", JOptionPane.INFORMATION_MESSAGE);
        });
        jpanelFirst.add(jButton1);


        JPanel jpanelSecond = new JPanel();
        // 加入第2个页面
        jTabbedPane.addTab(tabNames[i++], icon, jpanelSecond, "second");
        // 设置快捷键为1
        jTabbedPane.setMnemonicAt(1, KeyEvent.VK_1);

        JLabel jLabel2 = new JLabel("选项卡2:");
        jpanelSecond.add(jLabel2);

        JTextField jTextField2 = new JTextField(20);
        jTextField2.setText("测试选项卡2");
        jpanelSecond.add(jTextField2);

        JButton jButton2 = new JButton("提交");
        jButton2.addActionListener((ActionEvent e) -> {
            JOptionPane.showMessageDialog(null, "提交成功!!!", "选项卡2提交", JOptionPane.INFORMATION_MESSAGE);
        });
        jpanelSecond.add(jButton2);

        JPanel jpanel3 = new JPanel();
        jTabbedPane.addTab(tabNames[i++], icon, jpanel3, "第三个");
        jTabbedPane.setMnemonicAt(2, KeyEvent.VK_2);

        String[] columsNames = {"序号", "姓名", "语文", "数学", "英语", "总分", "操作"};
        Object[][] rowData = {
                {1, "张三", 80, 80, 80, 240, null},
                {2, "John", 70, 80, 90, 240, null},
                {3, "Sue", 70, 70, 70, 210, null},
                {4, "Jane", 80, 70, 60, 210, null},
                {5, "Joe_05", 80, 70, 60, 210, null},
                {6, "Joe_06", 80, 70, 60, 210, null},
                {7, "Joe_07", 80, 70, 60, 210, null},
                {8, "Joe_08", 80, 70, 60, 210, null},
                {9, "Joe_09", 80, 70, 60, 210, null},
                {10, "Joe_10", 80, 70, 60, 210, null},
                {11, "Joe_11", 80, 70, 60, 210, null},
                {12, "Joe_12", 80, 70, 60, 210, null},
                {13, "Joe_13", 80, 70, 60, 210, null},
                {14, "Joe_14", 80, 70, 60, 210, null},
                {15, "Joe_15", 80, 70, 60, 210, null},
                {16, "Joe_16", 80, 70, 60, 210, null},
                {17, "Joe_17", 80, 70, 60, 210, null},
                {18, "Joe_18", 80, 70, 60, 210, null},
                {19, "Joe_19", 80, 70, 60, 210, null},
                {20, "Joe_20", 80, 70, 60, 210, null}
        };

        JTable table = new JTable(rowData, columsNames);
        // 设置表格内容颜色
        table.setForeground(Color.BLACK);                   // 字体颜色
        table.setFont(new Font(null, Font.PLAIN, 14));      // 字体样式
        table.setSelectionForeground(Color.DARK_GRAY);      // 选中后字体颜色
        table.setSelectionBackground(Color.LIGHT_GRAY);     // 选中后字体背景
        table.setGridColor(Color.GRAY);


        // 设置行高
        table.setRowHeight(30);

        // 第一列列宽设置为40
        table.getColumnModel().getColumn(0).setPreferredWidth(40);

        // 设置滚动面板视口大小(超过该大小的行数据,需要拖动滚动条才能看到)
        table.setPreferredScrollableViewportSize(new Dimension(800, 300));
        table.getColumnModel().getColumn(6).setCellRenderer(new MyButtonRender());
        DefaultTableModel defaultTableModel = new DefaultTableModel();
        defaultTableModel.addRow(new Object[]{3, "Sue", 70, 70, 70, 210, null});

        JScrollPane jsp = new JScrollPane(table);
        jpanel3.add(jsp);

        setLayout(new GridLayout(1, 1));
        add(jTabbedPane);
    }

    private ImageIcon createImageIcon(String path) {

        URL url = JTabbedPaneDemo.class.getResource(path);
        if (url == null) {
            System.out.println("the image " + path + " is not exist!");
            return null;
        }
        return new ImageIcon(url);

//        return new ImageIcon("/Users/mt-mac/IdeaProjects/JavaGUI/src/main/java/com/java/gui/middle.png");
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            JFrame jFrame = new JFrame();
            jFrame.setLayout(null);
            jFrame.setVisible(true);
            jFrame.setContentPane(new JTabbedPaneDemo());
            jFrame.setSize(800, 800);
        });
    }

    public class MyButtonRender implements TableCellRenderer {

        private JPanel jPanel;
        private JButton jButton;

        public MyButtonRender() {
            initButton();
            initPanel();
            jPanel.add(jButton);
        }

        private void initPanel() {
            jPanel = new JPanel();
            jPanel.setLayout(null);
        }

        private void initButton() {
            jButton = new JButton();
            jButton.setBounds(2, 3, 80, 30);
            jButton.addActionListener((ActionEvent e) -> {
                System.out.println("" + e.getActionCommand());
                System.out.println(jButton.getText());
            });
        }

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            jButton.setText("编辑");
            return jPanel;
        }
    }
}

 

加载中
0
tcxu
tcxu

山东彩票_[官网首页]楼主没有将表格"操作列"中的多个"编辑"按钮,预先声明成类 JTabbedPaneDemo  的成员,所以"按钮按不到",即不起作用。
试问,每行设计的"编辑"按钮,它们的作用是什么?如果只是要 编辑/修改 某一单元的数据,那先用鼠标双击这个给定的单元格就可以通过键盘修改了。看来,这些"编辑"按钮,是多余的。
建议在表格的右下方,仅设置一个"保存"按钮就够了。这样,在用户修改完表格之后,只需按一下"保存"按钮,将表格现有数据,保存一下,就行了。
保存的作法:

  1. 山东彩票_[官网首页]使用嵌套的for语句,依次调用 JTable 的 getValueAt(int row, int colomn), 给你的 rowData 数组从新赋值。
  2. 用更新的 rowData, 从新创建一下这个表格。
牧头
牧头
是这样的,首先谢谢你的回答 1. 这个是根据网上demo写的,没有选项卡时时正常的,将表格放入选项卡中编辑按钮失效 2. 之后还会增加删除按钮,所以这个问题需要解决一下,目前不知道什么情况,可能是图层问题吧
返回顶部
顶部

页面底部区域 foot.htm