英文:
Rectangle shows up only after resizing on JFrame
问题
我有一些关于Java的经验,但对Swing是新手。我正在尝试运行一个非常简单的示例,但遇到一个我无法解决的烦人问题。
我试图打开一个白色窗口并绘制一个蓝色矩形。不知何故,矩形只在我手动调整窗口大小后才显示出来。我尝试了多种方法,比如取消验证然后验证,更改可见性,但无法让我的矩形显示出来。
这是JFrame和主函数的代码:
public class FieldView extends JFrame {
public FieldView(String name) {
super(name);
getContentPane().setBackground(Color.WHITE);
setSize(480, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.BLUE);
g2d.drawRect(30, 50, 10, 10);
}
}
public class AnimalApplication {
public static void main(String[] args) {
FieldView view = new FieldView("My view");
view.setVisible(true);
}
}
注意:我正在阅读这个教程,并且在提供的代码中遇到了相同的问题。
英文:
I have some experience with Java but I am new with Swing. I am trying to run a very simple example but I run into an annoying problem that I cannot solve.
I am trying to open a white window and draw a blue rectangle. Somehow, the rectangle only shows up after I manually resize the window. I have tried multiple things like unvalidate then validate, changing the visibility, but I cannot get my rectangle to show.
Here is the code of the JFrame and the main function
public class FieldView extends JFrame {
public FieldView(String name) {
super(name);
getContentPane().setBackground(Color.WHITE);
setSize(480, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.BLUE);
g2d.drawRect(30, 50, 10, 10);
}
}
public class AnimalApplication {
public static void main(String[] args) {
FieldView view = new FieldView("My view");
view.setVisible(true);
}
}
Note: I was reading this tutorial and I run into the same problem with the provided code.
答案1
得分: 1
你应该创建一个绘制你所需内容的JPanel。该JPanel应该具有大小首选项。
class DrawPanel extends JPanel{
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.BLUE);
g.drawRect(20, 20, 90, 90);
}
@Override
public Dimension getPreferredSize(){
return new Dimension(480, 200);
}
}
现在我们有一个绘制矩形并具有大小的组件。创建一个JFrame并将其添加到布局中,然后显示该窗口。
JFrame frame = new JFrame("holds my component");
DrawPanel panel = new DrawPanel();
frame.add(panel, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
英文:
You should create a JPanel that draws what you want. That JPanel should have a size preference.
class DrawPanel extends JPanel{
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.BLUE);
g.drawRect(20, 20, 90, 90);
}
@Override
public Dimension getPreferredSize(){
return new Dimension(480, 200);
}
}
Now we have a component that draws a rectangle and has a size. Create a JFrame add it to the layout and display the frame.
JFrame frame = new JFrame("holds my component");
DrawPanel panel = new DrawPanel();
frame.add( panel, BorderLayout.CENTER );
frame.pack();
frame.setVisible(true);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论