我正在给自己一个关于重新学习Java的速成课程。我正在编写一个非常简单的程序,当您将按钮单击到随机面板时,它只会更改按钮的位置。没有真正的问题,我几乎完成了我想要的程序。但是,我想知道为什么当我将布局样式应用到第一个面板(buttonPanel1)时,它会自动应用到每个面板?
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package buttonswitch;
import javax.swing.*;
import java.awt.*;
import javax.swing.border.*;
import java.awt.event.*;
import java.util.*;
/**
*
* @author Supreme Lenova
*/
import java.util.Random;
public class ButtonWindow extends JFrame{
private JPanel buttonPanel1;
private JPanel buttonPanel2;
private JPanel buttonPanel3;
private JPanel buttonPanel4;
private JButton Button;
private Border raisedbevel, loweredbevel;
private Border compound;
public ButtonWindow(){
setTitle("Button Game");
setLocation(600,50);
setDefaultCloseOperation(EXIT_ON_CLOSE);
GridLayout grid = new GridLayout(2,2);
setLayout(grid);
raisedbevel = BorderFactory.createRaisedBevelBorder();
loweredbevel = BorderFactory.createLoweredBevelBorder();
compound = BorderFactory.createCompoundBorder(raisedbevel, loweredbevel);
buildPanels();
Button = new JButton("Click!");
Button.setAlignmentX(Component.CENTER_ALIGNMENT);
Button.addActionListener(new ButtonListener());
buttonPanel1.add(Button);
buttonPanel1.add(Box.createVerticalGlue());
setSize(300,300);
setVisible(true);
}
private void buildPanels(){
buttonPanel1 = new JPanel();
buttonPanel2 = new JPanel();
buttonPanel3 = new JPanel();
buttonPanel4 = new JPanel();
buttonPanel1.setLayout(new BoxLayout(buttonPanel1, BoxLayout.PAGE_AXIS));
buttonPanel1.add(Box.createVerticalGlue());
buttonPanel1.setBorder(compound);
buttonPanel2.setBorder(compound);
buttonPanel3.setBorder(compound);
buttonPanel4.setBorder(compound);
this.add(buttonPanel1);
this.add(buttonPanel2);
this.add(buttonPanel3);
this.add(buttonPanel4);
}
private class ButtonListener implements ActionListener{
public void actionPerformed(ActionEvent e){
generateButton();
}
private void generateButton(){
int last = 5;
int place = 5;
Random rand = new Random();
while(place==last){
place = rand.nextInt(4)+1;
}
last = place;
switch (place){
case 1:
repaint();
buttonPanel1.add(Button);
break;
case 2:
repaint();
buttonPanel2.add(Button);
break;
case 3:
repaint();
buttonPanel3.add(Button);
break;
case 4:
repaint();
buttonPanel4.add(Button);
break;
}
}
}
}
答案 0 :(得分:0)
将布局管理器设置为function getDatetimeNow() {
$tz_object = new DateTimeZone('Asia/Kolkata');
//date_default_timezone_set('Brazil/East');
$datetime = new DateTime();
$datetime->setTimezone($tz_object);
return $datetime->format('Y-m-d H:i:s');
}
buttonPanel1
不会将布局管理器更改为默认使用buttonPanel1.setLayout(new BoxLayout(buttonPanel1, BoxLayout.PAGE_AXIS));
buttonPanel1.add(Box.createVerticalGlue());
的其他面板。
它确实会影响按钮的大小。打印
FlowLayout
现在更改System.out.println(Button.getSize()); //use button, not Button
buttonPanel1
并更改代码以将按钮添加到面板:
buttonPanel1.setLayout(new BorderLayout());
再次打印。
布局管理器更改按钮的初始大小。其他3个小组的buttonPanel1.add(Button, BorderLayout.CENTER);
经理不会改变大小。