gpt4 book ai didi

java - 如何将 JButton 移到 JPanel 的右侧?

转载 作者:行者123 更新时间:2023-11-30 08:20:15 26 4
gpt4 key购买 nike

在编程类(class)中,我被要求创建一个 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 太靠左了。我怎样才能把它移到右边?

最佳答案

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

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

关于java - 如何将 JButton 移到 JPanel 的右侧?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26199634/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com