我正在尝试使用布尔标志基于按钮单击绘制图形。之前,我在ShapeGUI类中将布尔值设置为公共静态布尔类型,并使用Shape.draw在ShapeListener中对其进行了调用,并且可以正常工作,尽管花了大约10秒钟才能生成图形。
现在,我在ShapeListener类本身中设置了布尔标志。现在,它要么不重新粉刷,要么很慢。单击按钮时如何使图形立即显示?
public class ShapeGUI extends JFrame
{
public static final int WIDTH = 500;
public static final int HEIGHT = 500;
public ShapeGUI()
{
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//setLayout(new FlowLayout());
setLocationRelativeTo(null);
JMenuBar menu = new JMenuBar();
JMenu file = new JMenu("File");
menu.add(file);
JMenuItem save = new JMenuItem("Save Information");
JMenuItem exit = new JMenuItem("Exit");
file.add(save);
file.add(exit);
setJMenuBar(menu);
ShapeListener test = new ShapeListener();
JButton button = new JButton("Hello");
test.setBackground(Color.CYAN);
button.addActionListener(new ShapeListener());
test.add(button);
add(test);
//followed this person's advice
//First off, your drawing should be done in a paintComponent method override that is held in a class that extends JPanel. Next, this class could have a boolean variable that the paintComponent uses to decide if it will draw a rectangle or not. The button press simply sets the boolean variable and calls repaint on the drawing JPanel.
}
}
public class ShapeListener extends JPanel implements ActionListener
{
boolean draw = false;
Shape s = new Circle();
Shape a = new Rectangle();
@Override
public void actionPerformed(ActionEvent e)
{
draw = true;
repaint();
}
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
if(draw)
{
s.draw(g);
}
else
{
a.draw(g);
}
}
}
最佳答案
你的逻辑有点不对劲...
ShapeListener test = new ShapeListener(); // First instance
JButton button = new JButton("Hello");
test.setBackground(Color.CYAN);
button.addActionListener(new ShapeListener()); // Second instance...
test.add(button);
add(test);
创建
ShapeListener
类的两个实例,将一个实例添加到容器中,然后将另一个用作动作侦听器。彼此之间不会互相了解,这意味着您可以单击将母牛带回家,ShapeListener
的第一个实例将永远不会改变。另外,除非您打算覆盖
preferredSize
窗格的ShapeListener
方法,否则我将在框架上使用类似BorderLayout
的名称,以确保面板正确布置,否则它将看起来像面板未上漆...尝试类似...
public class ShapeGUI extends JFrame {
public static final int WIDTH = 500;
public static final int HEIGHT = 500;
public ShapeGUI() {
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
JMenuBar menu = new JMenuBar();
JMenu file = new JMenu("File");
menu.add(file);
JMenuItem save = new JMenuItem("Save Information");
JMenuItem exit = new JMenuItem("Exit");
file.add(save);
file.add(exit);
setJMenuBar(menu);
setLayout(new BorderLayout());
ShapeListener test = new ShapeListener();
JButton button = new JButton("Hello");
test.setBackground(Color.CYAN);
button.addActionListener(test);
test.add(button);
add(test);
setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
new ShapeGUI();
}
});
}
public class ShapeListener extends JPanel implements ActionListener {
Shape s = new Ellipse2D.Float(0, 0, 20, 20);
Shape a = new Rectangle2D.Float(0, 0, 20, 20);
private Shape paintMe = a;
@Override
public void actionPerformed(ActionEvent e) {
paintMe = s;
repaint();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
int x = (getWidth() - paintMe.getBounds().width) / 2;
int y = (getHeight() - paintMe.getBounds().height) / 2;
Graphics2D g2d = (Graphics2D) g.create();
g2d.translate(x, y);
g2d.draw(paintMe);
g2d.dispose();
}
}
}
代替...