简体   繁体   English

JTable分裂单元格。 如何

[英]JTable splitting cells. How to

I'm new to Swing and I want to create a table having this form: 我是Swing的新手,我想创建一个具有以下形式的表:

在此输入图像描述

So How to split a cell like the table shows? 那么如何分割像桌子一样的细胞? Have you any useful links or tutorials or an idea? 你有任何有用的链接或教程或想法吗?

This looks like a bowling score card to me. 这对我来说看起来像一张保龄球记分卡。 Based on that assumption, the number of columns is fixed, and the number of entries tends to be 6 or less. 基于该假设,列数是固定的,并且条目的数量趋于为6或更小。 Since you likely won't need scrolling, I would recommend a fixed grid of components instead of a JTable. 由于您可能不需要滚动,我建议使用固定的组件网格而不是JTable。

This could easily be acheived using GridBagLayout. 使用GridBagLayout可以很容易地实现这一点。 For the name, I'd use a JTextArea. 对于名称,我使用JTextArea。 For the 2 scoring cells for each frame, I'd use JTextFields. 对于每个帧的2个评分单元格,我将使用JTextFields。 For the bottom 2-column-span component that holds the frame's score I'd probably use a JLabel. 对于保持框架得分的底部2列跨度组件,我可能使用JLabel。

Put all of this on a JPanel, and recreate the panel for each bowler. 将所有这些放在JPanel上,并为每个投球手重新创建面板。

EDIT: Here's a quick mock-up just to show the concept. 编辑:这是一个快速模型,只是为了展示这个概念。 Not necessarily visually pretty, but I'll leave that as an exercise for the reader: 不一定在视觉上很漂亮,但我会将其作为练习留给读者:

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class BowlingScoreCard implements Runnable
{
  public static void main(String[] args)
  {
    SwingUtilities.invokeLater(new BowlingScoreCard());
  }

  public void run()
  {
    JFrame frame = new JFrame("Demo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(createScorecard(4), BorderLayout.CENTER);
    frame.pack();
    frame.setVisible(true);
  }

  private JPanel createScorecard(int numPlayers)
  {
    JPanel p = new JPanel(new GridBagLayout());

    p.add(new JLabel("Player"), gbc(0, 0, 1, 1));
    for (int x = 1; x <= 10; x++)
    {
      p.add(new JLabel(Integer.toString(x)), gbc(x, 0, 1, 1));
    }

    for (int y = 1; y <= numPlayers; y++)
    {
      JTextArea textArea = new JTextArea(2, 10);
      p.add(textArea, gbc(0, y, 1, 1));

      for (int i = 1; i <= 9; i++)
      {
        p.add(createFrame(2), gbc(i, y, 1, 1));
      }

      p.add(createFrame(3), gbc(10, y, 1, 1));
    }

    return p;
  }

  private JPanel createFrame(int entries)
  {
    JLabel label = new JLabel(" ");
    label.setBackground(Color.GRAY);

    JPanel p = new JPanel(new GridBagLayout());
    p.setBorder(BorderFactory.createLineBorder(Color.GRAY, 1));
    for (int i = 0; i < entries; i++)
    {
      p.add(new JTextField(3), gbc(i, 0, 1, 1));
    }
    p.add(label, gbc(0, 1, 2, 1));
    return p;
  }

  private GridBagConstraints gbc(int x, int y, int colspan, int rowspan)
  {
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridx = x;
    gbc.gridy = y;
    gbc.gridwidth = colspan;
    gbc.gridheight = rowspan;
    gbc.weightx = 0;
    gbc.weighty = 0;
    gbc.anchor = GridBagConstraints.CENTER;
    gbc.fill = GridBagConstraints.NONE;
    return gbc;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM