[英]How elements in Container adjust when we resize window?
下面是窗口的代码,当用户单击按钮时,窗口会更改颜色,并在单击其他按钮时更改标签的文本。
它有两个按钮,一个用于按住按钮的面板,一个标签和一个用于图形的面板。
概念解释:首先,我使用默认的BorderLayout
将label
添加到frame
的North
。 然后我在一个单独的面板中添加了两个buttons
,然后我将该panel
添加到主frame
的South
。 然后我在主frame
的Center
添加了图形panel
。
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
class GuiAnimation extends JFrame
{
JButton colorBut;
JButton labelBut;
JLabel label;
PaintCanvas firstCanvas;
JPanel buttonPanel;
GuiAnimation()
{
colorBut = new JButton("Color Change");
labelBut = new JButton("Text Change");
label = new JLabel("I will change");
firstCanvas = new PaintCanvas();
buttonPanel = new JPanel();
addListener(); //May be i should make different method for different component for the flexibility.But here program is small.
addEverything();
}
private void addListener()
{
colorBut.addActionListener(new ColorListener());
labelBut.addActionListener(new LabelListener());
}
private void setButtonInPanel()
{
int pad = 80;
buttonPanel.setLayout(new FlowLayout(FlowLayout.LEADING, pad, 20));
buttonPanel.add(colorBut);
buttonPanel.add(labelBut);
}
void addEverything() //Here things are done with Layouts.
{
//add the label to the top of the window.
this.getContentPane().add(BorderLayout.NORTH, label);
//add button inside the panel.
setButtonInPanel();
//add that panel that has button to the frame.
this.getContentPane().add(BorderLayout.SOUTH, buttonPanel);
//add the graphics canvas to the frame.
this.getContentPane().add(BorderLayout.CENTER, firstCanvas);
}
class PaintCanvas extends JPanel
{
public void paintComponent(Graphics g)
{
int red = (int) (Math.random() * 255);
int green = (int) (Math.random() * 255);
int blue = (int) (Math.random() * 255);
Color randomColor = new Color(red, green, blue);
g.setColor(randomColor);
g.fillRect(0, 0, getWidth(), getHeight());
}
}
class ColorListener implements ActionListener
{
public void actionPerformed(ActionEvent e){
firstCanvas.repaint();
}
}
class LabelListener implements ActionListener
{
public void actionPerformed(ActionEvent e){
if ( label.getText().equals("I will change"))
label.setText("yes yes yes clicked");
else
label.setText("I will change");
}
}
public static void main(String[] args) {
GuiAnimation gui = new GuiAnimation();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setSize(500,600);
gui.setVisible(true);
}
}
这是上面代码的输出。太好了。
但是当我从右侧开始滑动时,会发生以下情况:
现在窗户的右墙朝左,但按钮是静止的。 继续这个我们得到:
但是,如果我们从左侧调整大小,我们会得到:
这里的按钮似乎与框架一起向右移动(保持其与框架边界的距离)。 这与前一案例相反。 但为什么? 怎么样?
最后,我们有:
继续这一点,“ColorChange”按钮也变得部分不可见。
如果我们从右向左滑动而从左向右滑动,为什么会发生这两种不同的事情呢?
我已经搜索过,但我无法找到准确的答案,所以最后我在这里问:为什么和如何?
您已解释了该问题,但您尚未解释该要求。
也许你应该考虑一个Box Layout
panel.add( Box.createHorizontalGlue() );
panel.add( button1 );
panel.add( Box.createHorizontalStrut(20) );
panel.add( button2 );
panel.add( Box.createHorizontalGlue() );
这将创建一个面板,其中按钮保持居中,两个按钮之间的间隙为20像素。
这是因为你的差距值
int pad = 80;
buttonPanel.setLayout(new FlowLayout(FlowLayout.LEADING, pad, 20));
。 它总是80px。 所以第一个按钮总是距离面板的“开头”80px。 如果从右向左滑动,则没有任何变化,除了一个按钮消失,因为没有足够的空间来填充3个间隙。 但是,如果你从左向右滑动,第一个按钮不会首先消失,因为如前所述,它必须从一开始就有80px的间隙。 对不起我的悲惨英语,但我希望我有所帮助。
这可能是你需要的: https : //docs.oracle.com/javase/tutorial/uiswing/lookandfeel/size.html
我有一段时间没有参与Swing / JavaFX,但我确实相信你可以提供frame.pack(); 一枪。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.