Can anyone give me some information what did I do wrong? Program runs but drowing a rectangle or anything else doesnt work. There is just empty space on the middle. I tried open the program from terminal and eclipse on kubuntu 15 and windows. Always with the same result.
I m just starting my adventure with java so please be patient.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyGUI {
JFrame frame;
JLabel label;
public static void main(String[] args)
{
MyGUI gui = new MyGUI();
gui.go();
}
public void go()
{
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton labelButton = new JButton("Change label");
labelButton.addActionListener(new LabelListener());
JButton colorButton = new JButton("Change color");
colorButton.addActionListener(new ColorListener());
label = new JLabel("LABEL");
DrawSmth2 drawPanel = new DrawSmth2();
frame.getContentPane().add(BorderLayout.SOUTH, colorButton);
frame.getContentPane().add(BorderLayout.CENTER, drawPanel);
frame.getContentPane().add(BorderLayout.EAST, labelButton);
frame.getContentPane().add(BorderLayout.WEST, label);
frame.setSize(680,480);
frame.setVisible(true);
}
class LabelListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
label.setText("DONE");
}
}
class ColorListener implements ActionListener
{
public void actionPerformed(ActionEvent e) {
frame.repaint();
}
}
public class DrawSmth2 extends JPanel{
public void PaintComponent(Graphics g)
{
g.setColor(Color.blue);
g.fillRect(0, 0, getWidth(), getHeight());
}
}
}