内部窗口的默认布局好像是流水布局,你就好改成网格布局.
如:content.setLayout(new GridBagLayout(行数,列数,格与控件水平空隙,
格与控件垂直空隙);
这样可以简单的完成比较好看的布局(跟Windows自带的差不多),至于详细的
你可以运行几个布局管理类型混合使用的方法来完成.
如:content.setLayout(new GridBagLayout(行数,列数,格与控件水平空隙,
格与控件垂直空隙);
这样可以简单的完成比较好看的布局(跟Windows自带的差不多),至于详细的
你可以运行几个布局管理类型混合使用的方法来完成.
Java本身有多种不同功能的布局器,你一一了解后就可以找到你的解决方案了。
或者用GridBagLayout+GridBagConstraints
import java.awt.event.*;import javax.swing.*;
import javax.swing.border.*;public class Calculator extends JFrame
{
JPanel displayPane = new JPanel();
JPanel buttonPane = new JPanel(); JPanel numPane = new JPanel();
JPanel memoryPane = new JPanel();
JPanel operatorPane = new JPanel();
JPanel funcPane = new JPanel();
JButton button;
JLabel label;
public Calculator(String title)
{
super(title);
JTextField display = new JTextField((32));
display.setHorizontalAlignment(JTextField.RIGHT);
display.setText("0.");
display.setEditable(false);
display.setBackground(Color.white);
displayPane.add(display, BorderLayout.NORTH);
funcPane.setLayout(new BoxLayout(funcPane,BoxLayout.X_AXIS));
funcPane.setBorder(BorderFactory.createEtchedBorder(0)); funcPane.add(label = new JLabel(" "));
label.setBorder(BorderFactory.createBevelBorder(1));
funcPane.add(Box.createRigidArea(new Dimension(35,0))); funcPane.add(button = new JButton("Back Space"));
button.setPreferredSize(new Dimension(90,30));
funcPane.add(Box.createRigidArea(new Dimension(15,0)));
button.setForeground(Color.RED);
funcPane.add(button = new JButton(" CE "));
button.setPreferredSize(new Dimension(90,30));
funcPane.add(Box.createRigidArea(new Dimension(15,0)));
button.setForeground(Color.RED); funcPane.add(button = new JButton(" C "));
button.setPreferredSize(new Dimension(90,30));
funcPane.add(Box.createRigidArea(new Dimension(15,0)));
button.setForeground(Color.RED);
buttonPane.add(funcPane, BorderLayout.NORTH);
funcPane.setBorder(BorderFactory.createEmptyBorder(0,60,10,10));
memoryPane.setLayout(new GridLayout(4,1,10,10));
memoryPane.add(button = new JButton("MC"));
button.setForeground(Color.RED);
memoryPane.add(button = new JButton("MR"));
button.setForeground(Color.RED);
memoryPane.add(button = new JButton("MS"));
button.setForeground(Color.RED);
memoryPane.add(button = new JButton("M+"));
button.setForeground(Color.RED);
buttonPane.add(memoryPane, BorderLayout.WEST);
numPane.setLayout(new GridLayout(4,3,10,10));
numPane.add(button = new JButton("7"));
numPane.add(button = new JButton("8"));
numPane.add(button = new JButton("9"));
numPane.add(button = new JButton("4"));
numPane.add(button = new JButton("5"));
numPane.add(button = new JButton("6"));
numPane.add(button = new JButton("1"));
numPane.add(button = new JButton("2"));
numPane.add(button = new JButton("3"));
numPane.add(button = new JButton("0"));
numPane.add(button = new JButton("+/-"));
numPane.add(button = new JButton(".")); buttonPane.add(numPane, BorderLayout.CENTER);
operatorPane.setLayout(new GridLayout(4,2,10,10));
operatorPane.add(button = new JButton("/"));
button.setForeground(Color.RED);
operatorPane.add(button = new JButton("sqrt"));
operatorPane.add(button = new JButton("*"));
button.setForeground(Color.RED);
operatorPane.add(button = new JButton("%"));
operatorPane.add(button = new JButton("-"));
button.setForeground(Color.RED);
operatorPane.add(button = new JButton("1/x"));
operatorPane.add(button = new JButton("+"));
button.setForeground(Color.RED);
operatorPane.add(button = new JButton("="));
button.setForeground(Color.RED);
buttonPane.add(operatorPane, BorderLayout.EAST);
//buttonPane.setBorder(BorderFactory.createEtchedBorder(1));
Container c = getContentPane();
c.add(displayPane, BorderLayout.NORTH);
c.add(buttonPane,BorderLayout.CENTER);
}
public void centerWindow(int width, int height)
{
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
setBounds((screenSize.width - width) / 2,
(screenSize.height - height) / 2,
width, height);
}
public static void main(String[] args)
{
Calculator c = new Calculator("Calculator");
c.setResizable(false);
c.centerWindow(400,260);
c.setDefaultCloseOperation(EXIT_ON_CLOSE);
c.setVisible(true);
}
}