以图形方式将按钮连接

以图形方式将按钮连接

本文介绍了以图形方式将按钮连接/链接在一起吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个简单的问题,是否有一种方法可以创建一种按钮组"(不,不是),其中两个或多个按钮连接在一起.例如,如果您在Mac系统上查看JTabbedPanes,则会得到以下信息:

Simple question, is there a way to create a sort of "button group" (no, not this), where two or more buttons are connected together. If you look at JTabbedPanes, for example, on a mac system you get this:

这正是我想要的一组按钮.将两个或多个按钮推到一起,就像在中看到的一样,由于边框布局冲突且不均匀,因此不起作用:

This is exactly what I'd like to have with a set of buttons. Pushing two or more buttons together, like seen in this post, does not work as the border layouts conflict and is also uneven:

我已经搜索了一段时间,但没有找到任何东西.现在,我的确知道我可以从头开始创建一个全新的组件,但是我想知道是否有更好的方法可以做到这一点.

I have been searching around for a while on anything like this but I haven't been able to find anything. Now, I do understand that I could create a whole new component from scratch, but I was wondering if there was a better way to do this.

我尝试解决JTabbedPanes的问题,但是这些选项卡"不是传统意义上的按钮,它们可以为您提供全部功能.它们受到严格的限制,此外,总体而言,JTabbedPane并不是可行的跨平台工具,因为我只希望Mac显示外观.

I tried screwing around with JTabbedPanes, but these "tabs" are not buttons in the traditional sense that give you full capabilities. They are heavily restricted and in addition overall JTabbedPane is not a viable cross platform tool as I just want the look-and-feel displayed by mac.

对此有什么解决办法吗?

Are there any solutions to this?

推荐答案

正如@MadProgrammer所说,这是一个使用FlowLayoutJPanel添加一堆关闭按钮的示例:

As @MadProgrammer says, here is an example of adding a bunch off buttons to JPanel using a FlowLayout:

import java.awt.*;
import java.awt.geom.*;
import java.util.*;
import javax.swing.*;

public class StickButtonsTest {
  private JComponent makeUI() {
    JPanel p = new JPanel();
    p.setBorder(BorderFactory.createEmptyBorder(10, 5, 0, 5));
    p.add(makeToggleButtonBar());
    return p;
  }
  private static AbstractButton makeButton(String title) {
    AbstractButton b = new JToggleButton(title) {
      @Override protected void fireStateChanged() {
        ButtonModel model = getModel();
        if (model.isSelected()) {
          setForeground(Color.ORANGE.brighter());
        } else {
          setForeground(Color.WHITE);
        }
        super.fireStateChanged();
      }
    };
    b.setHorizontalTextPosition(SwingConstants.CENTER);
    b.setBorder(BorderFactory.createEmptyBorder());
    b.setContentAreaFilled(false);
    b.setFocusPainted(false);
    b.setForeground(Color.WHITE);
    b.setBackground(new Color(0x00AEF3));
    b.setIcon(new ToggleButtonBarCellIcon());
    return b;
  }
  private static JPanel makeToggleButtonBar() {
    ButtonGroup bg = new ButtonGroup();
    JPanel p = new JPanel(new GridLayout(1, 0, 0, 0));
    for (AbstractButton b: Arrays.asList(
        makeButton("left"), makeButton("c1"),
        makeButton("c2"), makeButton("right"))) {
      bg.add(b);
      p.add(b);
    }
    return p;
  }
  public static void main(String... args) {
    EventQueue.invokeLater(() -> {
      JFrame f = new JFrame();
      f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      f.getContentPane().add(new StickButtonsTest().makeUI());
      f.setSize(320, 240);
      f.setLocationRelativeTo(null);
      f.setVisible(true);
    });
  }
  public static void createAndShowGUI() {
  }
}

//http://ateraimemo.com/Swing/ToggleButtonBar.html
class ToggleButtonBarCellIcon implements Icon {
  @Override public void paintIcon(Component c, Graphics g, int x, int y) {
    Container parent = c.getParent();
    if (!(parent instanceof JPanel)) {
      return;
    }
    int gap = 3;
    int r = 8;
    int w = c.getWidth();
    int h = c.getHeight() - 1;

    Graphics2D g2 = (Graphics2D) g.create();
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                        RenderingHints.VALUE_ANTIALIAS_ON);
    Path2D.Float p = new Path2D.Float();

    if (c == parent.getComponent(0)) {
      //:first-child
      p.moveTo(x, y + r);
      p.quadTo(x, y, x + r, y);
      p.lineTo(x + w, y);
      p.lineTo(x + w, y + h);
      p.lineTo(x + r, y + h);
      p.quadTo(x, y + h, x, y + h - r);
    } else if (c == parent.getComponent(parent.getComponentCount() - 1)) {
      //:last-child
      w--;
      p.moveTo(x, y);
      p.lineTo(x + w - r, y);
      p.quadTo(x + w, y, x + w, y + r);
      p.lineTo(x + w, y + h - r);
      p.quadTo(x + w, y + h, x + w - r, y + h);
      p.lineTo(x, y + h);
      p.moveTo(x, y + gap);
      p.lineTo(x, y + h - gap);
    } else {
      p.moveTo(x, y);
      p.lineTo(x + w, y);
      p.lineTo(x + w, y + h);
      p.lineTo(x, y + h);
      p.moveTo(x, y + gap);
      p.lineTo(x, y + h - gap);
    }
    p.closePath();

    Color ssc = new Color(1f, 1f, 1f, .2f);
    Color bgc = new Color(0f, 0f, 0f, .2f);
    if (c instanceof AbstractButton) {
      ButtonModel m = ((AbstractButton) c).getModel();
      if (m.isSelected() || m.isRollover()) {
        ssc = new Color(1f, 1f, 1f, .4f);
        bgc = new Color(1f, 1f, 1f, .1f);
      }
    }

    Area area = new Area(p);
    g2.setPaint(c.getBackground());
    g2.fill(area);
    g2.setPaint(new GradientPaint(x, y, ssc, x, y + h, bgc, true));
    g2.fill(area);
    g2.setPaint(new Color(0f, 0f, 0f, .5f));
    g2.draw(p);

    g2.dispose();
  }
  @Override public int getIconWidth() {
    return 60;
  }
  @Override public int getIconHeight() {
    return 24;
  }
}

这篇关于以图形方式将按钮连接/链接在一起吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 14:00