- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
这可能是一个愚蠢的问题,可以通过更改我为此使用的布局来解决,但是,在运行我的计算器程序时,我很聪明,直到我发现我的文本区域大小不会拉伸(stretch)以适应它所在的 Pane 的大小,而无需我在第一次创建它时设置大小,而且,我该如何消除 BorderLayout.CENTER
在我的按钮面板之间占据的巨大差距?
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class calculatorProg extends JFrame implements ActionListener
{
private Container contents;
private JButton add, subtr, mult, div, pow, pow10, b1, b2, b3, b4, b5, b6, b7, b8, b9, b0;
private JTextArea win;
private JPanel numArea, win1, arithArea, numPlusArith;
private JMenu men;
private JMenuItem menItem;
JSplitPane calcPane;
public calculatorProg()
{
super("Calculator");
contents = getContentPane();
setDefaultCloseOperation(EXIT_ON_CLOSE);
contents.setLayout(new BorderLayout());
//Arithmetic buttons
add = new JButton("+");
subtr = new JButton("-");
mult = new JButton("x");
div = new JButton("/");
pow = new JButton("x^");
pow10 = new JButton("x^10");
//Buttons
b1 = new JButton("1");
b5 = new JButton("5");
b2 = new JButton("2");
b6 = new JButton("6");
b3 = new JButton("3");
b7 = new JButton("7");
b4 = new JButton("4");
b8 = new JButton("8");
b9 = new JButton("9");
b0 = new JButton("0");
//Creating the panels
win = new JTextArea(2, 25);
win.setEditable(false);
win.setVisible(true);
win1 = new JPanel();
win1.setLayout(new FlowLayout(FlowLayout.LEFT));
win1.add(win);
numArea = new JPanel(new GridLayout(0, 3));
arithArea = new JPanel(new GridLayout(3, 3));
arithArea.setSize(new Dimension(8, 8));
numPlusArith = new JPanel(new BorderLayout());
//Adding to num area
numArea.add(b1);
numArea.add(b2);
numArea.add(b3);
numArea.add(b4);
numArea.add(b5);
numArea.add(b6);
numArea.add(b7);
numArea.add(b8);
numArea.add(b9);
numArea.add(b0);
arithArea.add(add);
arithArea.add(subtr);
arithArea.add(div);
arithArea.add(mult);
arithArea.add(pow);
arithArea.add(pow10);
numPlusArith.add(arithArea, BorderLayout.LINE_START);
numPlusArith.add(numArea, BorderLayout.LINE_END);
//Lay out the panes
calcPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, win1, numPlusArith);
calcPane.setOneTouchExpandable(true);
calcPane.setDividerLocation(50);
Dimension minimumSize = new Dimension(100, 50);
win1.setMinimumSize(minimumSize);
numArea.setMinimumSize(minimumSize);
//add to contents
contents.add(calcPane, BorderLayout.CENTER);
setSize(320, 370);
setVisible(true);
}
public static void main(String[] args)
{
calculatorProg cp = new calculatorProg();
}
}
有人可能会提到 0 在 numArea
中偏离中心(我在 Java 文档中阅读了关于 GridBagLayout
的内容,但我什至连一半的脚趾甲都没有插入其中水域,但是。)
编辑代码:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
class calculatorProg extends JFrame implements ActionListener {
private Container contents;
private JButton add, subtr, mult, div, pow, pow10, clr, plmi, b1, b2, b3, b4, b5, b6, b7, b8, b9, b0;
private JTextArea win;
private JPanel numArea, win1, arithArea, numPlusArith;
private JMenu men;
private JMenuItem menItem;
JSplitPane calcPane;
public calculatorProg(){
super("Calculator");
contents = getContentPane();
setDefaultCloseOperation(EXIT_ON_CLOSE);
contents.setLayout(new BorderLayout());
//Arithmetic buttons
add = new JButton("+");
subtr = new JButton("-");
mult = new JButton("x");
div = new JButton("/");
pow = new JButton("x^");
pow10 = new JButton("x^10");
//Buttons
b1 = new JButton("1"); b5 = new JButton("5");
b2 = new JButton("2"); b6 = new JButton("6");
b3 = new JButton("3"); b7 = new JButton("7");
b4 = new JButton("4"); b8 = new JButton("8");
b9 = new JButton("9"); b0 = new JButton("0");
clr = new JButton("C"); plmi = new JButton("+/-");
//Creating the panels
win = new JTextArea(2, 25);
win.setLineWrap(true);
win.setWrapStyleWord(true);
win.setEditable(false);
win.setVisible(true);
win1 = new JPanel();
win1.setLayout(new BorderLayout());
win1.add(new JScrollPane(win), BorderLayout.CENTER);
numArea = new JPanel(new GridLayout(0,3));
arithArea = new JPanel(new GridLayout(3, 3));
//arithArea.setSize(new Dimension(8, 8));
numPlusArith = new JPanel(new GridLayout(0, 2, 0, 0));
//Adding to num area
numArea.add(b1); numArea.add(b2);
numArea.add(b3); numArea.add(b4);
numArea.add(b5); numArea.add(b6);
numArea.add(b7); numArea.add(b8);
numArea.add(b9); numArea.add(b0);
numArea.add(clr); numArea.add(plmi);
arithArea.add(add);
arithArea.add(subtr);
arithArea.add(div);
arithArea.add(mult);
arithArea.add(pow);
arithArea.add(pow10);
numPlusArith.add(arithArea);
numPlusArith.add(numArea);
//Lay out the panes
calcPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, win1, numPlusArith);
calcPane.setOneTouchExpandable(true);
calcPane.setDividerLocation(50);
Dimension minimumSize = new Dimension(100, 50);
win1.setMinimumSize(minimumSize);
numArea.setMinimumSize(minimumSize);
//add to contents
contents.add(calcPane, BorderLayout.CENTER);
pack();
setVisible(true);
}
public static void main(String[] args) {
calculatorProg cp = new calculatorProg();
}
private double addition(double a, double b){
a=0.0; b=0.0;
double finAnswer = a + b;
return finAnswer;
}
private double subtraction(double a, double b){
a=0.0; b=0.0;
double finAnswer = a + b;
return finAnswer;
}
private double division(double a, double b){
a=0.0; b=0.0;
double finAnswer = a / b;
return finAnswer;
}
private double multiplication(double a, double b){
a=0.0; b=0.0;
double finAnswer = a * b;
return finAnswer;
}
private double pow(double a, double b){
a=0.0; b=0.0;
double finAnswer = Math.pow(a, b);
return finAnswer;
}
private double pow10(double a){
a = 0.0;
double finAnswer = Math.pow(a, 10);
return finAnswer;
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
}
如您所见,我进行了必要的更改。看起来干净多了。数字区域周围仍然存在微小差距,但它更干净了。 pack()
肯定会有所作为。感谢大家的帮助!
最佳答案
感谢 SSCCE,为此 +1。所缺少的只是 actionPerformed
方法(在最近的编辑之前,您还缺少最后一个 }
)。
无论如何,您的问题是您正在为包含您的JTextArea
的面板使用FlowLayout
。更改这些行:
win1.setLayout(new FlowLayout(FlowLayout.LEFT));
win1.add(win);
为此:
win1.setLayout(new BorderLayout());
win1.add(new JScrollPane(win), BorderLayout.CENTER);
您可能还注意到我将文本区域包裹在滚动 Pane 中。这将使任何文本溢出都可以滚动。您还应该添加以下行以打开换行:
win = new JTextArea(2, 25);
win.setLineWrap(true);
win.setWrapStyleWord(true);
关于java - 如何消除 BorderLayout.CENTER 占用的空隙?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18810100/
我编写了一些运行速度很慢的 VBA 代码。我的代码中有一系列不同的循环。我知道循环并不总是处理数据的最有效方式,所以我认为它们是问题所在。我需要有关如何更改或消除循环的想法,以便加快代码的运行时间。
我目前有一个网址:http://testsite.local/search/?q=findme一旦有人查询,我的搜索页面。我使用 mod_rewrite 重写了一些页面,想知道是否可以将其变成一个不错
有人可以帮助我执行一个查询,其中查询的重复元素被删除 Select * from table where APPNAME = 'Ap1' or APPNAME= 'Ap2' 使用 DISTINCT 的
我正在尝试在 ubuntu 上使用以下命令在一个文件夹中查找文件并通过 FFmpeg 提供并输出到另一个文件夹。问题是当它处理输出路径和文件名时,它添加了一个 .像这样的路径:/conversions
这个问题在这里已经有了答案: How can I remove all duplicates so that NONE are left in a data frame? (3 个答案) 关闭 1
我想证明以下定理: Theorem Frobenius (A: Set) (q: Prop) (p: A -> Prop) : (q \/ forall x : A, p x) -> (foral
我有一个 PHP 脚本,它只需要一些数据,将其分隔为制表符分隔格式,将其保存为 .xls 文件,然后为用户提供下载链接。 大多数情况下运行良好,但有些人正在获取导出的 .xls 文件的缓存版本。 我想
我有一个看起来有点像这个可重现代码的数据框,我想删除每列的异常值(在我们的例子中,数据点低于或高于平均值 2.5 个标准偏差)而不删除整个主题/行。 Subj mn + sd * 2.5) | (x
我正在尝试编写一个实现 fmap 的演示。在 Haskell 中与 continuation ,我的代码如下所示: #include #include template using Callba
在此 HighCharts例如,如何消除 xaxis 开始位置与 Jan 的刻度位置之间的差距。 http://jsfiddle.net/gh/get/jquery/1.7.2/highslide-s
重现步骤: 将TPanel添加到新的VCL表单并设置Align = alClient。 将 TSpeedButton 添加到面板,并将一些 bmp 图像分配给 Glyph 属性。 (可选,但更清晰:F
我得到一个 JavaScript 数组,其中包含一定数量(未指定)的各种元素(字符串、数字、 bool 值)。我需要消除字符串和 bool 值。我应该如何处理它?我应该使用typeof吗? 最佳答案
我正在寻找一个公式,可以消除字符串中空格后的空格和无用字符。 我的第一 react 是执行以下操作:=LEFT(A1,FIND("",A1)) 它适用于所有有空格的情况 但是如果单元格中没有空格,我的
我有以下问题:我正在尝试编写一个 Javascript 游戏,并且 Angular 色由箭头键控制。 问题是,当一个人按住按键时,在触发第一个按键和重复的按键之间存在短暂的延迟。 另外,当按下“向右箭
让我们考虑一个集合的集合,以及需要在管道内对内部集合的每个元素执行的操作。 为了简单起见,让它成为一个数组数组,操作简单的打印到屏幕上。为了表达我的问题,让我们还有一个元素不是集合的数组: $Arra
跟进this question关于包含源文件。我包括一个 Chapel 模块,其中包含一个名为 classes.chpl 的文件。 ,但我当前的项目也有一个 classes.chpl 。正确的消歧模式
我想知道如何在英特尔语法中的某些指令中区分标签名称和寄存器名称。例如,call rdx通常意味着间接跳转,但是如果我们在同一个汇编文件中有一个标签rdx怎么办?我相信它可以被解释为直接跳转到 rdx
据我了解,Chrome 会异步运行整个程序,这会导致我的扩展程序在单击后大约 2 秒后打开。有没有办法强制扩展程序显示带有“正在加载”消息的 html 页面,然后完成加载 javascript 并用内
我正在将 CSV 加载到 sqlite 数据库,如下所示: sqlite3 /path/to/output.db /dev/null 或者,您可以自己生成 SQL 命令,以便可以使用 INSERT 或
我的 .cabal 文件的许多节中经常有类似的属性。例如 Library x ... ghc-options: -O2 -Wall -fno-warn-missing-s
我是一名优秀的程序员,十分优秀!