在这个程序中,创建一个多边形以显示在JPanel
选项卡中。
为了让它显示,我必须重写形状并为它创建一个setter方法。不幸的是,它没有显示,程序也没有运行。
错误:
线程"main"异常java.lang.IllegalArgumentException: adding容器的窗口
,在SelectShape组件1 = new SelectShape(x, y, vert);在方法Page1 .
唯一可行的方法是创建一个框架并删除JTab并将形状分配到框架上,但这不是我想要的。我想做一个程序,可以使用一种图形方法将形状分发到不同的选项卡。
代码如下:
import java.awt.*;
import java.io.IOException;
import javax.swing.*;
/* This program create a graphics component that draws a polygon
*/
public class SelectShape extends JFrame
{
private JTabbedPane tabbedPane;
private JPanel panel1;
// //////////////////////////
static int[] x = { 20, 40, 50, 65, 80, 95 }; // Co-ords for a polygon
static int[] y = { 60, 105, 105, 110, 95, 95 };
static int vert = 6;
public SelectShape() throws IOException // Builds GUI
{
setTitle("Program");
setSize(900, 600);
setBackground(Color.gray);
JPanel topPanel = new JPanel();
topPanel.setLayout(new BorderLayout());
getContentPane().add(topPanel);
// Create the tab pages
createPage1();
// Create a tabbed pane
tabbedPane = new JTabbedPane();
tabbedPane.addTab("Shape Panel", panel1);
}
public void createPage1() throws IOException // Creates JPanel
{
panel1 = new JPanel();
panel1.setLayout(null);
SelectShape component1 = new SelectShape(x, y, vert); //error
SelectShape component2 = new SelectShape(x, y, vert); //over-rides shape
component1.setBounds(290, 70, 120, 40);
component2.setBounds(290, 70, 120, 40);
panel1.add(component1); // is not displayed!
panel1.add(component2); // component2 overwrites component1!!!
panel1.setVisible(true);
}
// overrides javax.swing.JComponent.paintComponent
public void paintComponent(Graphics g) {
// Recover Graphics2D
Graphics2D g2 = (Graphics2D) g;
// Construct a polygon then draw it
Polygon polygon = new Polygon(x, y, vert);
g2.draw(polygon);
g2.fill(polygon);
}
public SelectShape(int[] x, int y[], int vert) { // setter method
this.x = x;
this.y = y;
this.vert = vert;
}
public static void main(String[] args) throws IOException {
SelectShape mainFrame = new SelectShape(); //Frame
mainFrame.setVisible(true);
}
}
我认为你在代码中混合了很多概念,最终导致代码无法理解。
-
JFrame
没有扩展JComponent
,也没有paintComponent
方法。考虑对覆盖另一个方法的方法使用@Override
注释。这会让你很容易犯这样的错误。 - 不需要扩展
JFrame
,也不需要覆盖顶级容器(JDialog
,JFrame
,…)的paint()
方法 - 始终调用
paintXXX
方法的超方法 -
public SelectShape(int[] x, int y[], int vert) { // setter method
不是setter方法。它是一个接受3个参数并赋值的构造函数。在所有情况下,这在您的情况下绝对没有任何作用,因为您将这些变量设置为static
。避免使用static
,除非你描述常量,在这种情况下,它还应该后跟final
关键字。 - 在事件调度线程(EDT)上启动UI,并执行对UI的所有修改。这可以通过使用
SwingUtilities.invokeLater()
轻松完成。 - 你看到的错误:线程"main"异常java.lang.IllegalArgumentException:添加窗口到容器被抛出,因为你试图将
JFrame
添加到JComponent
,这是禁止的。JFrame
不能添加到任何内容中。如果你想这样做,你需要使用JDesktopPane
并添加JInternalFrame
(但这是另一个故事)。
我不太确定你想要实现什么,但这里是一个工作代码,从你的工作派生的更好:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;
/* This program create a graphics component that draws a polygon
*/
public class SelectShape extends JPanel {
// Constants
private static final int[] x = { 20, 40, 50, 65, 80, 95 }; // Co-ords for a polygon
private static final int[] y = { 60, 105, 105, 110, 95, 95 };
private static final Polygon POLYGON = new Polygon(x, y, Math.min(x.length, y.length));
private static final Ellipse2D CIRCLE = new Ellipse2D.Double(100, 40, 45, 45);
// Class variables
private final Shape shape;
private Dimension preferredSize;
public SelectShape(Shape shape) {
this.shape = shape;
Rectangle bounds = shape.getBounds();
this.preferredSize = new Dimension(bounds.x + bounds.width, bounds.y + bounds.height);
}
@Override
public Dimension getPreferredSize() {
return preferredSize;
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g.setColor(Color.BLUE);
g2.draw(shape);
g2.fill(shape);
}
public static void main(String[] args) throws IOException {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame mainFrame = new JFrame("Program");
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
SelectShape polygon = new SelectShape(POLYGON);
SelectShape circle = new SelectShape(CIRCLE);
// Create a tabbed pane
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addTab("Polygon", polygon);
tabbedPane.addTab("Circle", circle);
mainFrame.add(tabbedPane);
mainFrame.pack();
mainFrame.setVisible(true);
}
});
}
}