gpt4 book ai didi

java - 如何消除 JButton 行边缘和父 JPanel 边缘之间的间隙?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:11:20 27 4
gpt4 key购买 nike

我正在用 Swing 制作一个计算器作为练习,我不明白为什么 JButton 行的边缘和父 JPanel (buttonPanel) 的边缘之间有这个间隙。我查看了我的代码并更改了一些东西,但我仍然无法弄清楚为什么会有差距。如果我删除 MatteBorder,将边框保留为默认值,间隙就会消失,但按钮之间会有双边框。如果我将 JTextField 的列大小减少到 7,那么间隙就会消失。如何在不求助于双边框或缩小我的应用程序的情况下修复差距?

calculator

(我将面板的背景设置为红色,以帮助确定是哪个组件导致了间隙。)

public class CalculatorView extends JFrame {
// NOTE: Not recommended to extend JFrame, recommended to use composition over inheritance
private JTextField display = new JTextField("0", 17);

// Number buttons
private JButton zeroButton = new JButton("0");
private JButton oneButton = new JButton("1");
private JButton twoButton = new JButton("2");
private JButton threeButton = new JButton("3");
private JButton fourButton = new JButton("4");
private JButton fiveButton = new JButton("5");
private JButton sixButton = new JButton("6");
private JButton sevenButton = new JButton("7");
private JButton eightButton = new JButton("8");
private JButton nineButton = new JButton("9");
private JButton periodButton = new JButton(".");

// Operation buttons
private JButton additionButton = new JButton("+");
private JButton subtractionButton = new JButton("-");
private JButton divisionButton = new JButton("÷");
private JButton multiplicationButton = new JButton("x");
private JButton signChangeButton = new JButton("+/-");
private JButton percentButton = new JButton("%");
private JButton clearButton = new JButton("AC");
private JButton equalButton = new JButton("=");

CalculatorView() {
// Set the look and feel to the cross-platform look and feel,
// otherwise mac os will have quirks like gaps between jbuttons
try {
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
} catch (Exception e) {
System.err.println("Unsupported look and feel.");
e.printStackTrace();
}
// Let the OS set location, prevent user from resizing window, and exit app on close
this.setLocationByPlatform(true);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setUndecorated(true); // must be undecorated for setbackground to work

// Create the main panel, which by default covers the entire frame
// NOTE: Good practice. Never put components directly onto a JFrame.
JPanel gui = new JPanel();
// Set the main panel's layout manager to BorderLayout
gui.setLayout(new BorderLayout());

// Create the button panel
JPanel buttonPanel = new JPanel();
// Set button panel's layout manager to GridBagLayout
buttonPanel.setLayout(new GridBagLayout());
// Create a GridBagConstraints object to control the layout of components
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(0, 0, 0, 0);
c.fill = GridBagConstraints.HORIZONTAL;

// Position buttons on the grid
c.gridx = 0;
c.gridy = 0;
c.ipady = 30; // adjust vertical height of buttons
c.weightx = 1; // needed or buttons will cluster at center w/ gap on sides
buttonPanel.add(clearButton, c);
clearButton.setBackground(Color.gray);
clearButton.setOpaque(true);
clearButton.setBorder(new MatteBorder(0, 1, 0, 1, Color.BLACK));
c.gridx = 1;
buttonPanel.add(signChangeButton, c);
signChangeButton.setBackground(Color.gray);
signChangeButton.setOpaque(true);
signChangeButton.setBorder(new MatteBorder(0, 0, 0, 1, Color.BLACK));
c.gridx = 2;
buttonPanel.add(percentButton, c);
percentButton.setBackground(Color.gray);
percentButton.setOpaque(true);
percentButton.setBorder(new MatteBorder(0, 0, 0, 1, Color.BLACK));
c.gridx = 3;
buttonPanel.add(divisionButton, c);
divisionButton.setBackground(Color.ORANGE);
divisionButton.setOpaque(true);
divisionButton.setBorder(new MatteBorder(0, 0, 0, 1, Color.BLACK));
c.gridx = 0;
c.gridy = 1;
buttonPanel.add(sevenButton, c);
sevenButton.setBorder(new MatteBorder(1, 1, 0, 1, Color.BLACK));
c.gridx = 1;
buttonPanel.add(eightButton, c);
eightButton.setBorder(new MatteBorder(1, 0, 0, 1, Color.BLACK));
c.gridx = 2;
buttonPanel.add(nineButton, c);
nineButton.setBorder(new MatteBorder(1, 0, 0, 1, Color.BLACK));
c.gridx = 3;
buttonPanel.add(multiplicationButton, c);
multiplicationButton.setBackground(Color.ORANGE);
multiplicationButton.setOpaque(true);
multiplicationButton.setBorder(new MatteBorder(1, 0, 0, 1, Color.BLACK));
c.gridx = 0;
c.gridy = 2;
buttonPanel.add(fourButton, c);
fourButton.setBorder(new MatteBorder(1, 1, 0, 1, Color.BLACK));
c.gridx = 1;
buttonPanel.add(fiveButton, c);
fiveButton.setBorder(new MatteBorder(1, 0, 0, 1, Color.BLACK));
c.gridx = 2;
buttonPanel.add(sixButton, c);
sixButton.setBorder(new MatteBorder(1, 0, 0, 1, Color.BLACK));
c.gridx = 3;
buttonPanel.add(subtractionButton, c);
subtractionButton.setBackground(Color.ORANGE);
subtractionButton.setOpaque(true);
subtractionButton.setBorder(new MatteBorder(1, 0, 0, 1, Color.BLACK));
c.gridx = 0;
c.gridy = 3;
buttonPanel.add(oneButton, c);
oneButton.setBorder(new MatteBorder(1, 1, 0, 1, Color.BLACK));
c.gridx = 1;
buttonPanel.add(twoButton, c);
twoButton.setBorder(new MatteBorder(1, 0, 0, 1, Color.BLACK));
c.gridx = 2;
buttonPanel.add(threeButton, c);
threeButton.setBorder(new MatteBorder(1, 0, 0, 1, Color.BLACK));
c.gridx = 3;
buttonPanel.add(additionButton, c);
additionButton.setBackground(Color.ORANGE);
additionButton.setOpaque(true);
additionButton.setBorder(new MatteBorder(1, 0, 0, 1, Color.BLACK));
c.gridx = 0;
c.gridy = 4;
c.gridwidth = 2; // spans 2 cells
buttonPanel.add(zeroButton, c);
zeroButton.setBorder(new MatteBorder(1, 1, 1, 1, Color.BLACK));
c.gridx = 2;
c.gridwidth = 1;
buttonPanel.add(periodButton, c);
periodButton.setBorder(new MatteBorder(1, 0, 1, 1, Color.BLACK));
c.gridx = 3;
buttonPanel.add(equalButton, c);
equalButton.setBackground(Color.ORANGE);
equalButton.setOpaque(true);
equalButton.setBorder(new MatteBorder(1, 0, 1, 1, Color.BLACK));

// Customize display field
display.setHorizontalAlignment(JTextField.RIGHT);
display.setFont(new Font("Arial", Font.PLAIN, 40));
display.setBorder(new EmptyBorder(30, 0, 0, 0));
display.setForeground(Color.WHITE);
// Make display translucent but leave button panel opaque
this.setBackground(new Color(0, 0, 0, 100));
gui.setBackground(new Color(0,0,0,100));
display.setBackground(new Color(0, 0, 0, 100));

// I need to fix the gap between the edge of the buttonPanel and the buttons...
buttonPanel.setBackground(Color.red);

// Add display and button panel to main panel, then main panel to frame
gui.add(display, BorderLayout.NORTH);
gui.add(buttonPanel, BorderLayout.CENTER);
this.add(gui);
this.pack();
}
}

最佳答案

我不知道为什么会有这个空隙,但是如果你设置下面的空边框,背景就没有了:

JPanel buttonPanel = new JPanel();
// Set button panel's layout manager to GridBagLayout
buttonPanel.setLayout(new GridBagLayout());
buttonPanel.setBorder(new EmptyBorder(0, -1, 0, -1));

关于java - 如何消除 JButton 行边缘和父 JPanel 边缘之间的间隙?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44667914/

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