- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我即将学习 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 看起来像这样:
感谢您的帮助。
最佳答案
有两件事,首先,在使用后重置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);
还有...
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/
我正在本地编程应用程序,但是当我迁移到服务器时,使用此行出现解析错误: if(!is_array($data[array_keys($data)[0]])) 返回值: Parse error: syn
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 9 年前。 Improv
我已经开始尝试用 C++ 学习 Winsock,但我遇到了一些问题。我首先遵循 MSDN 上的 Winsock 示例(一个基本的客户端-服务器)。 http://msdn.microsoft.com/
我有一段使用 epoll 的代码,但它有问题。当我运行它时,它给出输出:服务器套接字()没问题......服务器绑定(bind)()没问题......3个4个接受:无效参数 我在 ubuntu lin
我正在寻找一种方法来接受 $_GET 变量作为文件路径,例如 - /page/test将转换为 page.php?page=test .我已经搜索过,但找不到任何可以帮助我的东西,因此我在这里问。 我
我想要一个只接受从 0 到 9 和减号的浮点数的正则表达式。 请帮忙。 最佳答案 ^[-+]?[0-9]*\.?[0-9]+$ ^ - 字符串开头 [-+]? - 0 或 1 符号指示符 [0-9]*
请问如何接受\r\n无需将其更改为 \\r\\n , 与 fgets . 我想让程序翻译 \r\n到换行符而不是将其打印为字符串。 当前代码: char buff[1024]; printf("Msg
我正在编写一个 Cocoa 应用程序,该应用程序需要在其 Dock 图标上接受已安装卷的滴落。它不是基于文档的;我打算将每个卷分派(dispatch)到 application:openFiles 中
我在 SQLite 中发现了这种意外行为。 SQLite 似乎接受 SQL 连接语法中的任意关键字。如果我不小心键入了 natural join 而不是 natural join,则会生成笛卡尔积。这
我在 Windows 窗体的同一个窗体上有一个 TreeView 和一个多行文本框。我有拖放设置,以便我可以将节点从 TreeView 拖到文本框并将文本插入文本框(这是有效的)。 我想增强这一点,以
我正在创建一棵类似于 D3 Layout Tree 的树并尝试绑定(bind)我的自定义 JSON 对象。切换树节点的代码如下。 function toggleAll(d) { if (d.c
所以,我希望能够向我的 DOB 字段发送空选项。 这是我的表单生成器: ->add('birthDate', DateType::class, array( 'widg
错误可以在这里看到:http://djaffry.selfip.com:8080/ 我希望索引页接受参数,无论是 mysite.com/search/param_here 或 mysite.com/?
我想知道标准 Scala 解析器组合器是否包含一个解析器,该解析器接受 Scala 语言本身也接受的相同标识符(如 Scala 语言规范第 1.1 节中所指定)。 StdTokenParsers 特征
我暂时使用以下行在我的 Android 手机上创建 ServerSocket: socketl = new ServerSocket(port, 0, InetAddress.getByName("1
今天早上刚刚发布了一个应用程序,我在所有可能的设备和模拟器上测试了它,但是当我从商店下载它时,应用程序在启动时崩溃。我在很多设备和iOS版本上测试过,但结果都是一样的: Incident Identi
我想要 5 个名字中最长的一个。我想我应该使用 compareTo() 方法或 length()? 输出必须是这样的: enter 5 names : Joey Mark Catherine Zach
抱歉,我不熟悉泛型,我可以创建一个如下所示的泛型类: public class InfoField { } 上面的类可以接受如下值: , User> 但是我可以有接受 map 对象作为值的类吗?
我想让一个单元格等于它上面的单元格。 当我写作时 =address(row()-1;column()) 它只是写入上面单元格的名称(例如,对于 B2,它写入 $B$1) 是否有一个函数可以输入地址并放
我正在使用Asm访问java字节码来获取方法信息。看来ClassReader类的accept方法是异步调用的,所以不可能立即获取方法信息。我该怎么办? 最佳答案 实际上,accept 方法是从您自己的
我是一名优秀的程序员,十分优秀!