gpt4 book ai didi

java - JPanel 的对齐

转载 作者:行者123 更新时间:2023-12-01 06:22:24 25 4
gpt4 key购买 nike

我对 Java 还很陌生,我完全不知所措。我正在尝试创建一个简单的程序来绘制一个圆圈并在屏幕上输出它的属性。从功能上讲,该程序运行良好,但 JPanel 的布局已关闭。由于某种原因,持有“Draw Me”按钮的 JPanel 和持有圆圈的 JPanel 之间存在间隙。即使我调整程序的高度,间隙也不会消失。同样,程序中的标签没有向左对齐,它们出于某种奇怪的原因偏离中心,删除边框似乎没有帮助。我被困住了...

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;

public class MainPanel extends JPanel {
private JPanel userHelpSubPanel;
private JPanel inputSubPanel;
private JPanel buttonSubPanel;
private JPanel drawSubPanel;
private JPanel resultsSubPanel;
private JLabel userHelpLabel;
private JLabel diameterLabel;
private JLabel circumferenceLabel;
private JLabel areaLabel;
private JButton drawButton;
private JTextField inputTextField;
private float radius;

public MainPanel() {
radius = 0;

setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

userHelpSubPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));

inputSubPanel = new JPanel();
inputSubPanel.setLayout(new BoxLayout(inputSubPanel, BoxLayout.Y_AXIS));

drawSubPanel = new JPanel() {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);

Graphics2D g2d = (Graphics2D) g;

g2d.draw(new Ellipse2D.Double(150, 20, radius * 2, radius * 2));
}
};

resultsSubPanel = new JPanel();
resultsSubPanel.setLayout(new BoxLayout(resultsSubPanel, BoxLayout.Y_AXIS));

buttonSubPanel = new JPanel();

userHelpLabel = new JLabel();
userHelpLabel.setText("Enter the radius of the ellipse: ");

inputTextField = new JTextField();
inputTextField.setMaximumSize(new Dimension(Integer.MAX_VALUE, inputTextField.getPreferredSize().height));

drawButton = new JButton("Draw Me");
drawButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
MainPanel.this.radius = Float.parseFloat(inputTextField.getText());

diameterLabel.setText(String.format("Diameter: %.1f", 2 * radius));
circumferenceLabel.setText(String.format("Circumference: %.1f", 2 * Math.PI * radius));
areaLabel.setText(String.format("Area: %.1f", Math.PI * Math.pow(radius, 2)));

drawSubPanel.repaint();
}
});

circumferenceLabel = new JLabel("Circumference: N/A");
diameterLabel = new JLabel("Diameter: N/A");
areaLabel = new JLabel("Area: N/A");

userHelpSubPanel.add(userHelpLabel);
inputSubPanel.add(inputTextField);
buttonSubPanel.add(drawButton);
resultsSubPanel.add(diameterLabel);
resultsSubPanel.add(circumferenceLabel);
resultsSubPanel.add(areaLabel);

add(userHelpLabel);
add(inputSubPanel);
add(buttonSubPanel);
add(drawSubPanel);
add(resultsSubPanel);
}
}

这就是它的样子......

/image/PEHVE.png

最佳答案

BoxLayout 将遵循组件的最小和最大尺寸。因此,如果框架中有额外的空间,那么面板将获得更多的空间。

解决方案是重写自定义面板的 getMaximumSize() 方法以返回面板的首选尺寸。

每个 Swing 组件都有责任确定自己的首选大小。因此,这意味着您还需要重写进行自定义绘画的面板的 getPreferredSize() 。首选尺寸将基于您正在绘制的椭圆形的尺寸/位置。

关于java - JPanel 的对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45002218/

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