J.O*_*sen 30 java user-interface swing
我的代码不起作用:
JFrame frame = new JFrame("mull");
mull panel = new mull();
// add panel to the center of window
frame.getContentPane().add("Center", panel);
frame.setSize(500, 300); // << not working!!!
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack(); // give a suitable size to window automatically
frame.setVisible(true); // make window visible
Run Code Online (Sandbox Code Playgroud)
我的窗户越来越小了.怎么解决?
gpr*_*our 56
好吧,你正在使用frame.setSize()和frame.pack().
你应该一次使用其中一个.
使用setSize()您可以给出您想要的框架大小,但如果您使用pack(),它将根据其中组件的大小自动更改框架的大小.它不会考虑您之前提到的尺寸.
尝试frame.pack()从代码中删除或在设置大小之前放置它然后运行它.
大多数布局管理器最适合使用组件的preferredSize,并且大多数GUI最好允许它们包含的组件根据其内容或属性设置自己的preferredSize.要最大限度地利用这些布局管理器,pack()请先调用顶级容器(如JFrame),然后再将其显示,因为这会告诉这些管理员执行操作 - 布局其组件.
通常当我需要在设置我的一个组件的大小时扮演更直接的角色时,我将覆盖getPreferredSize并让它返回一个大于super.preferredSize的Dimension(如果不是,则返回super的值).
例如,这是我为此站点上的另一个问题创建的一个小型拖动矩形应用程序:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MoveRect extends JPanel {
private static final int RECT_W = 90;
private static final int RECT_H = 70;
private static final int PREF_W = 600;
private static final int PREF_H = 300;
private static final Color DRAW_RECT_COLOR = Color.black;
private static final Color DRAG_RECT_COLOR = new Color(180, 200, 255);
private Rectangle rect = new Rectangle(25, 25, RECT_W, RECT_H);
private boolean dragging = false;
private int deltaX = 0;
private int deltaY = 0;
public MoveRect() {
MyMouseAdapter myMouseAdapter = new MyMouseAdapter();
addMouseListener(myMouseAdapter);
addMouseMotionListener(myMouseAdapter);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (rect != null) {
Color c = dragging ? DRAG_RECT_COLOR : DRAW_RECT_COLOR;
g.setColor(c);
Graphics2D g2 = (Graphics2D) g;
g2.draw(rect);
}
}
@Override
public Dimension getPreferredSize() {
return new Dimension(PREF_W, PREF_H);
}
private class MyMouseAdapter extends MouseAdapter {
@Override
public void mousePressed(MouseEvent e) {
Point mousePoint = e.getPoint();
if (rect.contains(mousePoint)) {
dragging = true;
deltaX = rect.x - mousePoint.x;
deltaY = rect.y - mousePoint.y;
}
}
@Override
public void mouseReleased(MouseEvent e) {
dragging = false;
repaint();
}
@Override
public void mouseDragged(MouseEvent e) {
Point p2 = e.getPoint();
if (dragging) {
int x = p2.x + deltaX;
int y = p2.y + deltaY;
rect = new Rectangle(x, y, RECT_W, RECT_H);
MoveRect.this.repaint();
}
}
}
private static void createAndShowGui() {
MoveRect mainPanel = new MoveRect();
JFrame frame = new JFrame("MoveRect");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,我的主类是JPanel,我覆盖了JPanel的getPreferredSize:
public class MoveRect extends JPanel {
//.... deleted constants
private static final int PREF_W = 600;
private static final int PREF_H = 300;
//.... deleted fields and constants
//... deleted methods and constructors
@Override
public Dimension getPreferredSize() {
return new Dimension(PREF_W, PREF_H);
}
Run Code Online (Sandbox Code Playgroud)
另请注意,当我显示GUI时,我将其放入JFrame,调用pack(); JFrame,设置其位置,然后调用setVisible(true);我的JFrame:
private static void createAndShowGui() {
MoveRect mainPanel = new MoveRect();
JFrame frame = new JFrame("MoveRect");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
Run Code Online (Sandbox Code Playgroud)