Toon posts:

lege string levert toch een spatie op

Pagina: 1
Acties:

Onderwerpen


Acties:
  • 0 Henk 'm!

Verwijderd

Topicstarter
besten

Ik heb hier een eenvoudig beginners code in java

Het kan niet veel behalve cijfers tonen (in een string) en deze weer clearen of de hele zaak stoppen.

Eigenlijk werkt het wel alleen als ik clear komt er een spatie te staan. Nu wil ik dat java deze telkens verwijderd. Maar heb geen idee hoe! als ik zeg
code:
1
output = "";
(dus zonder spatie verdwijnt mijn hele jlabel... Iemand een idee?

code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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.yellow);

        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) {
            // Object ""string;
            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) 
            output = " ";   
        else if (e.getSource() == bstop) 
            System.exit(0);
            
            l.setText(output);
    }
}

Acties:
  • 0 Henk 'm!

Verwijderd

Ik gok dat je hem nog moet repainten.

Acties:
  • 0 Henk 'm!

  • Herko_ter_Horst
  • Registratie: November 2002
  • Niet online
Nope, de JavaDoc voor JLabel.setText() zegt dat er niets wordt getoond bij een lege string.

Java:
1
2
3
4
5
6
7
8
9
...
  else if (e.getSource() == bclear) 
        output = ""; // dus een lege String
...
  if(output.isEmpty()) {
        l.setText(" ");
  } else {
        l.setText(output);
  }

Dit zou een oplossing kunnen zijn. Je houdt "output" dan correct en je zet alleen een spatie in het label als ie net gecleared is.

PS: je titel klopt niet. Een lege String levert geen spatie op, je hebt een spatie nodig omdat anders je JLabel verdwijnt.

[ Voor 25% gewijzigd door Herko_ter_Horst op 01-03-2011 20:16 ]

"Any sufficiently advanced technology is indistinguishable from magic."


Acties:
  • 0 Henk 'm!

  • Matis
  • Registratie: Januari 2007
  • Laatst online: 20:16

Matis

Rubber Rocket

Ik heb momenteel geen JDK bij de hand, maar kun je ook geen \0 gebruiken binnen je string?

If money talks then I'm a mime
If time is money then I'm out of time


Acties:
  • 0 Henk 'm!

Verwijderd

Je kan natuurlijk om dat probleem te omzeilen ook een non-editable JTextField gebruiken, wat wel overweg kan met lege Strings