如何将JButton移动到JPanel的右侧?

时间:2014-10-05 04:30:40

标签: java swing user-interface jpanel jbutton

对于编程课程,我被要求创建iCalendar应用程序的副本。我正在使用JAVA对其进行编码,并使用JFrame和JPanel来绘制它。这是我的问题的SSCCEE:

import java.awt.*;

import javax.swing.*;

public class Mainie extends JFrame {
    private JButton back = new TriangleButton(true),
            front = new TriangleButton(false);
    public Mainie(){
        super();
        setSize(800, 400);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        final JPanel months = new JPanel();
        final JPanel days = new JPanel();
        JLabel one = new JLabel("Hallo");
        one.setHorizontalAlignment(JLabel.CENTER);
        back.setHorizontalAlignment(SwingConstants.LEFT);
        front.setHorizontalAlignment(SwingConstants.RIGHT);
        months.setLayout(new BorderLayout());
        months.add(back, BorderLayout.WEST);
        months.add(one, BorderLayout.CENTER);
        months.add(front, BorderLayout.EAST);

        days.setLayout(new GridLayout(1,1));
        days.add(new JButton("Meister Camickr"));

        months.setAlignmentX(CENTER_ALIGNMENT);
        add(months, BorderLayout.NORTH);
        add(days, BorderLayout.CENTER);

        setVisible(true);
    }

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

class TriangleButton extends JButton {
    private Shape triangle = createTriangle();

    public void paintBorder(Graphics g) {
        ((Graphics2D) g).draw(triangle);
    }

    public void paintComponent(Graphics g) {
        ((Graphics2D) g).fill(triangle);
    }

    public Dimension getPreferredSize() {
        return new Dimension(200, 100);
    }

    public boolean contains(int x, int y) {
        return triangle.contains(x, y);
    }

    public TriangleButton(boolean pointLeft) {
        super();
        if (!pointLeft)
            this.triangle = createRTriangle();
    }

    private Shape createTriangle() {
        Polygon p = new Polygon();
        p.addPoint(0, 20);
        p.addPoint(20, 0);
        p.addPoint(20, 40);
        return p;
    }

    private Shape createRTriangle() {
        Polygon p = new Polygon();
        p.addPoint(20, 20);
        p.addPoint(0, 0);
        p.addPoint(0, 40);
        return p;
    }
}

如果你编译它,你可以看到我的JButton离左边太远了。我怎样才能将它移到右边?

2 个答案:

答案 0 :(得分:3)

这些按钮的基本问题是按钮非常宽,可视指示器位于左侧。在它们周围放置边框,这很明显。

OTOH我会使用一种完全不同的方法,使用按钮的文本和工厂方法使它们看起来像预期的那样。

enter image description here enter image description here

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

public class CustomPointerButtons {

    JPanel ui;

    CustomPointerButtons() {
        initUI();
    }

    private final void initUI() {
        ui = new JPanel(new BorderLayout(4, 4));

        JPanel topPanel = new JPanel(new BorderLayout(4, 4));

        ui.add(topPanel, BorderLayout.PAGE_START);
        ui.add(new JButton("Mister Mix")); //will default to CENTER

        topPanel.add(new JLabel("Blah, Blah.."));
        JButton back = getMinimalButton(new String(Character.toChars(9668)));
        topPanel.add(back, BorderLayout.LINE_START);

        JButton forward = getMinimalButton(new String(Character.toChars(9658)));
        topPanel.add(forward, BorderLayout.LINE_END);
    }

    public JButton getMinimalButton(String text) {
        JButton b = new JButton(text);
        b.setFont(b.getFont().deriveFont(40f));

        b.setMargin(new Insets(0,0,0,0));
        b.setContentAreaFilled(false);
        b.setBorder(null);

        return b;
    }

    public final JComponent getUI() {
        return ui;
    }


    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                JFrame f = new JFrame("Custom Pointer Buttons");
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setContentPane(new CustomPointerButtons().getUI());
                f.pack();
                f.setLocationByPlatform(true);
                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

答案 1 :(得分:0)

问题是您的随机首选尺寸不代表三角形的实际尺寸。

您可以执行以下操作:

public Dimension getPreferredSize() {
//return new Dimension(200, 100);
Rectangle bounds = triangle.getBounds();
return new Dimension(bounds.width, bounds.height);
}

你还有其他问题,因为你仍然会有绘画文物。覆盖方法时,应始终调用super.paintComponent():

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    ((Graphics2D) g).fill(triangle);
}

但是,这仍然不起作用,因为按钮也会进行其他绘画。所以真正的问题是你不应该尝试自定义绘制按钮。你真正想做的是自定义绘画Icon。然后,您只需将图标添加到按钮即可。

您可能需要查看Playing With Shapes。您可以使用ShapeIcon类来创建图标。这将提供更灵活的解决方案,因为我猜所有形状都不是由ascii字符表示的。