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

发布于 2024-11-19 21:52:38 字数 3164 浏览 1 评论 0原文

我使用一个简单的“计算器”项目作为圆角矩形按钮的示例。整个项目由一个小的类文件组成。它是这样的:

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();
    }
}

当它运行时,它会生成漂亮的矩形标签按钮,具有平坦的、略带 3D 的外观。我试图获得相同的效果,如下所示:

    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);

...但是我的按钮看起来像 Mac OS X Aqua 菱形!与计算器示例不同,我将按钮面板添加到一个封闭面板中,该面板本身被 BorderLayout 的中心包围 - 但我不知道这会如何影响按钮的绘制方式。

I'm using a simple "Calculator" project as an example of rounded-rectangular buttons. The entire project consists of one small class file. 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. 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, 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

给妤﹃绝世温柔 2024-11-26 21:52:38

在 Mac OS X 上,按钮的外观由 com.apple.laf.AquaButtonUI 的实例提供。当按钮的首选尺寸在特定范围内时,其显示方式如下此处 ;否则,其显示方式如此处所示。请注意,这两个程序都使用 GridLayout;但前者使用按钮的首选尺寸,而后者则拉伸按钮以填充封闭面板的首选尺寸。调整框架大小以查看效果。

您的示例使用 FlowLayout,它依赖于按钮的首选大小。

附录:在这方面,BorderLayout 的中心行为与 GridLayout 类似。尝试向五个 BorderLayout 区域中的每一个区域添加一个按钮,然后调整框架大小以查看效果。

On Mac OS X, a button's appearance is provided by an instance of 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; 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.

Addendum: The center of BorderLayout behaves similarly to GridLayout in this regard. Try adding a button to each of the five BorderLayout regions, and resize the frame to see the effect.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文