调整窗口大小时,Container中的元素如何调整? [英] How elements in Container adjust when we resize window?

查看:233
本文介绍了调整窗口大小时,Container中的元素如何调整?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是窗口的代码,该窗口在用户单击按钮时会更改颜色,而在单击另一个按钮时会更改标签的文本.

Below is the code for a Window which changes color when the user clicks a button, and changes the text of a label when he clicks the other button.

它有两个按钮,一个用于保持按钮的面板,一个标签,以及一个用于图形的面板.

It has two buttons, a panel for holding buttons, a label, and a panel for graphics.

概念说明: 首先,我使用默认的BorderLayoutlabel添加到frameNorth部分.然后,我在单独的面板中添加了两个buttons,并将该panel添加到主frameSouth部分中.然后,在主frameCenter中为图形添加了panel.

Conceptual explanation: First, I added the label to the North part of the frame using the default BorderLayout. Then I added the two buttons in a separate panel, and I added that panel to the South part of the main frame. Then I added the panel for the graphics in the Center of the main frame.

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);
 }
}

这是上面代码的输出.到目前为止,一切都很好.

Here is the output of above code.So far so good.

但是当我开始从右侧滑动时,会发生以下情况:

But when I start to slide from the right-hand side, the following happens:

现在,窗口的右壁朝左,但是按钮是固定的.继续进行下去,我们得到:

Now the right wall of the window is coming towards the left, but the buttons are stationary. Continuing this we get:

但是,如果我们从左侧调整大小,则会得到:

However, if we resize from the left side, we get:

此处,按钮似乎与框架一起向右移动(保持其与框架边界的距离).这与先前的情况相反.但为什么?怎么样?

Here the button seems to move towards the right, along with the frame (maintaining its distance from the frame boundary). This is the opposite of the previous case. But Why? How?

最后,我们有:

继续此操作,"ColorChange"按钮也变得部分不可见.

Continuing this, the "ColorChange" button also becomes partially invisible.

如果我们从右向左滑动而从左向右滑动,为什么会发生这两种不同的事情?

Why are these two different things happening, if we slide from right to left versus left to right?

我已经搜索过,但是无法找到准确的答案,所以最后我在这里问:为什么和如何?

I have searched, but I was unable to find an accurate answer to this, so finally I am asking here: Why and How?

推荐答案

您已经说明了问题,但尚未说明要求.

You have explained the problem, but you haven't explained the requirement.

也许您应该考虑框布局

panel.add( Box.createHorizontalGlue() );
panel.add( button1 );
panel.add( Box.createHorizontalStrut(20) );
panel.add( button2 );
panel.add( Box.createHorizontalGlue() );

这将创建一个面板,其中按钮保持居中,两个按钮之间的间距为20像素.

This will create a panel where the buttons remain centered with a gap of 20 pixels between the two buttons.

这篇关于调整窗口大小时,Container中的元素如何调整?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆