如何在秋千中制作齐平的JButton?

时间:2016-07-22 08:39:30

标签: java swing layout

我正在做一个布局,我有一对按钮,我想要齐心协力。我可以想到几种方法来实现这一目标,但我想知道是否有一个好方法。

以下是一些模板代码:

package helloworld;

import javax.swing.BoxLayout;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.EventQueue;
/**
 * Created by matt on 22/07/16.
 */
public class FlushButtons {

    private void showGui(){
        JFrame frame = new JFrame();

        JPanel panel = new JPanel();
        JButton plus = new JButton("+");
        JButton minus = new JButton("-");

        panel.setLayout(new BoxLayout(panel, BoxLayout.LINE_AXIS));
        panel.add(plus);
        panel.add(minus);

        frame.setContentPane(panel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
    public static void main(String[] args){

        EventQueue.invokeLater(()->{
            new FlushButtons().showGui();
        });

    }
}

这会产生左侧的图像,但我希望它看起来更像右侧的图像。default layout desired layout

我尝试使用JButton#setMargin,但这没有任何效果。我使用JButton#setPreferredSize我可以获得所需的结果,但我需要事先了解按钮的大小。

2 个答案:

答案 0 :(得分:3)

这是我的解决方案,看起来很漂亮!

enter image description here

import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class FlushButtons {

    private JComponent ui = null;

    FlushButtons() {
        initUI();
    }

    private JButton getFlushButton(String text) {
        JButton b = new JButton();

        b.setBorderPainted(false);
        b.setContentAreaFilled(false);
        b.setMargin(new Insets(0,0,0,0));
        b.setBorder(null);
        b.setIcon(new ImageIcon(getImageOfText(text, Color.GREEN.darker())));
        b.setRolloverIcon(new ImageIcon(getImageOfText(text, Color.ORANGE)));
        b.setPressedIcon(new ImageIcon(getImageOfText(text, Color.RED)));

        return b;
    }

    private BufferedImage getImageOfText(String text, Color color) {
        int s = 24;
        BufferedImage bi = new BufferedImage(s, s, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = bi.createGraphics();

        g.setColor(color);
        g.fillRect(0, 0, s, s);
        g.setColor(Color.BLACK);
        g.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 16));
        // use better ways to position text..
        g.drawString(text, 8, 16);

        g.dispose();
        return bi;
    }

    public void initUI() {
        if (ui!=null) return;

        ui = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
        ui.setBorder(new EmptyBorder(4,4,4,4));

        ui.add(getFlushButton("+"));
        ui.add(getFlushButton("-"));
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception useDefault) {
                }
                FlushButtons o = new FlushButtons();

                JFrame f = new JFrame(o.getClass().getSimpleName());
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setLocationByPlatform(true);

                f.setContentPane(o.getUI());
                f.pack();
                f.setMinimumSize(f.getSize());

                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

答案 1 :(得分:0)

这是我的解决方案,看起来有点荒谬。

    JPanel panel = new JPanel();

    JButton plus = new JButton("+");
    JButton minus = new JButton("-");

    plus.setPreferredSize(new Dimension(29, 29));
    plus.setMaximumSize(new Dimension(33, 33));
    minus.setPreferredSize(new Dimension(29, 29));
    minus.setMaximumSize(new Dimension(33, 33));


    panel.setLayout(new BoxLayout(panel, BoxLayout.LINE_AXIS));

    panel.add(plus);
    panel.add(minus);
    panel.add(Box.createVerticalStrut(33));
    panel.add(Box.createHorizontalGlue());

如果没有垂直支柱,按钮会无边框地打包。

某种黑客,希望听到更好的东西。