Wątek przeniesiony 2015-08-05 13:06 z Java przez bogdans.

Swing - nie dodaje JButton'ów

0

Cześć, możecie rzucić okiem na kod i zobaczyć co jest skopane ? Nie dodaje mi JButtonów a wydaje się być wszystko ok.

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;


public class Program extends JPanel implements ActionListener
{
	private JButton b1;
	private JButton b2;
	private JButton b3;
	private JButton b4;
	private JButton b5;
	private JButton b6;
	private JButton b7;
	private JButton b8;
	private JButton b9;
	private JButton b0;
	
	
	public Program()
	{
		super();
		
		b1 = new JButton("1");
		b1.setVisible(true);
		b1.addActionListener(this);
		b1.setActionCommand("1");
		
		
		b2 = new JButton("2");
		b2.setVisible(true);
		
		b3 = new JButton("3");
		b3.setVisible(true);
		
		b4 = new JButton("4");
		b4.setVisible(true);
		
		b5 = new JButton("5");
		b5.setVisible(true);
		
		b6 = new JButton("6");
		b6.setVisible(true);
		
		b7 = new JButton("7");
		b7.setVisible(true);
		
		b8 = new JButton("8");
		b8.setVisible(true);
		
		b9 = new JButton("9");
		b9.setVisible(true);
		
		b0 = new JButton("0");
		b0.setVisible(true);
		
		add(b1);
		add(b2);
		add(b3);
		add(b4);
		add(b5);
		add(b6);
		add(b7);
		add(b8);
		add(b9);
		add(b0);
		
	}


	@Override
	public void actionPerformed(ActionEvent e)
	{
		String comand = e.getActionCommand();
		
		
		
	}

}

0

a dodajesz JPanel do JFrame?

edit: poza tym chyba będziesz musiał ustawić layout bo domyślny ustawia wszystkie buttony na sobie i będziesz widział tylko ostatni.

0

Tak dodaję, chodzi o to że żadnego Buttona nie widać :(
Klasa GUI:

import java.awt.FlowLayout;
import java.awt.Panel;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class Main 
{
	public static void createGUI()
	{
		JFrame frame = new JFrame("Kalkulator");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(250, 200);
		
		
		JTextArea text = new JTextArea(1,20);
		text.setVisible(true);
		
		Panel panel = new Panel();
		panel.setVisible(true);
		
		frame.setContentPane(panel);
		
		
		frame.pack();
		frame.setVisible(true);
		frame.setResizable(false);
		
	}

	public static void main(String[] args) 
	{
		javax.swing.SwingUtilities.invokeLater(new Runnable()
		{
			public void run ()
			{
				createGUI();
			}
		}
		);

	}

}

Aaa i setsize() też nie działa, nie bierze pod uwage .

1
public Program()
    {
        super();
        this.setLayout(new FlowLayout());  //wstaw to
        b1 = new JButton("1");
        b1.setVisible(true);
        b1.addActionListener(this);
        b1.setActionCommand("1");
...
1

Nie dodałeś panelu - Program do kontenera Frame. Dodajesz pusty Panel.

1

też racja, a dokładniej:

zamień

Panel panel = new Panel();

na

Program panel = new Program();
1

dziwnie to zrobiłeś jak na mój gust ;P Możliwe jest ze klasa Program nie widzi frame z klasy createGUI.

Spróbuj moze na końcu klasy createGUI dodać:

frame.add(panel);

Jak coś pomyliłem to sorka :D Dokładnie sprawdzę jak będę miał więcej czasu

1

ok poprawiłem ;)

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
 
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextArea;
 
 
public class Program extends JPanel implements ActionListener
{
    private JButton b1;
    private JButton b2;
    private JButton b3;
    private JButton b4;
    private JButton b5;
    private JButton b6;
    private JButton b7;
    private JButton b8;
    private JButton b9;
    private JButton b0;
    private JTextArea text;
 
    public Program()
    {
        super();

        text = new JTextArea(1,20);
        b1 = new JButton("1");
        b1.setVisible(true);
        b1.addActionListener(this);
        b1.setActionCommand("1");
 
        b2 = new JButton("2");
        b3 = new JButton("3");
        b4 = new JButton("4");
        b5 = new JButton("5");
        b6 = new JButton("6");
        b7 = new JButton("7");
        b8 = new JButton("8");
        b9 = new JButton("9");
        b0 = new JButton("0");
     
        add(text);
        add(b1);
        add(b2);
        add(b3);
        add(b4);
        add(b5);
        add(b6);
        add(b7);
        add(b8);
        add(b9);
        add(b0);
 
    }
 
 
    @Override
    public void actionPerformed(ActionEvent e)
    {
        String comand = e.getActionCommand();
 
 
 
    }
 
}

Stwórz klasę do GUI rozszerzając ją JFrame:

public class CreateGUI extends JFrame{
	
	 public CreateGUI()
	    {
	        this.setTitle("Kalkulator");
	        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	        this.setPreferredSize(new Dimension(250,200));
	 

	        Program panel = new Program();
	 
	        this.add(panel);
	 
	        this.pack();
	        this.setVisible(true);
	        this.setResizable(false);
	 
	    }
	 
	    public static void main(String[] args) 
	    {
	       new CreateGUI();
	 
	    }
}
  1. Aby użyć setSize, twoje okno nie może korzystać z LayoutManagera, używaj this.setPreferredSize() i przekaż mu obiekt Dimension(x,y).. gdzie x i y to rozmiar okna
  2. do JFrame nie dodawaj kontrolek, (wcześniej dodałeś JTextArea i przykryłeś to JPanelem
  3. nie musisz ustawiać setVisible na true dla każdej kontrolki.. domyślnie jest ustawiona na true;
3

Omg

private JButton[] digits = new JButton[10];
...
for(int i=0;i<10;i++){
    JButton b = new JButton(""+i);
    //lub (jeśli przycisk "0" ma być na końcu)
    JButton b = new JButton(""+(i+1)%10);
    b.addActionListener(this);
    digits[i] = b;
    add(b);
}
0

Wow, wielkie dzięki ! :)

1 użytkowników online, w tym zalogowanych: 0, gości: 1