Java 分区计算器接口
在这段代码中,前三个按钮出现在文本区域中,如何使这些按钮从标题下方开始?在添加按钮之前,buttonarea从标题下方开始,但一旦添加按钮,它就会出现在标题上Java 分区计算器接口,java,swing,calculator,Java,Swing,Calculator,在这段代码中,前三个按钮出现在文本区域中,如何使这些按钮从标题下方开始?在添加按钮之前,buttonarea从标题下方开始,但一旦添加按钮,它就会出现在标题上 public Design () { header = new JPanel (); header.setBounds(0, 0, 300, 100); header.setBackground(Color.WHITE); header.setLayout (null); butto
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);
}
虽然空布局和设置框似乎是让新手喜欢的创建复杂GUI的最简单和最好的方法,但您创建的GUI越多,在使用它们时遇到的困难就越严重。当GUI调整大小时,它们不会调整您的组件的大小,它们是一个需要增强或维护的皇家女巫,当它们放置在滚动窗格中时会完全失败,当在所有平台或屏幕分辨率与原始分辨率不同的情况下查看时,它们看起来非常糟糕
在主GUI中使用BorderLayout,将JTextField BorderLayout.PAGE_放置在开始位置。使用JPanel将JButtons放置在GridLayout中,并将此JPanel放置在主GUI的BorderLayout.CENTER位置
例如,此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();
}
});
}
}
可以使用此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();
}
});
}
}
虽然空布局和设置框似乎是让新手喜欢的创建复杂GUI的最简单和最好的方法,但您创建的GUI越多,在使用它们时遇到的困难就越严重。当GUI调整大小时,它们不会调整您的组件的大小,它们是一个需要增强或维护的皇家女巫,当它们放置在滚动窗格中时会完全失败,当在所有平台或屏幕分辨率与原始分辨率不同的情况下查看时,它们看起来非常糟糕
在主GUI中使用BorderLayout,将JTextField BorderLayout.PAGE_放置在开始位置。使用JPanel将JButtons放置在GridLayout中,并将此JPanel放置在主GUI的BorderLayout.CENTER位置
例如,此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();
}
});
}
}
可以使用此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();
}
});
}
}
我猜您使用的是使用边框布局的JFrame。上面的代码向BorderLayout的中心添加了两个组件,这是不允许的
相反,您应该指定适当的约束:
add (header, BorderLayout.PAGE_NORTH);
add (buttonarea, BorderLayout.CENTER);
如果您没有在框架上使用边框布局,那么您应该这样做。有关更多信息和示例,请阅读上Swing教程的部分
此外,在创建JTextAra时,您应该使用:
written = new JTextArea (row, columns);
为文本区域指定适当的大小。在这种情况下,您可以直接将文本区域添加到框架中:
//add (header, BorderLayout.PAGE_NORTH);
add (written, BorderLayout.PAGE_NORTH);
不需要包装器面板
我猜您使用的是使用边框布局的JFrame。上面的代码向BorderLayout的中心添加了两个组件,这是不允许的
相反,您应该指定适当的约束:
add (header, BorderLayout.PAGE_NORTH);
add (buttonarea, BorderLayout.CENTER);
如果您没有在框架上使用边框布局,那么您应该这样做。有关更多信息和示例,请阅读上Swing教程的部分
此外,在创建JTextAra时,您应该使用:
written = new JTextArea (row, columns);
为文本区域指定适当的大小。在这种情况下,您可以直接将文本区域添加到框架中:
//add (header, BorderLayout.PAGE_NORTH);
add (written, BorderLayout.PAGE_NORTH);
不需要包装器面板