set text en grid

Status
Niet open voor verdere reacties.

kompas

Gebruiker
Lid geworden
2 okt 2002
Berichten
341
Ik moet een rekenmachientje maken in java.
De knoppen 0 t/m 9 en stop en clear moet ik maken met een grid.
Dat is mij prima gelukt Alleen heb ik nog geen scherm waar mijn cijfer heen kunnen.
(het hoeft niet te kunnen rekenen, alleen cijfers in het schermpje tonen en dit weer clearen als ik daar op klik)
Maar goed ik heb nu de knoppen maar hoe zal ik daarboven het schermpje toveren?
Iets met containers ofzo..?

Wie kan mij helpen..

PS hoe kan ik java code plaatsen in helpmij.nl???
 
Dit kan je bereiken door een Paneel te maken met die 2 panelen erin.

dus eerst
Code:
JPanel geheelpaneel = new JPanel();

ervan uitgaande dat dit paneel een BorderLayout heeft
Code:
 geheelpaneel.setLayout(new BorderLayout());

word het dan iets als dit:
Code:
geheelPaneel.add(nummerPaneel, BorderLayout.NORTH);
geheelPaneel.add(restPaneel, BorderLayout.CENTER);

*ik ben ervan uit gegaan dat je swing gebruikt. Deze package wordt met java meegeleverd.
*er kunnen fouten in de code zitten
 
ik ben al een stuk verder:

Code:
 import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MyFrame extends JFrame
implements ActionListener {
	JLabel l;
	JButton b1, b2, b3, b4, b5, b6, b7, b8, b9, b0, bclear, bstop;

	public MyFrame(){
		l = new JLabel(" ");
		l.setFont(new Font("times", Font.PLAIN, 66));
		l.setOpaque(true);
		l.setBackground(Color.blue);

		JPanel panel = new JPanel();
		Container c = getContentPane();
		c.add(l, BorderLayout.NORTH);
		c.add(panel, BorderLayout.CENTER);

		b1 = new JButton("1");
		b1.addActionListener(this);

		b2 = new JButton("2");
		b2.addActionListener(this);

		b3 = new JButton("3");
		b3.addActionListener(this);

		b4 = new JButton("4");
		b4.addActionListener(this);

		b5 = new JButton("5");
		b5.addActionListener(this);

		b6 = new JButton("6");
		b6.addActionListener(this);

		b7 = new JButton("7");
		b7.addActionListener(this);

		b8 = new JButton("8");
		b8.addActionListener(this);

		b9 = new JButton("9");
		b9.addActionListener(this);

		b0 = new JButton("0");
		b0.addActionListener(this);

		bclear = new JButton("clear");
		bclear.addActionListener(this);

		bstop = new JButton("stop");
		bstop.addActionListener(this);

		panel.setLayout(new GridLayout(4,1));
		panel.add (b1);
		panel.add (b2);
		panel.add (b3);
		panel.add (b4);
		panel.add (b5);
		panel.add (b6);
		panel.add (b7);
		panel.add (b8);
		panel.add (b9);
		panel.add (bclear);
		panel.add (b0);
		panel.add (bstop);

		setTitle("een rekenmachine");
		setSize(800, 400);
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == b1) 
			l.setText("1");
		if (e.getSource() == b2) 
			l.setText("2");
		if (e.getSource() == b3) 
			l.setText("3");
		if (e.getSource() == b4) 
			l.setText("4");
		if (e.getSource() == b5) 
			l.setText("5");
		if (e.getSource() == b6) 
			l.setText("6");
		if (e.getSource() == b7) 
			l.setText("7");
		if (e.getSource() == b8) 
			l.setText("8");
		if (e.getSource() == b9) 
			l.setText("9");
		if (e.getSource() == b0) 
			l.setText("0");
		if (e.getSource() == bclear) 
			l.setText(" ");
		if (e.getSource() == bstop) 
			l.setText("exit");
	}


}

Maar ik wil meerdere cijfer 'achterelkaar' kunnen zetten. Maar als ik op 2 klik staat er 2 en wanneer ik dan op 4 klik 4. Terwijl ik dan 24 wil zien staan? En de stop doet het nog niet. Iets met EXIT_ON_CLOSE ofzo?
 
probleem 1: als je setText() mag doen kan je meestal ook getText() doen, dus... hint: "a" + "b"
nog een stukje hulp:
PHP:
String str = l.getText();
if (str.equals(" ")) {
      str = "";
}
Puzzel maar ;:)

probleem 2: System.exit(0);
 
probleem 1: als je setText() mag doen kan je meestal ook getText() doen, dus... hint: "a" + "b"
nog een stukje hulp:
PHP:
String str = l.getText();
if (str.equals(" ")) {
      str = "";
}
Puzzel maar ;:)

probleem 2: System.exit(0);

probleem 2 dank
probleem 1 waarom zou ik het zo doen? Krijg ik dan meerder cijfers achter elkaar?
 
ik heb het nou zo gedaan:

Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MyFrame extends JFrame
implements ActionListener {
	JLabel l;
	JButton b1, b2, b3, b4, b5, b6, b7, b8, b9, b0, bclear, bstop;
	String output = "";

	public MyFrame(){
		l = new JLabel(" ");
		l.setFont(new Font("times", Font.PLAIN, 66));
		l.setOpaque(true);
		l.setBackground(Color.blue);

		JPanel panel = new JPanel();
		Container c = getContentPane();
		c.add(l, BorderLayout.NORTH);
		c.add(panel, BorderLayout.CENTER);

		b1 = new JButton("1");
		b1.addActionListener(this);

		b2 = new JButton("2");
		b2.addActionListener(this);

		b3 = new JButton("3");
		b3.addActionListener(this);

		b4 = new JButton("4");
		b4.addActionListener(this);

		b5 = new JButton("5");
		b5.addActionListener(this);

		b6 = new JButton("6");
		b6.addActionListener(this);

		b7 = new JButton("7");
		b7.addActionListener(this);

		b8 = new JButton("8");
		b8.addActionListener(this);

		b9 = new JButton("9");
		b9.addActionListener(this);

		b0 = new JButton("0");
		b0.addActionListener(this);

		bclear = new JButton("clear");
		bclear.addActionListener(this);

		bstop = new JButton("stop");
		bstop.addActionListener(this);

		panel.setLayout(new GridLayout(4,1));
		panel.add (b1);
		panel.add (b2);
		panel.add (b3);
		panel.add (b4);
		panel.add (b5);
		panel.add (b6);
		panel.add (b7);
		panel.add (b8);
		panel.add (b9);
		panel.add (bclear);
		panel.add (b0);
		panel.add (bstop);

		setTitle("een rekenmachine");
		setSize(800, 400);
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == b1) 
			output = output.concat("1");
		if (e.getSource() == b2) 
			output = output.concat("2");
		if (e.getSource() == b3) 
			output = output.concat("3");
		if (e.getSource() == b4) 
			output = output.concat("4");
		if (e.getSource() == b5) 
			output = output.concat("5");
		if (e.getSource() == b6) 
			output = output.concat("6");
		if (e.getSource() == b7) 
			output = output.concat("7");
		if (e.getSource() == b8) 
			output = output.concat("8");
		if (e.getSource() == b9) 
			output = output.concat("9");
		if (e.getSource() == b0) 
			output = output.concat("0");
		if (e.getSource() == bclear) 
			l.setText(" ");
		if (e.getSource() == bstop) 
			System.exit(0);
		l.setText(output);
	}


}

wat dachten jullie hiervan? Kan het eenvoudiger?
 
Code:
public void actionPerformed(ActionEvent e) {
		if (e.getSource() == b1) 
			output = output.concat("1");
		else if (e.getSource() == b2) 
			output = output.concat("2");
		else if (e.getSource() == b3) 
			output = output.concat("3");
		else if (e.getSource() == b4) 
			output = output.concat("4");
		else if (e.getSource() == b5) 
			output = output.concat("5");
		else if (e.getSource() == b6) 
			output = output.concat("6");
		else if (e.getSource() == b7) 
			output = output.concat("7");
		else if (e.getSource() == b8) 
			output = output.concat("8");
		else if (e.getSource() == b9) 
			output = output.concat("9");
		else if (e.getSource() == b0) 
			output = output.concat("0");
		else if (e.getSource() == bclear) 
			l.setText(" ");
		else if (e.getSource() == bstop) 
			System.exit(0);
		l.setText(output);
	}

Dit scheelt een redelijk aantal if loops tijdens het uitvoeren.
 
Code:
 else if (e.getSource() == bclear) 
			l.setText(" ");

De clear knop wil maar niet werken.
Wat doe ik verkeerd!!??
 
kijk eens aan:-)
Code:
else if (e.getSource() == bclear) 
			output = "";

het is gelukt. Alleen wanneer ik nu op clear druk verdwijnt mijn invoerscherm:-(
iemand daar een oplossing voor?
 
het is zo netjes om enkel om op eigen vragen te reageren maar telkens als ik wat oplos komt er een nieuw probleem. Ik hoop dat het geen probleem is?

ik heb nu in plaats van "" " " gedaan. (een spatie ertussen)

Echter nu wil ik dat het programma controleert of er een spatie staat!??
Want door het clear komt er een spatie natuurlijk.
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan