java - 关于流程布局工作的困惑

标签 java swing user-interface layout-manager graphics2d

我开发了一个 Swing 小应用,上半部分有一个旋转的方 block ,下半部分有一个按钮可以停止/运行方 block 旋转。 我使用了 GridLayout 来放置旋转方 block 和按钮。 (另一种选择是使用 2 个 JPanel,一个带有旋转正方形,第二个包含按钮。使用此按钮显示适当的大小。)

这是代码:-

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;


public class Rotation {
JButton jbtn=new JButton("Stop");
component jpn2=new component();    //created a JPanel named jpn2 and got a reference to its timer object.
Timer timer=jpn2.timer;
Rotation()
{


    JFrame jfrm=new JFrame("Rotating a square about a center");
    jfrm.setSize(400,400);
    jfrm.setLayout(new GridLayout(2,1));
    jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //JPanel jpnl=new JPanel();

    //jpnl.add(jbtn);

    jbtn.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){
        if(e.getActionCommand().equals("Stop"))
        {
            timer.stop();
            jbtn.setText("Spin");
        }
        if(e.getActionCommand().equals("Spin"))
        {
            timer.start();
            jbtn.setText("Stop");
        }

    }});

    jfrm.add(jpn2);
    jfrm.add(jbtn);

    //jfrm.add(new JButton("Click"));
    jfrm.setVisible(true);
    //jfrm.setOpacity(0.8f);
}
public static void main(String args[]) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException
{
    //JFrame.setDefaultLookAndFeelDecorated(true);
    UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
    SwingUtilities.invokeLater(new Runnable(){public void run(){new Rotation();}});
}

}

class component extends JPanel implements ActionListener
{
Timer timer;
int theta=0;
component()
{
    timer=new Timer(10,this);
    timer.start();
}
public void paintComponent(Graphics g)
{
    super.paintComponent(g);
    Graphics2D g2=(Graphics2D)g;
    g2.rotate(theta,100,100);
    g2.fillRect(50, 50, 100,100);
}
public void actionPerformed(ActionEvent e)
{
    //Changing a global variable and then drawing the rectangle again and hence indirectly the square rotates.
    theta=theta+10;
    if(theta==360)
        theta=0;
    repaint();
}
}

这是输出:-

output

但我的困惑是,当我决定使用 FlowLayout 而不是 GridLayout 时,我只得到按钮而没有旋转方 block 。 据我所知,FlowLayout 将组件排成一行,如果空间小于它使用多行。 任何人都可以解决我目前无法解决的这个小愚蠢问题。

最佳答案

您的问题是您将 setSize(...) 与这些布局一起使用,这将完全按照您的要求进行 - 将 GUI 的大小设置为该大小,无论是否显示所有内容。通常,您希望避免调用 setSize(...),而是让组件在必要时覆盖 getPreferredSize(),并在显示它之前打包您的 GUI,让组件自行调整大小.请注意,FlowLayout 有其用途,但与其他布局相比,它并不是同类布局中“最智能”的布局(在我看来)。

关于java - 关于流程布局工作的困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13070113/

相关文章:

java - 创建 IndexSearcher 抛出 IOException 读取过去的 EOF

java - k 个不同因子与最大可能因子 n 的可能乘法

java - 重新验证和重绘不起作用

java - 使用多个按钮创建弹出选项框

user-interface - 高效的用户界面

Java 线程和同步列表

仅用于副作用的 Java 构造函数

java - JOptionPane.showInputDialog() 是否返回不同的字符串

java - Android-键盘打开时表单内容不调整

cocoa - 使用 Mac OS X Cocoa 绘制文本时如何重现插入文本样式?