【问题标题】:How can I move my JButton to the right side of my JPanel?如何将 JButton 移动到 JPanel 的右侧?
【发布时间】:2014-10-05 04:30:39
【问题描述】:

对于编程课,我被要求创建 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 离左边太远了。我怎样才能把它移到右边?

【问题讨论】:

    标签: java swing user-interface jpanel jbutton


    【解决方案1】:

    这些按钮的基本问题是按钮非常宽且视觉指示器在左侧。在它们周围加上一个边框,它就会变得很明显。

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

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

    【讨论】:

      【解决方案2】:

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

      你可以这样做:

      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 字符表示的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-05
        • 1970-01-01
        • 2014-09-26
        • 2013-11-05
        • 2020-12-21
        • 2018-04-19
        • 1970-01-01
        • 2021-05-06
        相关资源
        最近更新 更多