贴出源码
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 .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;
}
}
}