当我们调整 window 大小时,Container 中的元素如何调整?
How elements in Container adjust when we resize window?
下面是 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);
}
}
这是上面 code.So 到目前为止的输出。
但是当我开始从 right-hand 侧滑动时,会发生以下情况:
现在 window 的右墙向左移动,但按钮是静止的。继续这个我们得到:
但是,如果我们从左侧调整大小,我们会得到:
这里的按钮似乎随着框架向右移动(保持与框架边界的距离)。这与之前的情况相反。但为什么?怎么样?
最后,我们有:
继续这样做,"ColorChange" 按钮也变得部分不可见。
如果我们从右向左滑动与从左向右滑动,为什么会发生这两种不同的情况?
我已经搜索过了,但是找不到准确的答案,所以最后我在这里问:为什么以及如何?
这可能是您需要的:https://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/size.html
我有一段时间没有在 Swing/JavaFX 上工作了,但我相信你可以给 frame.pack();一个镜头。
这是因为你在
的差距值
int pad = 80;
buttonPanel.setLayout(new FlowLayout(FlowLayout.LEADING, pad, 20));
。它总是80px。所以第一个按钮总是 距离面板的"beginning" 80px。如果您从右向左滑动,除了一个按钮消失之外没有任何变化,因为没有足够的 space 来填补 3 个空白。但是,如果你从左向右滑动,第一个按钮不会先消失,因为如前所述,它有的差距80px 从头开始。对不起,我的英语很糟糕,但我希望我能帮到你。
你已经说明了问题,但是你还没有说明需求。
也许你应该考虑 Box Layout
panel.add( Box.createHorizontalGlue() );
panel.add( button1 );
panel.add( Box.createHorizontalStrut(20) );
panel.add( button2 );
panel.add( Box.createHorizontalGlue() );
这将创建一个面板,其中按钮保持居中,两个按钮之间有 20 像素的间隙。
下面是 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);
}
}
这是上面 code.So 到目前为止的输出。
但是当我开始从 right-hand 侧滑动时,会发生以下情况:
现在 window 的右墙向左移动,但按钮是静止的。继续这个我们得到:
但是,如果我们从左侧调整大小,我们会得到:
这里的按钮似乎随着框架向右移动(保持与框架边界的距离)。这与之前的情况相反。但为什么?怎么样?
最后,我们有:
继续这样做,"ColorChange" 按钮也变得部分不可见。
如果我们从右向左滑动与从左向右滑动,为什么会发生这两种不同的情况?
我已经搜索过了,但是找不到准确的答案,所以最后我在这里问:为什么以及如何?
这可能是您需要的:https://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/size.html
我有一段时间没有在 Swing/JavaFX 上工作了,但我相信你可以给 frame.pack();一个镜头。
这是因为你在
的差距值int pad = 80;
buttonPanel.setLayout(new FlowLayout(FlowLayout.LEADING, pad, 20));
。它总是80px。所以第一个按钮总是 距离面板的"beginning" 80px。如果您从右向左滑动,除了一个按钮消失之外没有任何变化,因为没有足够的 space 来填补 3 个空白。但是,如果你从左向右滑动,第一个按钮不会先消失,因为如前所述,它有的差距80px 从头开始。对不起,我的英语很糟糕,但我希望我能帮到你。
你已经说明了问题,但是你还没有说明需求。
也许你应该考虑 Box Layout
panel.add( Box.createHorizontalGlue() );
panel.add( button1 );
panel.add( Box.createHorizontalStrut(20) );
panel.add( button2 );
panel.add( Box.createHorizontalGlue() );
这将创建一个面板,其中按钮保持居中,两个按钮之间有 20 像素的间隙。