如何获取 JPanel 的大小?
How to get the size of a JPanel?
这是我的代码:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.TextField;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
class NorthPanel extends JPanel{
public NorthPanel() {
this.setBackground(Color.LIGHT_GRAY); // 배경색 설정
this.add(new JButton("열기"));
this.add(new JButton("닫기"));
this.add(new JButton("나가기"));
}
}
class CenterPanel extends JPanel{
public CenterPanel() {
setLayout(null); // Layout없애줌.(default=bodrderlayout)
for (int i=0; i<10; i++) {
int x = (int)(Math.random()*400); // 랜덤 x좌표 생성
int y = (int)(Math.random()*400); // 랜덤 y좌표 생성
JLabel l = new JLabel("*");
l.setForeground(Color.RED);
l.setLocation(x,y); // 생성한 랜덤좌표로 설정
l.setSize(20,20);
add(l);
}
}
}
class SouthPanel extends JPanel{
public SouthPanel() {
this.setBackground(Color.YELLOW);
add(new JButton("Word Input"));
add(new TextField(20));
}
}
public class Panels extends JFrame {
public Panels(){
this.setTitle("여러 개의 패널을 가진 프레임");
this.setSize(500,500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 창 닫으면 종료되도록
Container c = this.getContentPane(); // 기반이 될 contentpane
c.add(new NorthPanel(), BorderLayout.NORTH);
c.add(new CenterPanel(), BorderLayout.CENTER);
c.add(new SouthPanel(), BorderLayout.SOUTH);
this.setVisible(true); // 화면에 보이게
}
public static void main(String[] args) {
new Panels();
}
}
我想在CenterPanel
中打印随机“*”。
JFrame
是流动的,所以我必须得到 NorthPanel
、SouthPanel
和 ContentPane
的大小,但我不知道如何..
如何获取 CenterPanel
的位置?
int x = (int)(Math.random()*400);
int y = (int)(Math.random()*400);
400 是临时整数,所以我尝试了 getWidth()
但这不起作用。
首先,不要使用TextField
。那是一个 AWT 组件。对于 Swing,您应该使用 JTextField
.
在框架可见之前,Swing 组件没有大小。
Swing 组件负责确定自己的大小。如果您想进行自定义绘画或添加随机组件,那么您应该将组件的 getPreferredSize()
方法覆盖到 return 所需的大小。
@Override
public Dimension getPreferredSize()
{
return new Dimension(400, 400);
}
然后您的构造函数可以调用 getPreferredSize()
方法来获取要在您的随机代码中使用的 width/height。
int x = (int)(Math.random() * getPreferredSize().width); // 랜덤 x좌표 생성
int y = (int)(Math.random() * getPreferredSize().height); // 랜덤 y좌표 생성
然后在使框架可见之前调用框架上的 pack()
,所有组件将以其首选大小显示。
this.pack();
this.setVisible(true);
请注意,您为什么要在随机位置显示标签。我也同意 ControlAltDel 的回答,即自定义绘画是一种更好的方法(比使用标签组件)。但是,我在这里提出的相同建议也适用于那里。也就是说,您将实施 getPreferredSize()
并使用 pack()
.
如果您想使用自定义绘画,那么您将创建一个 ArrayList
对象来绘画。然后 paintComponent() 方法将遍历 ArrayList 以绘制每个对象。有关此方法的示例,请参阅:Custom Painting Approaches。
这是我的代码:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.TextField;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
class NorthPanel extends JPanel{
public NorthPanel() {
this.setBackground(Color.LIGHT_GRAY); // 배경색 설정
this.add(new JButton("열기"));
this.add(new JButton("닫기"));
this.add(new JButton("나가기"));
}
}
class CenterPanel extends JPanel{
public CenterPanel() {
setLayout(null); // Layout없애줌.(default=bodrderlayout)
for (int i=0; i<10; i++) {
int x = (int)(Math.random()*400); // 랜덤 x좌표 생성
int y = (int)(Math.random()*400); // 랜덤 y좌표 생성
JLabel l = new JLabel("*");
l.setForeground(Color.RED);
l.setLocation(x,y); // 생성한 랜덤좌표로 설정
l.setSize(20,20);
add(l);
}
}
}
class SouthPanel extends JPanel{
public SouthPanel() {
this.setBackground(Color.YELLOW);
add(new JButton("Word Input"));
add(new TextField(20));
}
}
public class Panels extends JFrame {
public Panels(){
this.setTitle("여러 개의 패널을 가진 프레임");
this.setSize(500,500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 창 닫으면 종료되도록
Container c = this.getContentPane(); // 기반이 될 contentpane
c.add(new NorthPanel(), BorderLayout.NORTH);
c.add(new CenterPanel(), BorderLayout.CENTER);
c.add(new SouthPanel(), BorderLayout.SOUTH);
this.setVisible(true); // 화면에 보이게
}
public static void main(String[] args) {
new Panels();
}
}
我想在CenterPanel
中打印随机“*”。
JFrame
是流动的,所以我必须得到 NorthPanel
、SouthPanel
和 ContentPane
的大小,但我不知道如何..
如何获取 CenterPanel
的位置?
int x = (int)(Math.random()*400);
int y = (int)(Math.random()*400);
400 是临时整数,所以我尝试了 getWidth()
但这不起作用。
首先,不要使用TextField
。那是一个 AWT 组件。对于 Swing,您应该使用 JTextField
.
在框架可见之前,Swing 组件没有大小。
Swing 组件负责确定自己的大小。如果您想进行自定义绘画或添加随机组件,那么您应该将组件的 getPreferredSize()
方法覆盖到 return 所需的大小。
@Override
public Dimension getPreferredSize()
{
return new Dimension(400, 400);
}
然后您的构造函数可以调用 getPreferredSize()
方法来获取要在您的随机代码中使用的 width/height。
int x = (int)(Math.random() * getPreferredSize().width); // 랜덤 x좌표 생성
int y = (int)(Math.random() * getPreferredSize().height); // 랜덤 y좌표 생성
然后在使框架可见之前调用框架上的 pack()
,所有组件将以其首选大小显示。
this.pack();
this.setVisible(true);
请注意,您为什么要在随机位置显示标签。我也同意 ControlAltDel 的回答,即自定义绘画是一种更好的方法(比使用标签组件)。但是,我在这里提出的相同建议也适用于那里。也就是说,您将实施 getPreferredSize()
并使用 pack()
.
如果您想使用自定义绘画,那么您将创建一个 ArrayList
对象来绘画。然后 paintComponent() 方法将遍历 ArrayList 以绘制每个对象。有关此方法的示例,请参阅:Custom Painting Approaches。