gpt4 book ai didi

java - GridBagLayout 不接受我的 rowspan 命令(gridtheight)

转载 作者:搜寻专家 更新时间:2023-11-01 02:44:57 24 4
gpt4 key购买 nike

我即将学习 Java。我正在尝试为带有 GridBagLayout 的简单计算器编写 GUI。

问题是我的“+”按钮没有超过 2 行高。我不知道为什么?我试着在最后一行做一次:

    c.gridwidth = 1;
c.gridy = 4;
c.gridx = 3;
c.gridheight = 2;
c.ipady = 26;
subpanel.add(bplus, c);

还有一次在 2. 最后一行。

    c.gridwidth = 1;
c.gridy = 3;
c.gridx = 3;
c.gridheight = 2;
c.ipady = 26;
subpanel.add(bplus, c);

这里是gui的打洞代码,如果要测试的话,可能要调main了。

package gui;

import classes.Rechner;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

/**
* Created by tq67 on 17.07.2014.
*/
public class CalculatorGUI extends JFrame {

public static void main(String[] args) {

CalculatorGUI gui = new CalculatorGUI();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setVisible(true);


}

public CalculatorGUI() {
setTitle("Calculator");
setBounds(300, 300, 220, 225);

JPanel subpanel = new JPanel();
JPanel headpanel = new JPanel();
final JTextField input = new JTextField();

input.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {

}

@Override
public void keyPressed(KeyEvent e) {

}

@Override
public void keyReleased(KeyEvent e) {
if (10 == e.getKeyCode()) {
Rechner rechner = new Rechner(input.getText());
input.setText(rechner.rechnen().toPlainString());
}
if (27 == e.getKeyCode()) {
input.setText("");
}
}
});
ActionListener buttonListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
input.setText(input.getText() + e.getActionCommand());
}
};
ActionListener resultListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("=")) {
Rechner rechner = new Rechner(input.getText());
input.setText(rechner.rechnen().toPlainString());
} else {
input.setText("");
}

}

private void calc() {
Rechner rechner = new Rechner(input.getText());
input.setText(rechner.rechnen().toPlainString());
}
};

JButton b1 = new JButton("1");
JButton b2 = new JButton("2");
JButton b3 = new JButton("3");
JButton b4 = new JButton("4");
JButton b5 = new JButton("5");
JButton b6 = new JButton("6");
JButton b7 = new JButton("7");
JButton b8 = new JButton("8");
JButton b9 = new JButton("9");
JButton b0 = new JButton("0");

JButton bdot = new JButton(".");
JButton bplus = new JButton("+");
JButton bminus = new JButton("-");
JButton bdiv = new JButton("/");
JButton bmult = new JButton("*");
JButton bresult = new JButton("=");

JButton bcancel = new JButton("C");
b1.addActionListener(buttonListener);
b2.addActionListener(buttonListener);
b3.addActionListener(buttonListener);
b4.addActionListener(buttonListener);
b5.addActionListener(buttonListener);
b6.addActionListener(buttonListener);
b7.addActionListener(buttonListener);
b8.addActionListener(buttonListener);
b9.addActionListener(buttonListener);
b0.addActionListener(buttonListener);
bdot.addActionListener(buttonListener);
bplus.addActionListener(buttonListener);
bminus.addActionListener(buttonListener);
bdiv.addActionListener(buttonListener);
bmult.addActionListener(buttonListener);

bresult.addActionListener(resultListener);
bcancel.addActionListener(resultListener);

headpanel.setLayout(new GridBagLayout());
subpanel.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();




c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 1;
c.weighty = 3;
c.gridx = 0;
c.gridy = 0;
c.ipady = 10;
headpanel.add(input, c);

setLayout(new BorderLayout());
add(headpanel, BorderLayout.NORTH);
add(subpanel, BorderLayout.SOUTH);
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 0;
c.gridx = 0;
c.gridy = 0;
subpanel.add(b7, c);
c.gridx++;
subpanel.add(b8, c);
c.gridx++;
subpanel.add(b9, c);
c.gridx++;
subpanel.add(bmult, c);

c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 1;
subpanel.add(b4, c);
c.gridx++;
subpanel.add(b5, c);
c.gridx++;
subpanel.add(b6, c);
c.gridx++;
subpanel.add(bdiv, c);


c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 2;
subpanel.add(b1, c);
c.gridx++;
subpanel.add(b2, c);
c.gridx++;
subpanel.add(b3, c);
c.gridx++;
subpanel.add(bminus, c);


c.fill = GridBagConstraints.HORIZONTAL;
c.gridheight = 1;
c.ipady = 0;
c.gridx = 0;
c.gridy = 3;
subpanel.add(bdot, c);
c.gridwidth = 2;
c.gridx++;
subpanel.add(b0, c);
c.gridwidth = 1;
c.gridx = 3;
c.gridheight = 2;
c.ipady = 26;
subpanel.add(bplus, c);


c.fill = GridBagConstraints.HORIZONTAL;
c.gridwidth = 1;
c.ipady = 0;
c.gridx = 0;
c.gridy = 4;
subpanel.add(bcancel, c);
c.gridx = 1;
c.gridwidth = 2;
subpanel.add(bresult, c);

}
}

我在 Stackoverflow 中读到一个错误,当您使用 gridx 大于 0 时,您将遇到 gridheight 问题...

目前 GUI 看起来像这样:

calc.png

感谢您的帮助。

最佳答案

有两件事,首先,在使用后重置gridheight

c.gridwidth = 1;
c.gridx = 3;
c.gridheight = 2;
c.ipady = 26;
subpanel.add(bplus, c);

c.fill = GridBagConstraints.HORIZONTAL;
c.gridwidth = 1;
c.ipady = 0;
c.gridx = 0;
c.gridy = 4;
// Still using the gridheight of 2...
subpanel.add(bcancel, c);

应该更像是……

c.fill = GridBagConstraints.HORIZONTAL;
c.gridwidth = 1;
c.ipady = 0;
c.gridx = 0;
c.gridy = 4;
c.gridheight = 1;
subpanel.add(bcancel, c);

我也会去掉 c.ipady = 26;,但那是我 ;)

其次,利用BOTH 填充约束...

c.fill = GridBagConstraints.BOTH;
c.gridwidth = 1;
c.gridx = 3;
c.gridheight = 2;
c.ipady = 26;
subpanel.add(bplus, c);

Calculator

还有...

public void keyReleased(KeyEvent e) {
if (10 == e.getKeyCode()) {
Rechner rechner = new Rechner(input.getText());
input.setText(rechner.rechnen().toPlainString());
}
if (27 == e.getKeyCode()) {
input.setText("");
}
}

keyCode 不代表 ASCII 字符,而是代表虚拟键码。您应该使用 KeyEvent.VK_ENTER(我认为)和 KeyEvent.VK_ESCAPE。请注意,JTextField 已经有能力处理 ActionListener 接口(interface)的 Enter。无论如何,您应该考虑使用 Key Bindings可能的话API

关于java - GridBagLayout 不接受我的 rowspan 命令(gridtheight),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24859212/

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