gpt4 book ai didi

java - JPanel 填充整个 JFrame

转载 作者:行者123 更新时间:2023-12-01 07:54:49 26 4
gpt4 key购买 nike

我想在 JFrame 的 100,50 位置放置一个大小为 300,200 的 JPanel,但是下面的代码使整个 JFrame 都被 JPanel 填充,我哪里错了?代码:

public class Class1  {

public static void main(String[] args) {

JFrame frame = new JFrame();
JPanel panel = new JPanel();

frame.setSize(700, 500);
panel.setSize(300, 200);
panel.setBackground(Color.green);
panel.setCursor(new Cursor(java.awt.Cursor.HAND_CURSOR));

frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(panel, BorderLayout.CENTER);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
}
}

如果我更改 BorderLayout.SOUTH 或 NORTH,JPanel 看起来就像 JFrame 底部或顶部的一条细线。我也尝试使用首选尺寸,但结果仍然相同。

最佳答案

在尝试使用 Swing 进行任何操作之前,您需要了解布局管理器的基础知识。

在此处学习教程 https://docs.oracle.com/javase/tutorial/uiswing/layout/howLayoutWorks.html

如果您尝试用各种 Swing 控件构造表单,切勿使用绝对坐标来定位它们。调整窗口大小应根据您要使用的布局类型动态地重新定位控件。如果您将面板用作绘图 Canvas (我认为这就是您所追求的),请查看下面的示例。

首先尝试掌握一些基本布局,例如 BorderLayout 和 GridLayout。一旦掌握,您可以使用很少的内容通过将它们嵌套在一起来实现您想要的几乎任何类型的布局。另请学习 ScrollPane 的基础知识,以有效利用屏幕空间

对于您原来的问题,我创建了一个示例,说明 BorderLayout 如何工作以及如何绘制到面板上的特定区域。我还添加了一些代码,以便您可以根据您是否位于给定面板中的较小区域来以不同方式设置光标。

试试这个:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

public class FrameDemo extends JFrame {
private static final long serialVersionUID = 1L;

public FrameDemo() {
super("Frame Demo");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

getContentPane().add(new CustomPanel(Color.RED), BorderLayout.NORTH);
getContentPane().add(new CustomPanel(Color.GREEN), BorderLayout.SOUTH);
getContentPane().add(new CustomPanel(Color.BLUE), BorderLayout.EAST);
getContentPane().add(new CustomPanel(Color.YELLOW), BorderLayout.WEST);
getContentPane().add(new JScrollPane(new CustomPanel(Color.BLACK)), BorderLayout.CENTER);

pack();
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new FrameDemo();
frame.setVisible(true);
}
});
}
}

class CustomPanel extends JPanel implements MouseMotionListener {
private static final long serialVersionUID = 1L;

private static final Dimension PANEL_SIZE = new Dimension(200, 100);
private static final int HAND_CURSOR_INDEX = 1;
private static final int DEFAULT_CURSOR_INDEX = 0;
private static final Cursor[] _cursors = new Cursor[] {
Cursor.getDefaultCursor(),
Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)
};

// I want to place a JPanel with 300,200 size at 100,50 location in a JFrame
private static final Rectangle _rectangle = new Rectangle(50, 10, 40, 50);

public CustomPanel(Color color) {
setBackground(color);
addMouseMotionListener(this);
}

public Dimension getPreferredSize() {
return PANEL_SIZE;
}

public Dimension getMinimumSize() {
return PANEL_SIZE;
}

public Dimension getMaximumSize() {
return PANEL_SIZE;
}


protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.WHITE);
g.fillRect(
(int)_rectangle.getX(),
(int)_rectangle.getY(),
(int)_rectangle.getWidth(),
(int)_rectangle.getHeight());
}

@Override
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub

}

@Override
public void mouseMoved(MouseEvent e) {
int cursorIndex = _rectangle.contains(e.getPoint()) ?
HAND_CURSOR_INDEX :
DEFAULT_CURSOR_INDEX;
setCursor(_cursors[cursorIndex]);
}
}

enter image description here

关于java - JPanel 填充整个 JFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31490273/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com