- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
因此,我正在为我正在开发的复杂程序编写 GUI,并且我厌倦了尝试让组件在调整窗口大小时正确缩放。
起初我在 jframe 和每个 jpanel 中使用了几个布局来尝试正确放置组件并适本地缩放它们。自然地,我受够了它们,我开始尝试动态缩放和设置组件的 x、y 位置(这要容易得多 :D)。
基本上,我试图将屏幕分成三个部分,左边距 (JSplitPane)、中间 (JTabbedPane) 和右边距 (JSplitPane)。我认为此时内部组件并不重要。 主要问题是尽管我使用 setBounds() 将 x,y 放在右侧并将大小设置为总宽度的 21%,但右侧的 JSplitPane 会在整个窗口上缩放。它似乎与其他面板的交互很奇怪。
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.Dimension;
@SuppressWarnings("deprecation")
public class test extends JFrame implements WindowListener {
/* Constants =========================================================================*/
private final double LEFT_SIZE = .21;
private final double CENTER_SIZE = .58;
private final double RIGHT_SIZE = .21;
private final int TOP_PADDING = 50;
private final int LEFT_PADDING = 4;
private final int RIGHT_PADDING = 4;
private final int BOTTOM_PADDING = 4;
private final int MIN_WIDTH = 640;
private final int MIN_HEIGHT = 480;
public static final String INIT_TITLE = "TestFrame v0.01";
/* End Constants =====================================================================*/
/* Instance Variables ================================================================*/
private int contentWidth;
private int contentHeight;
/* End Instance Variables ============================================================*/
/* Objects ===========================================================================*/
public static test window;
/* Begin Frame Design =========================================================== */
private JSplitPane left;
private JButton button1; private JButton button2;
private JTabbedPane center;
private JPanel panel1; private JPanel panel2;
private JSplitPane right;
private JButton button3; private JButton button4;
/* End Frame Design ============================================================= */
/* End Objects ====================================================================== */
/** Initializes and Places all GUI elements **/
public test ( String windowName ) {
super(windowName); //call parent constructor
this.addWindowListener(this); //adds window event functionality such as close
this.setExtendedState(this.getExtendedState() | JFrame.MAXIMIZED_BOTH); //Starts program maximized
this.setMinimumSize(new Dimension(MIN_WIDTH,MIN_HEIGHT));
this.setVisible(true);
this.setMaximumSize(this.getSize());
/* Begin Init JFrame this ------------------------------------------------------------ */
button1 = new JButton("button1");
button2 = new JButton("button2");
left = new JSplitPane(JSplitPane.VERTICAL_SPLIT, button1, button2);
left.setResizeWeight(1);
button3 = new JButton("button3");
button4 = new JButton("button4");
right = new JSplitPane(JSplitPane.VERTICAL_SPLIT, button3, button4);
right.setResizeWeight(.25);
panel1 = new JPanel();
panel2 = new JPanel();
center = new JTabbedPane();
center.addTab("Panel1", panel1);
center.addTab("Panel2", panel2);
this.add(left);
this.add(center);
this.add(right);
this.addComponentListener(new ComponentListener() {
@Override
public void componentResized (ComponentEvent e) {
window.contentWidth = window.getWidth() - window.LEFT_PADDING - window.RIGHT_PADDING;
window.contentHeight = window.getHeight() - window.TOP_PADDING - window.BOTTOM_PADDING;
window.left.setBounds ( 0, 0, (int)(window.contentWidth * window.LEFT_SIZE), window.contentHeight);
window.center.setBounds ( window.left.getWidth(), 0, (int)(window.contentWidth * window.CENTER_SIZE), window.contentHeight);
window.panel1.setBounds ( 0, 0, (int)(window.contentWidth * window.CENTER_SIZE), window.contentHeight);
window.panel2.setBounds ( 0, 0, (int)(window.contentWidth * window.CENTER_SIZE), window.contentHeight);
window.right.setBounds ( window.left.getWidth() + window.center.getWidth(), 0, (int)(window.contentWidth * window.RIGHT_SIZE), window.contentHeight);
}
public void componentHidden (ComponentEvent e) {}
public void componentMoved (ComponentEvent e) {}
public void componentShown (ComponentEvent e) {}
});
/* End Init JFrame this -------------------------------------------------------------- */
}
// window event abstracts
@Override
public void windowClosing (WindowEvent event) { window.dispose(); System.exit(0); }
public void windowClosed (WindowEvent event) {}
public void windowDeiconified (WindowEvent event) {}
public void windowIconified (WindowEvent event) {}
public void windowActivated (WindowEvent event) {}
public void windowDeactivated (WindowEvent event) {}
public void windowOpened (WindowEvent event) {}
public static void main(String[] args){
window = new test(INIT_TITLE);
window.setVisible(true);
}
}
最佳答案
I don't think the internal components matter at this point.
如 Should I avoid the use of set[Preferred|Maximum|Minimum]Size methods in Java Swing? 中所述, 没有东西会离事实很远。布局的正确使用依赖于组件的首选尺寸。该尺寸是根据内容仔细计算的。如您的示例所示,第二次猜测注定要失败。
相反,添加组件和 pack()
框架。在下面的示例中,中心面板返回一个任意结果以显示 pack()
是如何工作的。
附录:@mKorbel 有益地引用了另外两点:
Swing GUI 对象应该仅在 event dispatch thread 上构造和操作.
另请参阅此 example展示了如何在 invokeLater()
中使用 setDividerLocation()
。
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import java.awt.Dimension;
import java.awt.EventQueue;
public class Test extends JFrame {
public static final String INIT_TITLE = "TestFrame v0.02";
public static Test window;
private JSplitPane left;
private JTabbedPane center;
private JSplitPane right;
public Test(String windowName) {
super(windowName);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
left = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
new JButton("button1"), new JButton("button2"));
left.setResizeWeight(0.5);
right = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
new JButton("button3"), new JButton("button4"));
right.setResizeWeight(0.5);
center = new JTabbedPane();
center.addTab("Panel1", new MyPanel());
center.addTab("Panel2", new MyPanel());
this.add(left, BorderLayout.WEST);
this.add(center, BorderLayout.CENTER);
this.add(right, BorderLayout.EAST);
this.pack();
this.setLocationByPlatform(true);
this.setVisible(true);
}
private static class MyPanel extends JPanel {
private Dimension d = new Dimension(320, 240);
@Override
public Dimension getPreferredSize() {
return d;
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
window = new Test(INIT_TITLE);
window.setVisible(true);
}
});
}
}
关于java - Swing 组件 setSize()/setBounds() 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9117458/
有没有可能,当我们使用Java的setBounds(x,y,width,height)方法时,如果我们改变机器(不同尺寸的显示器或任何其他设备),组件就会从一个地方改变到另一个地方?多种尺寸显示设备)
我想制作 3 个面板,一个在西侧,一个在东侧,一个在南侧。当我执行此操作时,它会给我一个框架,其颜色全部在另一个之上,并且不会给我按钮。 frame = new JFrame(); frame.set
是否可以将一个值放入 setBounds 中以将宽度设置为所需的最小值? JLabel title = new JLabel("Title Text"); title.setBounds(50, 20
如何使用 setBounds 示例获得自动值: myJPanel.setBounds(0, 0, [auto], 300); 我正在使用框布局,并希望自动计算宽度,就像我不调用 setBounds 时
我在将 .setBounds 应用于 JLabel 时遇到问题。它与我之前定义的 JLabel 一起使用,但完全忽略了我的第二个边界定义。我稍微修改了宽度和高度,看看它是否会影响 JLabel,但事实
这是我继承自 JFrame 的一个小类的构造函数: public ToolView(Controller controller) { super("Tools"); setLayout
我正在 Java Swing 中制作一个类似国际象棋的游戏(我知道 Swing 不是正确的方法,但我仍然想尝试一下)我在使棋子以适当的速度显示在显示屏上时遇到问题。由于棋子物理位置的性质,我无法使用布
我正在用java做一个小Gui。我正在使用 setBounds 方法在我的 JFrame 上设置按钮等的位置,但问题是,当我将它与 JPanel 一起使用时,按钮在 JFrame 上不可见,而没有 J
我想在按下按钮时显示标签的移动一段时间。请帮忙。只需告诉我如何在更改 setBounds 期间暂停进程一段时间,在本例中 sleep 方法暂停进程,但仅执行最后一个 setBound 语句: void
您好,我正在使用 setBounds 方法打开一个特定大小的窗口。但是我在参数中传递的大小是窗口的大小,包括框架的条。如何仅为内容设置尺寸? 最佳答案 设置内容的大小,然后在 JFrame 上调用 p
我在包含 2 个 JPanel 的 JFrame 中遇到了一个奇怪的问题。我有一个名为“SlotCheck”的类,它扩展了 JFrame。我有一个名为“Slot”的类,它扩展了 JPanel。在 Sl
是否在事件调度线程上调用了像 JPanel 这样的 Component 的 setBounds() 方法?我问这个问题是因为我正在编写一个程序来绘制平台并使用 Swing Timer 将它们向上移动。
我正在用 Java 做一个小的 Gui。我正在使用 setBounds 方法在我的 JFrame 上设置按钮等的位置,但问题是当我将它与 JPanel 一起使用时,按钮在 JFrame 上不可见,并且
我在 setBounds 上找不到任何内容、它的用途或它的实际功能。有人可以澄清我吗?非常感谢! 最佳答案 如果您将 layout 设置为 null,您可以使用 setBounds(x, y, wid
您好,我正在处理项目中基于 swing 的 gui 部分,我需要将 setBounds JFrame 的方法与 setResizable 调用同步,所以我这样做: setResizable(t
我正在开发一个 Eclipse 插件,其中我想显示一个标签和一个进度条。但不幸的是我无法更改它们的大小。无论我在标签和进度条的 setBounds 属性中提到什么大小,都会给我坐标:矩形 { 3, 3
我得到的 setbounds 值是魔数(Magic Number)。有没有办法将所有值分配给一个变量,而不是为每个值分配一个变量? setBounds(10,0,30,200); 提前致谢。 最佳答案
我读到使用 setBounds() 命令创建 GUI 是不好的。 我知道如何使用布局管理器,但我不得不说它们也有其缺点。 我现在正在编写游戏,有时我需要将组件堆叠在一起。我发现没有办法使用布局管理器来
我正在尝试创建一个包含 3 个文本字段的 JPanel。除了 textArea3 之外,包括按钮在内的其他所有内容都已就位。最后的面板是这样的。正如您在图片中看到的,textArea3 使用整个 JF
getFrame().setBounds(200, 200, 200, 200) 在 netbeans 中不起作用,原因是什么?我也使用了 getFrame().setTitle() 并且它可以工作,
我是一名优秀的程序员,十分优秀!