9.2 布局管理器概述 JPanel默认布局是流式布局 Component子组件非常多 大多情况下,容器是组件,容器可以包含容器和组件,但是也有特例: JFrame 是顶层容器,不能被其他组件包含 JComp
9.2 布局管理器概述
JPanel默认布局是流式布局
Component子组件非常多
大多情况下,容器是组件,容器可以包含容器和组件,但是也有特例:
JFrame 是顶层容器,不能被其他组件包含
JComponent 是组件,但是继承 JContainer,不直接继承 Component
代码:
public class Main {public static void main(String[] args) throws CloneNotSupportedException, InterruptedException {
Main solution = new Main();
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
LayoutFrame frame = new LayoutFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
class LayoutFrame extends JFrame{
public static final int W = 300;
public static final int H = 200;
public LayoutFrame(){
setTitle("layoutTest");
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension screenSize = toolkit.getScreenSize();
setBounds(((int)screenSize.getWidth()-W)/2,((int)screenSize.getHeight()-H)/2,W,H);
LayoutPanel p = new LayoutPanel();
add(p);
}
}
class LayoutPanel extends JPanel{
public LayoutPanel(){
// setLayout(new GridLayout(4,4));
for(int i = 1; i <= 16; i++){
JButton b = new JButton(String.valueOf(i));
add(b);
}
}
}
默认流式布局
代码:
public class Main {public static void main(String[] args) throws CloneNotSupportedException, InterruptedException {
Main solution = new Main();
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
LayoutFrame frame = new LayoutFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
class LayoutFrame extends JFrame{
public static final int W = 300;
public static final int H = 200;
public LayoutFrame(){
setTitle("layoutTest");
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension screenSize = toolkit.getScreenSize();
setBounds(((int)screenSize.getWidth()-W)/2,((int)screenSize.getHeight()-H)/2,W,H);
LayoutPanel p = new LayoutPanel();
add(p);
}
}
class LayoutPanel extends JPanel{
public LayoutPanel(){
// setLayout(new GridLayout(4,4));
for(int i = 1; i <= 16; i++){
JButton b = new JButton(String.valueOf(i));
add(b);
}
}
}
效果:
和系统的文件管理系统近似:
网格布局:
这个地方注释放开,改为4*4的网格布局
9.2.1 边框布局
可把控件放在东南西北中5个位置
实际上JFrame内添加了一个叫做JRootPane的组件
创建过程中,设置了JFrame的默认布局为边框布局
我们把刚刚的LayoutPanel改一下,看一下边框布局效果:
class LayoutPanel extends JPanel{public LayoutPanel(){
setLayout(new BorderLayout());
String[] layoutPoses = new String[]{BorderLayout.NORTH,
BorderLayout.SOUTH,BorderLayout.WEST,BorderLayout.EAST,
BorderLayout.CENTER};
for(int i = 0; i <layoutPoses.length; i++){
JButton b = new JButton(layoutPoses[i]);
add(b,layoutPoses[i]);
}
}
}
嵌套示例:
class LayoutPanel extends JPanel{public LayoutPanel(){
setLayout(new BorderLayout());
String[] layoutPoses = new String[]{BorderLayout.NORTH,
BorderLayout.SOUTH, BorderLayout.CENTER,
BorderLayout.WEST,BorderLayout.EAST};
for(int i = 0; i <layoutPoses.length; i++){
if(i<2){
SubLayoutPanel b = new SubLayoutPanel(1,2);
add(b,layoutPoses[i]);
}else if(i==2){
SubLayoutPanel b = new SubLayoutPanel(2,2);
add(b,layoutPoses[i]);
}else{
SubLayoutPanel b = new SubLayoutPanel(2,1);
add(b,layoutPoses[i]);
}
}
}
}
class SubLayoutPanel extends JPanel{
public SubLayoutPanel(int row, int col){
setLayout(new GridLayout(row,col));
for(int i = 0; i < row*col; i++){
JButton b = new JButton(String.valueOf(i+1));
add(b);
}
}
}
9.2.2 网格布局
和表格类似,给出几行几列,依次添加元素时添加到空格处
public class Main {public static void main(String[] args) throws CloneNotSupportedException, InterruptedException {
Main solution = new Main();
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
CalculatorFrame frame = new CalculatorFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
class CalculatorFrame extends JFrame{
public static final int W = 300;
public static final int H = 200;
public CalculatorFrame(){
setTitle("Calculator");
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension screenSize = toolkit.getScreenSize();
setBounds(((int)screenSize.getWidth()-W)/2,((int)screenSize.getHeight()-H)/2,W,H);
CalculatorPanel p = new CalculatorPanel();
add(p);
pack();
}
}
class CalculatorPanel extends JPanel{
private JButton display;
private JPanel panel;
private double result;
private String lastCommand;
private boolean start;
public CalculatorPanel(){
setLayout(new BorderLayout());
result = 0;
lastCommand="=";
start = true;
display = new JButton("0");
display.setEnabled(true);
add(display, BorderLayout.NORTH);
ActionListener insert = new InsertAction();
ActionListener command = new CommandAction();
panel = new JPanel();
panel.setLayout(new GridLayout(4,4));
String labels = "789/456*123-0.=+";
for(int i = 0; i < 16; i++){
addButton(String.valueOf(labels.charAt(i)),
i%4==3||i==14?command:insert);
}
add(panel,BorderLayout.CENTER);
}
private void addButton(String label, ActionListener listener){
JButton button = new JButton(label);
button.addActionListener(listener);
panel.add(button);
}
private class InsertAction implements ActionListener{
public void actionPerformed(ActionEvent event){
String input = event.getActionCommand();
if(start){
display.setText("");
start = false;
}
display.setText(display.getText()+input);
}
}
private class CommandAction implements ActionListener{
public void actionPerformed(ActionEvent event){
String command = event.getActionCommand();
if(start){
if(command.equals("-")){
display.setText(command);
start = false;
}else
lastCommand = command;
}else{
calculate(Double.parseDouble(display.getText()));
lastCommand = command;
start = true;
}
}
}
public void calculate(double x){
if(lastCommand.equals("+"))
result+=x;
else if(lastCommand.equals("-"))
result-=x;
else if(lastCommand.equals("*"))
result*=x;
else if(lastCommand.equals("/"))
result/=x;
else if(lastCommand.equals("="))
result=x;
display.setText(String.valueOf(result));
}
}
注意:最上方按钮代表当前输入内容
相关内容:选择 《Java核心技术 卷1》查找相关笔记
喜欢的话,点个赞吧~!平时做题,以及笔记内容将更新到公众号。
关注公众号,互相学习:钰娘娘知识汇总