我遇到绘制RectangularShapes或Shapes的问题。我需要在实际的Panel中而不是在Frame中,因为我需要这种灵活性。但我一直陷入困境。我没有接受任何语言的GUI培训,因此非常感谢您的反馈。
代码如下,请帮助我找出为什么这不起作用,以及我如何让它工作。
public class GUI_Test
{
public static void main(String[] args)
{
GUI_Test gui = new GUI_Test();
gui.tryBasic();
}
public void tryBasic()
{
JFrame MyFrame = new JFrame();
MyFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyFrame.setSize(800, 500);
MyFrame.setLayout(new BorderLayout());
// This won't work and can't figure out why!!!!
// The button appears but not the Ellipse.
JPanel JP = new JPanel();
JButton btn3 = new JButton("This is a button");
JP.add(new MyGUIObject(new Ellipse2D.Double(70, 70, 100, 100), Color.red));
JP.add(btn3);
MyFrame.getContentPane().add(JP);
// This works but I need it in a panel for easier control
//MyFrame.add(new MyGUIObject(new Ellipse2D.Double(70, 70, 100, 100), Color.blue));
// This of course works.
//MyFrame.getContentPane().add(new MyGUIObject(new Ellipse2D.Double(70, 70, 100, 100), Color.green));
MyFrame.setVisible(true);
}
public class MyGUIObject extends JPanel
{
protected RectangularShape RecObj;
protected Color myColor;
public MyGUIObject(RectangularShape SetShape, Color setColor)
{
RecObj = SetShape;
myColor = setColor;
}
@Override
protected void paintComponent(Graphics g)
{
Graphics2D g2d = (Graphics2D) g;
g2d.setPaint(myColor);
g2d.fill(RecObj);
}
}
}
答案 0 :(得分:2)
我可以看到三个主要问题。
paintComponent
方法中调用JPanel
,这会导致一些有趣但令人讨厌的油漆文物FlowLayout
默认情况下使用JFrame
,BorderLayout
使用MyGUIObject
,这会更加复杂...... FlowLayout
课程提供任何大小调整提示,因此JPanel
将使用preferredSize
的默认0x0
,即JPanel
当它计算super.paintComponent
。首先,从您的paintComponent
方法中调用getPreferredSize
并覆盖public class MyGUIObject extends JPanel {
protected RectangularShape RecObj;
protected Color myColor;
public MyGUIObject(RectangularShape SetShape, Color setColor) {
RecObj = SetShape;
myColor = setColor;
}
@Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setPaint(myColor);
g2d.fill(RecObj);
}
}
方法
JPanel
接下来,将BorderLayout
正在使用的布局更改为FlowLayout
(虽然默认BorderLayout
可以使用,JPanel JP = new JPanel(new BorderLayout());
JButton btn3 = new JButton("This is a button");
JP.add(new MyGUIObject(new Ellipse2D.Double(70, 70, 100, 100), Color.red));
JP.add(btn3, BorderLayout.SOUTH);
MyFrame.getContentPane().add(JP);
会给你更多控制权)
2&>1
有关详细信息,请查看Painting in AWT and Swing,Performing Custom Painting,Laying Out Components Within a Container和How to Use Borders
答案 1 :(得分:1)
这个怎么样:
public class GUI_Test {
public static void main(String[] args) {
GUI_Test gui = new GUI_Test();
gui.tryBasic();
}
public void tryBasic() {
JFrame frame = createFrame();
JPanel panel = new JPanel(new BorderLayout());
JButton button = new JButton("This is a button");
JPanel buttonPanel = new JPanel(new FlowLayout());
buttonPanel.add(button);
MyGUIObject guiObject = new MyGUIObject(new Ellipse2D.Double(70, 70, 100, 100), Color.red);
panel.add(buttonPanel, BorderLayout.PAGE_START);
panel.add(guiObject,BorderLayout.CENTER);
frame.getContentPane().add(panel, BorderLayout.CENTER);
frame.setVisible(true);
}
private JFrame createFrame() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 500);
frame.setLayout(new BorderLayout());
return frame;
}
public class MyGUIObject extends JPanel {
protected RectangularShape RecObj;
protected Color myColor;
public MyGUIObject(RectangularShape SetShape, Color setColor) {
RecObj = SetShape;
myColor = setColor;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setPaint(myColor);
g2d.fill(RecObj);
}
}
}