开发者

Buttons all over the window - Java

开发者 https://www.devze.com 2023-04-02 14:14 出处:网络
I am trying to learn java and i am practicing with a simple program with 2 simple buttons. Here is my code :

I am trying to learn java and i am practicing with a simple program with 2 simple buttons. Here is my code :

 import javax.swing.*;

public class Main {

    public static void main(String[] args) {

    JFrame frame = new JFrame("Askhsh 3");    
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    ColorJPanel application = new ColorJPanel();
    frame.add(application);
    frame.setSize(500,500);
    frame.setVisible(true);

    }

}

And the class ColorJPanel:

import java.awt.*;
import javax.swing.*;


public class ColorJPanel extends JPanel{

    public void paintComponent(Graphics g)
    {
    开发者_如何学JAVAsuper.paintComponent(g);
    this.setBackground(Color.WHITE);

    JButton arxikopoihsh = new JButton("Αρχικοποίκηση");

    JButton klhrwsh = new JButton("Κλήρωση");

    add(arxikopoihsh);
    add(klhrwsh);    

    this.revalidate();
    this.repaint();

    }
}

As you can see the only thing i want to do for now is to place 2 simple buttons that do nothing! Here is my output: http://imageshack.us/photo/my-images/847/efarmogh.jpg/ When i am running the application i am seeing the buttons filling the window! Note that if i remove the "this.revalidate();" command i have to resize the window to see the buttons ! Thanks very much for your time :)


Don't add components in paintComponent. This method is for painting only, not for program logic or to build GUI's. Know that this method gets called many times, often by the JVM and most of the time this is out of your control, and also know that when you ask for it to be called via the repaint() method, this is only a suggestion and the paint manager may sometimes choose to ignore your request. The paintComponent method must be lean and fast as anything that slows it down will slow down the perceived responsiveness of your application.

In your current code, I don't even see a need to have a paintComponent method override, so unless you need it (if doing for instance custom painting of the component), I suggest that you get rid of this method (and the calls to repaint and revalidate). Instead, add your components in the class's constructor and make sure to pack your top level container after adding components and before calling setVisible(true). Most important -- read the Swing tutorials as this is all covered there.

e.g.,

Main.java

import javax.swing.*; 

public class Main { 

  public static void main(String[] args) { 

    JFrame frame = new JFrame("Askhsh 3"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    ColorJPanel application = new ColorJPanel(); 
    frame.add(application); 
    frame.pack();
    frame.setVisible(true); 
  } 
}

ColorJPanel.Java

import java.awt.*; 
import javax.swing.*; 

public class ColorJPanel extends JPanel{ 
  public static final int CJP_WIDTH = 500;
  public static final int CJP_HEIGHT = 500;

  public ColorJPanel()  { 
    this.setBackground(Color.WHITE); 
    JButton arxikopoihsh = new JButton("Αρχικοποίκηση"); 
    JButton klhrwsh = new JButton("Κλήρωση"); 
    add(arxikopoihsh); 
    add(klhrwsh); 
  } 

  // let the component size itself
  public Dimension getPreferredSize() {
    return new Dimension(CJP_WIDTH, CJP_HEIGHT); 
  }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号