简体   繁体   English

如何使用 Java Swing 创建简单的圆角矩形按钮?

[英]How do I create simple round-rectangle buttons with Java Swing?

I'm using a simple "Calculator" project as an example of rounded-rectangular buttons.我使用一个简单的“计算器”项目作为圆角矩形按钮的示例。 The entire project consists of one small class file.整个项目由一个小的 class 文件组成。 Here it is:这里是:

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.KeyEvent;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 * Swing layout management tutorial
 *
 * This program shows how to use the
 * GridLayout manager to create a
 * calculator skeleton.
 *
 * @author jan bodnar
 * website zetcode.com
 * last modified February 2009
 */
public class Calculator extends JFrame {

    public Calculator() {
        setTitle("Calculator");
        initUI();
        setSize(320, 290);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    public void initUI() {
        JMenuBar menubar = new JMenuBar();
        JMenu file = new JMenu("File");
        file.setMnemonic(KeyEvent.VK_F);
        menubar.add(file);
        setJMenuBar(menubar);

        String[] labels = {
            "Cls", "Bck", "", "Close",
            "7", "8", "9", "/",
            "4", "5", "6", "*",
            "1", "2", "3", "-",
            "0", ".", "=", "+"
        };
        JTextField field = new JTextField();
        add(field, BorderLayout.NORTH);
        JPanel buttonPanel = new JPanel(new GridLayout(5, 4, 3, 3));
        buttonPanel.setBorder(BorderFactory.createEmptyBorder(8, 0, 0, 0));

        for (String label: labels) {
            if (label.isEmpty()) {
                JLabel lbl = new JLabel();
                buttonPanel.add(lbl);
            } else {
                JButton button = new JButton(label);
                buttonPanel.add(button);
            }
        }
        add(buttonPanel, BorderLayout.CENTER);
    }

    public static void main(String[] args) {
        new Calculator();
    }
}

When this runs, it produces nice rectangular labelled buttons with a flat, slightly 3D appearance.当它运行时,它会产生漂亮的矩形标记按钮,具有扁平、略带 3D 的外观。 I attempted to get the same effect as follows:我试图获得如下相同的效果:

    FlowLayout flowLayoutManager = new FlowLayout(FlowLayout.RIGHT);
    JPanel lowerPanel = new JPanel(flowLayoutManager);
    lowerPanel.setBorder(
            BorderFactory.createEtchedBorder(
            EtchedBorder.RAISED));
    lowerPanel.setBackground(Color.ORANGE);
    lowerPanel.setPreferredSize(new Dimension(700, 100));
    lowerPanel.setMaximumSize(lowerPanel.getPreferredSize());
    lowerPanel.setMinimumSize(lowerPanel.getPreferredSize());

    /*
     * cardReaderButtonPanel holds three card reader control buttons
     */
    JPanel cardReaderButtonPanel = new JPanel(new GridLayout(1, 3, 10, 0));
    cardReaderButtonPanel.setBorder(BorderFactory.createEmptyBorder(8, 0, 0, 0));
    cardReaderButtonPanel.setOpaque(false); // transparent background

    String[] labels = {"Load", "Stop", "Start"};
    for (String label : labels) {
        JButton button = new JButton(label);
        cardReaderButtonPanel.add(button);
    }
    lowerPanel.add(cardReaderButtonPanel);

... but my buttons look like Mac OS X Aqua lozenges, Unlike in the Calculator example. ...但我的按钮看起来像 Mac OS X Aqua 菱形,与计算器示例不同。 I'm adding the panel of buttons to an enclosing panel that itself is enclosed by the center of a BorderLayout - but I don't see how that could influence the way a button is drawn.我正在将按钮面板添加到一个封闭面板,该面板本身由 BorderLayout 的中心包围 - 但我不明白这会如何影响按钮的绘制方式。

On Mac OS X, a button's appearance is provided by an instance of com.apple.laf.AquaButtonUI .在 Mac OS X 上,按钮的外观由com.apple.laf.AquaButtonUI的实例提供。 When the button's preferred size it within a particular range, it is displayed as shown here ;当按钮的首选大小在特定范围内时,显示如下所示 otherwise, it is displayed as shown here .否则,将显示为此处所示。 Note that both programs use a GridLayout ;请注意,这两个程序都使用GridLayout but the former uses the button's preferred size, while the latter stretches the buttons to fill the enclosing panel's preferred size.但前者使用按钮的首选大小,而后者拉伸按钮以填充封闭面板的首选大小。 Resize the frames to see the effect.调整框架大小以查看效果。

Your example uses FlowLayout , which relies on the button's preferred size.您的示例使用FlowLayout ,它依赖于按钮的首选大小。

Addendum: The center of BorderLayout behaves similarly to GridLayout in this regard.附录: BorderLayout的中心在这方面与GridLayout的行为类似。 Try adding a button to each of the five BorderLayout regions, and resize the frame to see the effect.尝试向五个BorderLayout区域中的每一个区域添加一个按钮,然后调整框架大小以查看效果。

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

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