[英]My JButton should execute and a rectangle should me seen on my Pane. How do i make it visible?
I have to code an interface with 4 Buttons. 我必须使用4个按钮编写一个接口。 The buttons should draw a rectangle.
按钮应绘制一个矩形。 I have set the layout and wanted to implement the action the button "Schwarz" (black in English) does after clicking it but it wont do anything... Are they alternatives of letting a button draw a rectangle or square?
我已经设置了布局并希望实现“Schwarz”按钮(英文为黑色)在点击后执行动作但它不会做任何事情......它们是否可以让按钮绘制矩形或正方形?
I also tried to add a jpanel
to my already implementet panel but to set the visiblity on false. 我还尝试将
jpanel
添加到我已经实现的面板中,但是将visiblity设置为false。 After pressing the butten the boolean state should change but that didint work either 按下按钮后,布尔状态应该改变,但是这也不起作用
... ...
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();}
}
... ...
I do get the layout with the buttons and everything. 我确实得到了按钮和一切的布局。 It is just the functunalitiy of the button
这只是按钮的功能
I found it. 我找到了。 the super.paintcomponent has to be out side of my if satement.
super.paintcomponent必须在我的if语句之外。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.