我正在尝试在辅助监视器上设置Windows窗体,如下所示:
private void button1_Click(object sender, EventArgs e)
{
MatrixView n = new MatrixView();
Screen[] screens = Screen.AllScreens;
setFormLocation(n, screens[1]);
n.Show();
}
private void setFormLocation(Form form, Screen screen)
{
// first method
Rectangle bounds = screen.Bounds;
form.SetBounds(bounds.X, bounds.Y, bounds.Width, bounds.Height);
// second method
//Point location = screen.Bounds.Location;
//Size size = screen.Bounds.Size;
//form.Left = location.X;
//form.Top = location.Y;
//form.Width = size.Width;
//form.Height = size.Height;
}
Run Code Online (Sandbox Code Playgroud)
边界的属性似乎是正确的,但在我尝试的两种方法中,这最大化了主监视器上的表单.有任何想法吗?
我在setBounds上找不到任何东西,它的用途是什么,或者它的功能实际上是什么.有人能让我对此有所了解吗?非常感谢!
我遇到了这个问题.我有一个JPanel,通常我会像这样创建一个JLabel:
JLabel lblNewLabel = new JLabel("New label");
lblNewLabel.setBounds(0, 0, 135, 14);
panel.add(lblNewLabel);
Run Code Online (Sandbox Code Playgroud)
但我希望每次单击一个按钮,在该面板中创建一个具有相同大小但具有不同高度的新JLabel.我试过了:
panel.add(new JLabel(stringName));
Run Code Online (Sandbox Code Playgroud)
但这样我就无法设定它的界限.我从JTextField获取stringName.
我正在尝试设置地图的界限fitbounds不起作用,因为它在边界周围放置了一些空间
{{{map.fitBounds(map.getBounds())}}}
Run Code Online (Sandbox Code Playgroud)
几次会快速缩小地图
我需要能够做到
{{{map.setBounds(map.getBounds())}}}
Run Code Online (Sandbox Code Playgroud)
而且没有任何事情发生
我注意到一些非常奇怪的事情.我在表单关闭时保持表单的top,left,width和height属性,并使用此信息通过使用以前存储的信息调用SetBounds再次打开表单时恢复表单的最后位置.这很有效,但前提是表单的Position属性在设计时设置为poDefault.如果设置为其他内容,例如poDesigned,poScreenCenter或poMainFormCenter,则SetBounds不会恢复表单的先前位置和大小.
这是奇怪的部分.看似重要的是在设计时将Position属性设置为什么.我可以在运行时将此属性的值更改为poDefault,并且对SetBounds的调用仍然无法正常工作.我尝试过类似下面的内容
if Self.Position <> poDefault then
Self.Position := poDefault;
Run Code Online (Sandbox Code Playgroud)
在表单的OnCreate事件处理程序中,以及从重写的构造函数(并在构造函数中将Position设置为poDefault,并在OnCreate事件处理程序中调用SetBounds).在所有情况下,在运行时将表单的Position属性更改为poDefault并不能解决我在SetBounds中观察到的问题.我发现的唯一一致模式是SetBounds只有在设计时表单的Position属性为poDefault才能正常工作.
当表单的Position属性在设计时未设置为poDefault时,还有其他一些我注意到SetBounds的工作原理.例如,如果调用SetBounds,则在设计时将Position属性设置为poScreenCenter的表单不一定会显示在屏幕的中心.但是,它不会出现在由SetBounds定义的左上角位置,也不会出现在调用SetBounds时指定的宽度和高度.但是,让我重复一遍,我在调用SetBounds之前将表单的Position属性设置为poDefault.我甚至在两个操作之间调用了Application.ProcessMessages,但这并没有解决问题.
我已经在Windows 10上运行Delphi 10.1 Berlin进行了广泛的测试.我还在Windows 7上使用Delphi XE6对其进行了测试.结果相同.
如果您有疑问,请创建一个包含四种表单的VCL应用程序.在第一个表单上放置三个按钮,并为每个按钮添加如下OnClick:
with TForm2.Create(nil) do
try
ShowModal;
finally
Release;
end;
Run Code Online (Sandbox Code Playgroud)
构造函数创建TForm2,然后TForm3和TForm4.
在表单2到4的OnCreate上,添加以下代码:
if Self.Position <> poDefault then
Self.Position := poDefault;
Self.SetBounds(500,500,500,500);
Run Code Online (Sandbox Code Playgroud)
在form2上,将Position设置为poDefault,在form3上将Position设置为poScreenCenter,并在form4上将Position设置为默认值poDefaultPosOnly.只有form2将出现在500,500,宽度为500,高度为500.
有没有人对这个结果有合理的解释?
我知道如何设置边界,所以最后一个新的setbounds()调用会给出新的边界,但是我不知道新边界应该多长/多,这取决于这里的按钮的输入数量例如:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
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.JToggleButton;
import javax.swing.border.EmptyBorder;
public class Book_GUI extends JFrame {
private EconomyClass eco;
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Book_GUI frame = new Book_GUI();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Book_GUI() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); …
Run Code Online (Sandbox Code Playgroud) 我使用以下代码将JTextArea添加到我的JPanel:
commentTextArea.setLineWrap(true);
commentTextArea.setWrapStyleWord(true);
commentTextArea.setVisible(true);
this.add(commentTextArea);
commentTextArea.setBounds(0, 0, 100, 100);
//commentTextArea.setLocation(0, 0);
Run Code Online (Sandbox Code Playgroud)
每当我使用setLocation(0,0)时,JTextArea都不会移动.它始终位于屏幕的顶部中间,而不是(0,0).setBounds(0,0,100,100)也是如此,但高度和宽度都是这样设置的,而不是位置.为什么是这样?
完整代码
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class UMLEditor {
public static void main(String[] args) {
JFrame frame = new UMLWindow();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(30, 30, 1000, 700);
frame.getContentPane().setBackground(Color.white);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
class UMLWindow extends JFrame {
Canvas canvas = new Canvas();
private static final long serialVersionUID = 1L;
public UMLWindow() {
addMenus();
}
public …
Run Code Online (Sandbox Code Playgroud) 我有空面板的代码:
JPanel thePanel = new JPanel();
thePanel.setLayout(null);
Run Code Online (Sandbox Code Playgroud)
我在这里使用setBounds(x,y,width,heigth),fe:
label2.setBounds(150, 220, 459, 311);
Run Code Online (Sandbox Code Playgroud)
我读到这不是一个好习惯,您能告诉我为什么吗?仅仅是因为当您想要在一个组件和另一个组件之间添加某些内容时,必须一次又一次地设置它们的位置还是其他内容?
我非常感谢您的回答,谢谢!;)
我有一个小应用程序,它起到警报系统的作用,我使用一个表单作为警报出现在屏幕上,因为它们更通用和消息框.由于警报的性质,我需要它出现在所有当前连接的监视器的中心.我现在只在主监视器上显示它.
我在这里看了这两个帖子:
但我真的无法理解它,我已经查看了Screens.AllScreens属性,但仍然感觉没有更好的理解如何告诉表单哪个监视器出现,甚至更多地让它出现在多个,因为我假设我需要通过AllScreens数组进行循环.
我还需要关闭其中一个按钮时钟的所有表格,但是现在我只想在所有显示器上显示它们.
很抱歉提出一个我觉得大多数人都认为已经回答的问题.