下面是我试图在JFrame上绘制一些图形的代码。我试图设置出错的JFrame布局。但是,如果我没有设置布局,代码工作正常,但没有理想的方式。我无法弄清楚问题是什么。请帮忙! =)
import java.awt.*;
import javax.swing.*;
class GuiForJFrame extends JFrame {
private FlowLayout layout;
private Container container;
public GuiForJFrame() {
super("Drawing Graphics");
// Setting the Layout
layout = new FlowLayout(FlowLayout.LEFT);
container = getContentPane();
setLayout(layout);
}
}
class GuiForDrawingGraphics extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(Color.WHITE);
g.setColor(Color.RED);
g.fillRect(25, 25, 150, 50);
g.setColor(new Color(156, 32, 111));
g.fillRect(25, 80, 150, 50);
g.setColor(Color.BLACK);
g.drawString("Drawing Graphics in JAVA", 25, 150);
}
}
public class Application {
public static void main(String[] args) {
// Creating the JFrame object
GuiForJFrame jFrame = new GuiForJFrame();
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Adding Graphics to JFrame
GuiForDrawingGraphics graphics = new GuiForDrawingGraphics();
jFrame.add(graphics);
jFrame.setSize(500, 200);
jFrame.setVisible(true);
}
}
答案 0 :(得分:2)
如另一个答案所述,自定义绘制面板没有固有尺寸。它应返回适合内容的首选大小。然后我们只需要在添加后pack()
帧。
import java.awt.*;
import javax.swing.*;
class GuiForJFrame extends JFrame {
private FlowLayout layout;
private Container container;
public GuiForJFrame() {
super("Drawing Graphics");
// Setting the Layout
layout = new FlowLayout(FlowLayout.LEFT);
container = getContentPane();
setLayout(layout);
}
public static void main(String[] args) {
// Creating the JFrame object
GuiForJFrame jFrame = new GuiForJFrame();
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Adding Graphics to JFrame
GuiForDrawingGraphics graphics = new GuiForDrawingGraphics();
jFrame.add(graphics);
//jFrame.setSize(500, 200);
jFrame.pack();
jFrame.setVisible(true);
}
}
class GuiForDrawingGraphics extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(Color.WHITE);
g.setColor(Color.RED);
g.fillRect(25, 25, 150, 50);
g.setColor(new Color(156, 32, 111));
g.fillRect(25, 80, 150, 50);
g.setColor(Color.BLACK);
g.drawString("Drawing Graphics in JAVA", 25, 150);
}
public Dimension getPreferredSize() {
return new Dimension(300,200);
}
}
答案 1 :(得分:1)
添加jFrame.pack();
和jFrame.validate();
。这会将所有子组件(即jFrame中的组件)以及jFrame本身设置为适当的大小,并确保它们甚至可以显示,并且不会抛出Exception
。
如果你不做其中任何一项,那么什么都不会(而且什么都不应该)出现。
因此,Application
类的代码应如下所示:
public class Application {
public static void main(String[] args) {
// Creating the JFrame object
GuiForJFrame jFrame = new GuiForJFrame();
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Adding Graphics to JFrame
GuiForDrawingGraphics graphics = new GuiForDrawingGraphics();
jFrame.add(graphics);
jFrame.setSize(500, 200);
// Displaying the JFrame properly.
jFrame.pack();
jFrame.validate();
jFrame.setVisible(true);
}
}
除此之外,您还必须使用setPreferredSize(Dimension dimension)
方法指定每个JComponent
所需的尺寸。
查看http://docs.oracle.com/javase/7/docs/api/上的JComponent
和JFrame
课程,了解更多信息。
答案 2 :(得分:1)
默认情况下,JFrame使用BorderLayout,因此当您不指定任何布局时,它会起作用,因为您的面板将被添加到BorderLayout的中心。
但是当你指定我不建议你做的FlowLayout时,因为它会在行上排列所有组件,然后在需要时扩展到多行。而另一件事FlowLayout无法获得您的preferredSize,您需要在自定义面板上提供此尺寸。
流布局允许每个组件采用其自然(首选)大小。
http://docs.oracle.com/javase/7/docs/api/java/awt/FlowLayout.html
所以要么指定面板大小,例如在面板的paintComponent中:
setSize(200, 100); // values depend on your components
或使用任何其他布局,如默认布局(BorderLayout)或BoxLayout等其他类型,它取决于您希望框架看起来像什么
了解更多布局类型: http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html