我必须使用4个按钮编写一个接口。按钮应绘制一个矩形。我已经设置了布局并希望实现“Schwarz”按钮(英文为黑色)在点击后执行动作但它不会做任何事情......它们是否可以让按钮绘制矩形或正方形?
我还尝试将jpanel
添加到我已经实现的面板中,但是将visiblity设置为false。按下按钮后,布尔状态应该改变,但是这也不起作用
...
public class MyPanel extends javax.swing.JPanel {
boolean E = false;
public void paintComponement (Graphics rechtecke) {
if(E == true) {
super.paintComponents(rechtecke);
rechtecke.setColor(Color.black);
rechtecke.fillRect(200, 400, 400, 400);
}
}
public MyPanel(){
setBackground(Color.green);
...
...
public class MyFrame extends javax.swing.JFrame implements ActionListener {
public MyFrame()
{
int a = 1200;
int b = 800;
setSize(a, b);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
foad = new MyPanel();
JButton Entfernen = new JButton("Entfernen");
JButton Bild = new JButton("Bild");
add(foad, BorderLayout.CENTER);
//foad.setBackground(Color.GREEN);;
JButton Rot = new JButton("rot");
JButton Schwarz = new JButton("Schwarz");
Entfernen.addActionListener(this);
Bild.addActionListener(this);
Rot.addActionListener(this);
Schwarz.addActionListener(this);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource()==this.Schwarz) {
foad.E = true;
repaint();}
}
...
我确实得到了按钮和一切的布局。它只是按钮的功能
我找到了。 super.paintcomponent必须在我的if语句之外。