这是我的编程课,我被迫使用的这种原始方法让我发疯,并且真的很高兴知道我花了很多时间在一些我可能永远不会再使用的东西上 . 请帮我 .
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Problem1 {
JFrame window;
JPanel mainPanel;
JLabel descriptionLabel;
JButton nextButton;
int pageNumber;
public Problem1()
{
gui();
}
public class Rectangle extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
this.setBackground(Color.WHITE);
g.setColor(Color.BLUE);
g.drawRect(120, 25, 350, 280);
}
}
public class FRectangle extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
this.setBackground(Color.WHITE);
g.setColor(Color.BLUE);
g.fillRect(120, 25, 350, 280);
}
}
public void gui()
{
window = new JFrame("Shapeshifter");
window.setVisible(true);
window.setSize(600, 400);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainPanel = new JPanel();
mainPanel.setBackground(Color.WHITE);
window.add(mainPanel,BorderLayout.SOUTH);
descriptionLabel = new JLabel("This is a rectangle.");
mainPanel.add(descriptionLabel);
nextButton = new JButton("Next Shape");
nextButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
pageNumber = pageNumber++;
if(pageNumber == 2)
{
FRectangle DrawFilledRectangle = new FRectangle();
window.add(DrawFilledRectangle);
}
}
});
mainPanel.add(nextButton);
Rectangle DrawRectangle = new Rectangle();
window.add(DrawRectangle);
window.setVisible(true);
pageNumber = 1;
}
public static void main(String[] args)
{
new Problem1();
}
}
我必须制作一个程序,显示大约10种不同的形状,首先显示一个矩形,然后按一下按钮,填充矩形,依此类推 .
所以我想解决这个问题的方法是,因为paintComponent或任何图形对我来说非常混乱,最初将intNumber设置为1并多次制作此代码片段的副本,但更改形状:
public class Rectangle extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
this.setBackground(Color.WHITE);
g.setColor(Color.BLUE);
g.drawRect(120, 25, 350, 280);
}
}
通过这种方式在我的程序上发出可怕的垃圾邮件后,通过使用带有ActionListener的JButton,我将执行 window.remove(DrawRectangle)
然后 window.add(DrawFilledRectangle)
并重复每个计数直到它达到最后一个数字,让's say 10 and if hit again, it' ll遵循相同的提示删除当前形状的JFrame并添加原始DrawRectangle JFrame并允许用户循环 .
但是,按钮's ActionListener isn' t将其作业 window.remove(DrawRectangle)
,因为它无法将其解析为变量 . 我该怎么办?
我觉得很可怜,因为我已经花了3个小时来完成这个部分,还有3个其他问题需要我解决 .
如何有效和干净地制作这个程序?
请帮我程序员领主 .