I'd like to do something like the following:
public class MyCustomDialog extends JDialog
{
public MyCustomDialog()
{
if (getClientVertScreenSize() < 800)
{
// Set the vertical size as 600, and give them a scroll pane to navigate to the bottom of the gui.
}
else
{
setSize(600, 800); // No need to add a scroll pane.
}
}
}
The trouble is I don't know how to check the clients' screen size, so I don't know how to write the method my constructor relies on.
请参见Toolkit.getDefaultToolkit()。getScreenSize()
What you could do is just put your component in a JScrollPane
and set the horizontal and vertical scroll bar policies to [VERTICAL|HORIZONTAL]_SCROLLBAR_AS_NEEDED
. This way, the scroll bar will only appear when the JScrollPane
's size is smaller than your component's preferred size.
The class java.awt.Toolkit
has an instance method named getScreenSize()
, which returns an object of type java.awt.Dimension
. A Dimension
is an immutable object (it cannot be changed) with two public, final fields: width
and height
. You can obtain an instance of java.awt.Toolkit
and perform your
You should use the following code:
import java.awt.Dimension;
import java.awt.Toolkit;
public class MyCustomDialog extends javax.swing.JDialog {
public MyCustomDialog() {
super(); // Invoke any JDialog initialization code.
Toolkit t = Toolkit.getDefaultToolkit();
if (t.getScreenSize().height < 600) {
setSize(getWidth(), 600);
// Add your scrollpane, etc.
} else {
setSize(600, 800); // No need to add a scroll pane.
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.