我正在使用Eclipse中的swing创建一个计算器程序。 有几个按钮0-9,+,-,*,/等。 到目前为止,我已经创建了3个面板。我需要5,我想把它们放成
clear / -> Fifth panel
7 8 9 + -> Fourth panel
4 5 6 - -> Third panel
1 2 3 * -> Second panel
0 = . -> First panel
但是按钮重叠或有些东西,我无法将它们分开
我尝试了不同的BorderLayout。____,但他们仍然无法分开 例如:
add(First, BorderLayout.BEFORE_FIRST_LINE);
add(Second, BorderLayout.EAST);
add(Third, BorderLayout.CENTER);
这是我的代码
import java.awt.*;
import javax.swing.*; // swing offers many GUI components
import java.awt.event.*;
public class calculator
{
public static void main (String [] args)
{
GUI myCalc = new GUI();
myCalc.setTitle("Calculator");
myCalc.setSize(350, 500);
myCalc.setVisible(true);
myCalc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class GUI extends JFrame implements ActionListener, MouseListener
{
JPanel myFirstPanel;
JButton zero, one, two, three, four, five, six, seven, eight, nine,
dot, backspace, plus, minus, multiply, divide, clear;
JLabel msgLabel;
JTextField textBox;
GUI()
{
super();
JPanel First = new JPanel();
JPanel Second = new JPanel();
JPanel Third = new JPanel();
one = new JButton("1");
two = new JButton("2");
three = new JButton("3");
four = new JButton("4");
five = new JButton("5");
six = new JButton("6");
seven = new JButton("7");
eight = new JButton("8");
nine = new JButton("9");
First.add(one);
First.add(two);
First.add(three);
Second.add(four);
Second.add(five);
Second.add(six);
Third.add(seven);
Third.add(eight);
Third.add(nine);
add(First, BorderLayout.BEFORE_FIRST_LINE);
add(Second, BorderLayout.EAST);
add(Third, BorderLayout.CENTER);
add(First);
add(Second);
add(Third);
}
public void actionPerformed(ActionEvent e)
{
}
public void mouseExited(MouseEvent me) // when mouse leaves
{
}
public void mouseEntered(MouseEvent me) // when mouse enters
{
}
public void mouseReleased(MouseEvent me) // when mouse is released after press
{
}
public void mouseClicked(MouseEvent me) // when mouse is clicked
{
}
public void mousePressed(MouseEvent me) // when mouse is pressed and held
{
}
}
我一直只获得789个按钮:/ 我对此并不陌生,因此任何解释都将有所帮助。谢谢
答案 0 :(得分:1)
作为“一般”建议,您可以探索其他布局管理器。最终,您将到达一个探索GridBagLayout
的地步。
这是迄今为止最灵活的布局管理器,这也使其最复杂。
您应该特别从How to layout components within a container和How to use GridBagLayout开始
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Test {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new GridBagLayout());
// First line
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 3;
gbc.fill = GridBagConstraints.BOTH;
add(new JButton("Clear"), gbc);
gbc.gridwidth = 1;
gbc.gridx = 3;
add(new JButton("/"), gbc);
// Second line
gbc.gridx = 0;
gbc.gridy = 1;
add(new JButton("7"), gbc);
gbc.gridx++;
add(new JButton("8"), gbc);
gbc.gridx++;
add(new JButton("9"), gbc);
gbc.gridx++;
add(new JButton("+"), gbc);
// Third line
gbc.gridx = 0;
gbc.gridy = 2;
add(new JButton("4"), gbc);
gbc.gridx++;
add(new JButton("5"), gbc);
gbc.gridx++;
add(new JButton("6"), gbc);
gbc.gridx++;
add(new JButton("-"), gbc);
// Second line
gbc.gridx = 0;
gbc.gridy = 3;
add(new JButton("1"), gbc);
gbc.gridx++;
add(new JButton("2"), gbc);
gbc.gridx++;
add(new JButton("3"), gbc);
gbc.gridx++;
add(new JButton("*"), gbc);
// Last line
gbc.gridx = 0;
gbc.gridy = 4;
add(new JButton("0"), gbc);
gbc.gridx++;
gbc.gridwidth = 2;
add(new JButton("="), gbc);
gbc.gridx += 2;
gbc.gridwidth = 1;
add(new JButton("."), gbc);
}
}
}