java 没有布局管理器的响应式 JFrame
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28171714/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Responsive JFrame without Layout Manager
提问by Elyas Hadizadeh
I am trying to do have two button in JFrame
, and I call the setBounds
method of them for setting the positions and size of them and also I passed null
to setLayout1 because I want to use
setBounds` method of component.
我试图在 中有两个按钮JFrame
,我调用setBounds
它们的方法来设置它们的位置和大小,并且我传递null
给setLayout1 because I want to use
组件的 setBounds 方法。
Now I want to do something with my code that whenever I resize the frame buttons decoration will change in a suitable form like below pictures:
现在我想对我的代码做一些事情,每当我调整框架按钮的大小时,装饰都会以合适的形式改变,如下图所示:
I know it is possible to use create an object from JPanel
class and add buttons to it and at the end add created panel object to frame, but I am not allowed to it right now because of some reason (specified by professor).
我知道可以使用从JPanel
类创建对象并向其添加按钮,最后将创建的面板对象添加到框架中,但由于某些原因(由教授指定),我现在不允许这样做。
Is there any way or do you have any suggestion?
有什么办法或者你有什么建议吗?
My code is like this:
我的代码是这样的:
public class Responsive
{
public static void main(String[] args)
{
JFrame jFrame = new JFrame("Responsive JFrame");
jFrame.setLayout(null);
jFrame.setBounds(0,0,400,300);
JButton jButton1 = new JButton("button 1");
JButton jButton2 = new JButton("button 2");
jButton1.setBounds(50,50,100,100);
jButton2.setBounds(150,50,100,100);
jFrame.add(jButton1);
jFrame.add(jButton2);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setVisible(true);
}
}
回答by Andrew Thompson
A FlowLayout
with no horizontal spacing, some vertical spacing and large borders could achieve that easily. A null
layout manager is neverthe answer to a 'responsive' robust GUI.
一个FlowLayout
没有水平间距,一些垂直间距和大边框可以做到这一点很容易。一个null
布局管理器是永远的答案,“响应”稳健的GUI。
import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class ResponsiveGUI {
private JComponent ui = null;
ResponsiveGUI() {
initUI();
}
public void initUI() {
if (ui!=null) return;
ui = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 8));
ui.setBorder(new EmptyBorder(10,40,10,40));
for (int i=1; i<3; i++) {
ui.add(getBigButton(i));
}
}
public JComponent getUI() {
return ui;
}
private final JButton getBigButton(int number) {
JButton b = new JButton("Button " + number);
int pad = 20;
b.setMargin(new Insets(pad, pad, pad, pad));
return b;
}
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception useDefault) {
}
ResponsiveGUI o = new ResponsiveGUI();
JFrame f = new JFrame("Responsive GUI");
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationByPlatform(true);
f.setContentPane(o.getUI());
f.pack();
f.setVisible(true);
}
};
SwingUtilities.invokeLater(r);
}
}
回答by Rafcik
You could try to use:
你可以尝试使用:
jFrame.addComponentListener(new ComponentListener() {
// this method invokes each time you resize the frame
public void componentResized(ComponentEvent e) {
// your calculations on buttons
}
});