整理计算器界面
Areanging calculator interface
在这段代码中,前三个按钮出现在文本区域,如何让这些按钮从 header 下面开始?在添加按钮之前,按钮区域从 header 下方开始,但一旦我添加按钮,它就会出现在 header
上
public Design ()
{
header = new JPanel ();
header.setBounds(0, 0, 300, 100);
header.setBackground(Color.WHITE);
header.setLayout (null);
buttonarea = new JPanel ();
buttonarea.setBounds (0,0, 300, 300);
buttonarea.setBackground(Color.BLACK);
buttonarea.setLayout(new GridLayout (6,3));
add (header);
add (buttonarea);
zero = new JButton ("0");
zero.setBackground(Color.PINK);
one = new JButton ("1");
one.setBackground(Color.PINK);
two = new JButton ("2");
two.setBackground(Color.PINK);
three = new JButton ("3");
three.setBackground(Color.PINK);
four = new JButton ("4");
four.setBackground(Color.PINK);
five = new JButton ("5");
five.setBackground(Color.PINK);
six = new JButton ("6");
six.setBackground(Color.PINK);
seven = new JButton ("7");
seven.setBackground(Color.PINK);
eight = new JButton ("8");
eight.setBackground(Color.PINK);
nine = new JButton ("9");
nine.setBackground(Color.PINK);
add = new JButton ("+");
add.setBackground(Color.PINK);
subtract = new JButton ("-");
subtract.setBackground(Color.PINK);
divide = new JButton ("*");
divide.setBackground(Color.PINK);
multiply = new JButton ("/");
multiply.setBackground(Color.PINK);
square = new JButton ("x^2");
square.setBackground(Color.PINK);
equal = new JButton ("=");
equal.setBackground(Color.PINK);
c = new JButton ("C");
c.setBackground(Color.PINK);
clear = new JButton ("delete");
clear.setBackground(Color.PINK);
dot = new JButton (".");
dot.setBackground(Color.PINK);
written = new JTextArea ();
written.setBackground(Color.WHITE);
header.add (written);
buttonarea.add (c);
buttonarea.add (clear);
buttonarea.add (equal);
buttonarea.add (zero);
buttonarea.add (one);
buttonarea.add (add);
buttonarea.add (two);
buttonarea.add (three);
buttonarea.add (subtract);
buttonarea.add (four);
buttonarea.add (five);
buttonarea.add (multiply);
buttonarea.add (six);
buttonarea.add (seven);
buttonarea.add (square);
buttonarea.add (eight);
buttonarea.add (nine);
buttonarea.add (dot);
}
虽然空布局和 setBounds()
对于 Swing 新手来说似乎是创建复杂 GUI 的最简单和最好的方法,但您创建的 Swing GUI 越多,您在使用时 运行 遇到的困难就越严重他们。它们不会在 GUI 调整大小时调整组件的大小,它们是皇家 来增强或维护的,当放置在滚动窗格中时它们会完全失效,当在所有平台或不同的屏幕分辨率上查看时它们看起来很糟糕从原来的。
为主 GUI 使用 BorderLayout,放置 JTextField BorderLayout.PAGE_START。使用 JPanel 将 JButton 放置在 GridLayout 中,并将此 JPanel 放置在主 GUI 的 BorderLayout.CENTER 位置。
例如这个 GUI:
可以用这个 GUI 制作:
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
@SuppressWarnings("serial")
public class SimpleCalc extends JPanel {
private static final String[][] BTN_TEXTS = {
{"C", "Del", "X^2", "+"},
{"7", "8", "9", "-"},
{"4", "5", "6", "*"},
{"1", "2", "3", "/"},
{"0", ".", "", "="}
};
private static final int COLS = 12;
private static final int GAP = 3;
private static final char[] NUMBER_ARRAY = "0123456789".toCharArray();
private static final List<Character> NUMBER_LIST = new ArrayList<>();
private static final float FONT_SIZE = 36f;
private JTextField display = new JTextField(COLS);
static {
for (char c : NUMBER_ARRAY) {
NUMBER_LIST.add(c);
}
}
public SimpleCalc() {
display.setFont(display.getFont().deriveFont(FONT_SIZE));
display.setFocusable(false);
int rows = BTN_TEXTS.length;
int cols = BTN_TEXTS[0].length;
JPanel buttonPanel = new JPanel(new GridLayout(rows, cols, GAP, GAP));
for (String[] btnTextRow : BTN_TEXTS) {
for (String btnText : btnTextRow) {
if (btnText.isEmpty()) {
buttonPanel.add(new JLabel());
} else {
Action action = null;
if (NUMBER_LIST.contains(btnText.charAt(0))) {
action = new NumericAction(btnText);
} else if (".".equals(btnText)) {
action = new DotAction(btnText);
} else {
action = new OperationAction(btnText);
}
JButton button = new JButton(action);
button.setFont(button.getFont().deriveFont(Font.BOLD, FONT_SIZE));
buttonPanel.add(button);
}
}
}
setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
setLayout(new BorderLayout(GAP, GAP));
add(display, BorderLayout.PAGE_START);
add(buttonPanel, BorderLayout.CENTER);
}
private class NumericAction extends AbstractAction {
public NumericAction(String name) {
super(name);
}
@Override
public void actionPerformed(ActionEvent e) {
String text = display.getText();
text += e.getActionCommand();
display.setText(text);
}
}
private class DotAction extends AbstractAction {
public DotAction(String name) {
super(name);
}
@Override
public void actionPerformed(ActionEvent e) {
String text = display.getText();
if (text.contains(".")) {
return; // only one dot allowed
}
text += e.getActionCommand();
display.setText(text);
}
}
private class OperationAction extends AbstractAction {
public OperationAction(String name) {
super(name);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO write code for operations buttons
}
}
private static void createAndShowGui() {
SimpleCalc mainPanel = new SimpleCalc();
JFrame frame = new JFrame("SimpleCalc");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
add (header);
add (buttonarea);
我猜您正在使用使用 BorderLayout 的 JFrame。上面的代码在 BorderLayout 的 CENTER 中添加了两个组件,这是不允许的。
相反,您应该指定适当的约束条件:
add (header, BorderLayout.PAGE_NORTH);
add (buttonarea, BorderLayout.CENTER);
如果您没有在框架上使用 BorderLayout,那么您应该这样做。阅读有关 How to Use BorderLayout 的 Swing 教程部分,了解更多信息和示例。
此外,在创建 JTextAra 时,您应该使用:
written = new JTextArea (row, columns);
给文本区域适当的大小。在这种情况下,您可以直接将文本区域添加到框架中:
//add (header, BorderLayout.PAGE_NORTH);
add (written, BorderLayout.PAGE_NORTH);
不需要包装面板。
在这段代码中,前三个按钮出现在文本区域,如何让这些按钮从 header 下面开始?在添加按钮之前,按钮区域从 header 下方开始,但一旦我添加按钮,它就会出现在 header
上 public Design ()
{
header = new JPanel ();
header.setBounds(0, 0, 300, 100);
header.setBackground(Color.WHITE);
header.setLayout (null);
buttonarea = new JPanel ();
buttonarea.setBounds (0,0, 300, 300);
buttonarea.setBackground(Color.BLACK);
buttonarea.setLayout(new GridLayout (6,3));
add (header);
add (buttonarea);
zero = new JButton ("0");
zero.setBackground(Color.PINK);
one = new JButton ("1");
one.setBackground(Color.PINK);
two = new JButton ("2");
two.setBackground(Color.PINK);
three = new JButton ("3");
three.setBackground(Color.PINK);
four = new JButton ("4");
four.setBackground(Color.PINK);
five = new JButton ("5");
five.setBackground(Color.PINK);
six = new JButton ("6");
six.setBackground(Color.PINK);
seven = new JButton ("7");
seven.setBackground(Color.PINK);
eight = new JButton ("8");
eight.setBackground(Color.PINK);
nine = new JButton ("9");
nine.setBackground(Color.PINK);
add = new JButton ("+");
add.setBackground(Color.PINK);
subtract = new JButton ("-");
subtract.setBackground(Color.PINK);
divide = new JButton ("*");
divide.setBackground(Color.PINK);
multiply = new JButton ("/");
multiply.setBackground(Color.PINK);
square = new JButton ("x^2");
square.setBackground(Color.PINK);
equal = new JButton ("=");
equal.setBackground(Color.PINK);
c = new JButton ("C");
c.setBackground(Color.PINK);
clear = new JButton ("delete");
clear.setBackground(Color.PINK);
dot = new JButton (".");
dot.setBackground(Color.PINK);
written = new JTextArea ();
written.setBackground(Color.WHITE);
header.add (written);
buttonarea.add (c);
buttonarea.add (clear);
buttonarea.add (equal);
buttonarea.add (zero);
buttonarea.add (one);
buttonarea.add (add);
buttonarea.add (two);
buttonarea.add (three);
buttonarea.add (subtract);
buttonarea.add (four);
buttonarea.add (five);
buttonarea.add (multiply);
buttonarea.add (six);
buttonarea.add (seven);
buttonarea.add (square);
buttonarea.add (eight);
buttonarea.add (nine);
buttonarea.add (dot);
}
虽然空布局和 setBounds()
对于 Swing 新手来说似乎是创建复杂 GUI 的最简单和最好的方法,但您创建的 Swing GUI 越多,您在使用时 运行 遇到的困难就越严重他们。它们不会在 GUI 调整大小时调整组件的大小,它们是皇家 来增强或维护的,当放置在滚动窗格中时它们会完全失效,当在所有平台或不同的屏幕分辨率上查看时它们看起来很糟糕从原来的。
为主 GUI 使用 BorderLayout,放置 JTextField BorderLayout.PAGE_START。使用 JPanel 将 JButton 放置在 GridLayout 中,并将此 JPanel 放置在主 GUI 的 BorderLayout.CENTER 位置。
例如这个 GUI:
可以用这个 GUI 制作:
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
@SuppressWarnings("serial")
public class SimpleCalc extends JPanel {
private static final String[][] BTN_TEXTS = {
{"C", "Del", "X^2", "+"},
{"7", "8", "9", "-"},
{"4", "5", "6", "*"},
{"1", "2", "3", "/"},
{"0", ".", "", "="}
};
private static final int COLS = 12;
private static final int GAP = 3;
private static final char[] NUMBER_ARRAY = "0123456789".toCharArray();
private static final List<Character> NUMBER_LIST = new ArrayList<>();
private static final float FONT_SIZE = 36f;
private JTextField display = new JTextField(COLS);
static {
for (char c : NUMBER_ARRAY) {
NUMBER_LIST.add(c);
}
}
public SimpleCalc() {
display.setFont(display.getFont().deriveFont(FONT_SIZE));
display.setFocusable(false);
int rows = BTN_TEXTS.length;
int cols = BTN_TEXTS[0].length;
JPanel buttonPanel = new JPanel(new GridLayout(rows, cols, GAP, GAP));
for (String[] btnTextRow : BTN_TEXTS) {
for (String btnText : btnTextRow) {
if (btnText.isEmpty()) {
buttonPanel.add(new JLabel());
} else {
Action action = null;
if (NUMBER_LIST.contains(btnText.charAt(0))) {
action = new NumericAction(btnText);
} else if (".".equals(btnText)) {
action = new DotAction(btnText);
} else {
action = new OperationAction(btnText);
}
JButton button = new JButton(action);
button.setFont(button.getFont().deriveFont(Font.BOLD, FONT_SIZE));
buttonPanel.add(button);
}
}
}
setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
setLayout(new BorderLayout(GAP, GAP));
add(display, BorderLayout.PAGE_START);
add(buttonPanel, BorderLayout.CENTER);
}
private class NumericAction extends AbstractAction {
public NumericAction(String name) {
super(name);
}
@Override
public void actionPerformed(ActionEvent e) {
String text = display.getText();
text += e.getActionCommand();
display.setText(text);
}
}
private class DotAction extends AbstractAction {
public DotAction(String name) {
super(name);
}
@Override
public void actionPerformed(ActionEvent e) {
String text = display.getText();
if (text.contains(".")) {
return; // only one dot allowed
}
text += e.getActionCommand();
display.setText(text);
}
}
private class OperationAction extends AbstractAction {
public OperationAction(String name) {
super(name);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO write code for operations buttons
}
}
private static void createAndShowGui() {
SimpleCalc mainPanel = new SimpleCalc();
JFrame frame = new JFrame("SimpleCalc");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
add (header);
add (buttonarea);
我猜您正在使用使用 BorderLayout 的 JFrame。上面的代码在 BorderLayout 的 CENTER 中添加了两个组件,这是不允许的。
相反,您应该指定适当的约束条件:
add (header, BorderLayout.PAGE_NORTH);
add (buttonarea, BorderLayout.CENTER);
如果您没有在框架上使用 BorderLayout,那么您应该这样做。阅读有关 How to Use BorderLayout 的 Swing 教程部分,了解更多信息和示例。
此外,在创建 JTextAra 时,您应该使用:
written = new JTextArea (row, columns);
给文本区域适当的大小。在这种情况下,您可以直接将文本区域添加到框架中:
//add (header, BorderLayout.PAGE_NORTH);
add (written, BorderLayout.PAGE_NORTH);
不需要包装面板。