In der Schule sollen wir die grafische Oberfläche eines Taschenrechners programmieren. Dieser soll genauso aussehen wie der Mac-Taschenrechner von Apple.
Dieser hat abgerundete buttons. :rtfm: im Internet gab es mehrere Lösungsansätze ... aber das Problem ist, dass die Buttons eine andere Farbe haben als der Hintergrund des JPanels. Der gesamte JPanel färbt sich durch die Lösungsvorschläge entweder in die Farbe der Buttons oder die Buttons nehmen die Hintergrundfarbe des JPanels an. Gibt es eine Möglichkeit das zu verhindern? ???:L
[Java]
public class RoundedButton extends JButton {
private static final long serialVersionUID = 9032198251140247116L;
String text;
boolean mouseIn = false;
public RoundedButton(String s) {
super(s);
text = s;
setBorderPainted(true);
setContentAreaFilled(true);
}
@Override
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
if (getModel().isPressed()) {
g.setColor(g.getColor());
g2.fillRect(4, 4, getWidth() - 7, getHeight() - 7);
}
super.paintComponent(g);
if (mouseIn) {
g2.setColor(Color.GRAY);
}
else{
g2.setColor(Color.GRAY);
}
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setStroke(new BasicStroke(1.2f));
g2.draw(new RoundRectangle2D.Double(2, 2, (getWidth() - 4),
(getHeight() - 4), 13, 9));
g2.setStroke(new BasicStroke(2.5f));
g2.drawLine(5, getHeight() - 4, getWidth() - 5, getHeight() - 4);
g2.dispose();
}
public class TaschenrechnerUI{
private JWindow fenster;
private JMenuBar menu;
private JPanel tasten;
private GridBagConstraints c;
private RoundedButton buttonMC;
private RoundedButton buttonMPlus;
private RoundedButton buttonMMinus;
private RoundedButton buttonMR;
private RoundedButton buttonC;
private RoundedButton buttonPlusMinus;
private RoundedButton buttonGeteilt;
private RoundedButton buttonMal;
private RoundedButton buttonSieben;
private RoundedButton buttonAcht;
private RoundedButton buttonNeun;
private RoundedButton buttonMinus;
private RoundedButton buttonVier;
private RoundedButton buttonFuenf;
private RoundedButton buttonSechs;
private RoundedButton buttonPlus;
private RoundedButton buttonEins;
private RoundedButton buttonZwei;
private RoundedButton buttonDrei;
private RoundedButton buttonNull;
private JButton buttonKomma;
private JButton buttonGleich;
private RoundedBorder border;
private GridBagLayout layout;
public TaschenrechnerUI(){
fenster= new JWindow();
tasten= new JPanel(new GridBagLayout());
c= new GridBagConstraints();
layout= new GridBagLayout();
fenster.setBackground(Color.GRAY);
menu= new JMenuBar();
fenster.add(menu);
border= new RoundedBorder();
buttonMC= new RoundedButton("MC");
tasten.setBorder(new LineBorder(Color.GRAY, 10));
buttonMC.setBackground(Color.lightGray);
buttonMPlus= new RoundedButton("M+");
buttonMPlus.setBackground(Color.lightGray);
buttonMMinus= new RoundedButton("M-");
buttonMMinus.setBackground(Color.lightGray);
buttonMR= new RoundedButton("MR");
buttonMR.setBackground(Color.lightGray);
buttonC= new RoundedButton("C");
buttonC.setBackground(Color.lightGray);
buttonPlusMinus= new RoundedButton("±");
buttonPlusMinus.setBackground(Color.lightGray);
buttonGeteilt= new RoundedButton("÷");
buttonGeteilt.setBackground(Color.lightGray);
buttonMal= new RoundedButton("X");
buttonMal.setBackground(Color.lightGray);
buttonSieben= new RoundedButton("7");
buttonSieben.setBackground(Color.lightGray);
buttonAcht= new RoundedButton("8");
buttonAcht.setBackground(Color.lightGray);
buttonNeun= new RoundedButton("9");
buttonNeun.setBackground(Color.lightGray);
buttonMinus= new RoundedButton("-");
buttonMinus.setBackground(Color.lightGray);
buttonVier= new RoundedButton("4");
buttonVier.setBackground(Color.lightGray);
buttonFuenf= new RoundedButton("5");
buttonFuenf.setBackground(Color.lightGray);
buttonSechs= new RoundedButton("6");
buttonSechs.setBackground(Color.lightGray);
buttonPlus= new RoundedButton("+");
buttonPlus.setBackground(Color.lightGray);
buttonEins= new RoundedButton("1");
buttonEins.setBackground(Color.lightGray);
buttonZwei= new RoundedButton("2");
buttonZwei.setBackground(Color.lightGray);
buttonDrei= new RoundedButton("3");
buttonDrei.setBackground(Color.lightGray);
buttonNull= new RoundedButton("0");
buttonNull.setBackground(Color.lightGray);
buttonKomma= new RoundedButton(",");
buttonKomma.setBackground(Color.lightGray);
buttonGleich= new RoundedButton("=");
buttonGleich.setBackground(Color.lightGray);
tasten.setLayout(layout);
tasten.setBackground(Color.GRAY);
tasten.setBackground(Color.GRAY);
c.fill= GridBagConstraints.BOTH;
c.weightx=300;
c.weighty=400;
c.fill= GridBagConstraints.BOTH;
c.gridx=0;
c.gridy=2;
c.gridwidth=1;
c.gridheight=1;
layout.setConstraints(buttonMC, c);
tasten.add(buttonMC, c);
c.fill=GridBagConstraints.BOTH;
c.gridx = 1;
c.gridy = 2;
layout.setConstraints(buttonMPlus, c);
tasten.add(buttonMPlus);
c.fill=GridBagConstraints.BOTH;
c.gridx = 2;
c.gridy = 2;
layout.setConstraints(buttonMMinus, c);
tasten.add(buttonMMinus);
c.fill=GridBagConstraints.BOTH;
c.gridx = 3;
c.gridy = 2;
layout.setConstraints(buttonMR, c);
tasten.add(buttonMR);
c.fill= GridBagConstraints.BOTH;
c.gridx=0;
c.gridy=3;
layout.setConstraints(buttonC, c);
tasten.add(buttonC);
c.fill= GridBagConstraints.BOTH;
c.gridx=1;
c.gridy=3;
layout.setConstraints(buttonPlusMinus, c);
tasten.add(buttonPlusMinus);
c.fill= GridBagConstraints.BOTH;
c.gridx=2;
c.gridy=3;
layout.setConstraints(buttonGeteilt, c);
tasten.add(buttonGeteilt);
c.fill= GridBagConstraints.BOTH;
c.gridx=3;
c.gridy=3;
layout.setConstraints(buttonMal, c);
tasten.add(buttonMal);
c.fill= GridBagConstraints.BOTH;
c.gridx=0;
c.gridy=4;
layout.setConstraints(buttonSieben, c);
tasten.add(buttonSieben);
c.fill= GridBagConstraints.BOTH;
c.gridx=1;
c.gridy=4;
layout.setConstraints(buttonAcht, c);
tasten.add(buttonAcht);
c.fill= GridBagConstraints.BOTH;
c.gridx=2;
c.gridy=4;
layout.setConstraints(buttonNeun, c);
tasten.add(buttonNeun);
c.fill= GridBagConstraints.BOTH;
c.gridx=3;
c.gridy=4;
layout.setConstraints(buttonMinus, c);
tasten.add(buttonMinus);
c.fill=GridBagConstraints.BOTH;
c.gridx=0;
c.gridy=5;
layout.setConstraints(buttonVier, c);
tasten.add(buttonVier);
c.fill=GridBagConstraints.BOTH;
c.gridx=1;
c.gridy=5;
layout.setConstraints(buttonFuenf, c);
tasten.add(buttonFuenf);
c.fill=GridBagConstraints.BOTH;
c.gridx=2;
c.gridy=5;
layout.setConstraints(buttonSechs, c);
tasten.add(buttonSechs);
c.fill=GridBagConstraints.BOTH;
c.gridx=3;
c.gridy=5;
layout.setConstraints(buttonPlus, c);
tasten.add(buttonPlus);
c.fill=GridBagConstraints.BOTH;
c.gridx=0;
c.gridy=6;
layout.setConstraints(buttonEins, c);
tasten.add(buttonEins);
c.fill=GridBagConstraints.BOTH;
c.gridx=1;
c.gridy=6;
layout.setConstraints(buttonZwei, c);
tasten.add(buttonZwei);
c.fill=GridBagConstraints.BOTH;
c.gridx=2;
c.gridy=6;
layout.setConstraints(buttonDrei, c);
tasten.add(buttonDrei);
c.fill=GridBagConstraints.BOTH;
c.gridx=0;
c.gridy=7;
c.gridwidth=2;
layout.setConstraints(buttonNull, c);
tasten.add(buttonNull);
c.fill=GridBagConstraints.BOTH;
c.gridx=2;
c.gridy=7;
c.gridwidth=1;
layout.setConstraints(buttonKomma, c);
tasten.add(buttonKomma);
c.fill=GridBagConstraints.BOTH;
c.gridx=3;
c.gridy=6;
c.gridheight=2;
layout.setConstraints(buttonGleich, c);
tasten.add(buttonGleich);
c.fill=GridBagConstraints.BOTH;
c.gridx=0;
c.gridy=2;
c.gridwidth=4;
c.gridheight=7;
layout.setConstraints(tasten, c);
fenster.add(tasten);
fenster.setVisible(true);
fenster.setSize(300,420);
fenster.setBackground(Color.GRAY);
}
}
[/code]
Mein zweites Problem ist die Länge meines Quellcodes . Gibt es irgendeine Möglichkeit ihn kürzer zu fassen?
Mit lokalen Variablen dürfen wir nicht arbeiten ...
Ich gebe zu ich bin blutiger Anfänger
Wäre super, wenn ihr mir einen Tipp geben könntet. Ich komme jedenfalls nicht mehr weiter ...
Dieser hat abgerundete buttons. :rtfm: im Internet gab es mehrere Lösungsansätze ... aber das Problem ist, dass die Buttons eine andere Farbe haben als der Hintergrund des JPanels. Der gesamte JPanel färbt sich durch die Lösungsvorschläge entweder in die Farbe der Buttons oder die Buttons nehmen die Hintergrundfarbe des JPanels an. Gibt es eine Möglichkeit das zu verhindern? ???:L
[Java]
public class RoundedButton extends JButton {
private static final long serialVersionUID = 9032198251140247116L;
String text;
boolean mouseIn = false;
public RoundedButton(String s) {
super(s);
text = s;
setBorderPainted(true);
setContentAreaFilled(true);
}
@Override
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
if (getModel().isPressed()) {
g.setColor(g.getColor());
g2.fillRect(4, 4, getWidth() - 7, getHeight() - 7);
}
super.paintComponent(g);
if (mouseIn) {
g2.setColor(Color.GRAY);
}
else{
g2.setColor(Color.GRAY);
}
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setStroke(new BasicStroke(1.2f));
g2.draw(new RoundRectangle2D.Double(2, 2, (getWidth() - 4),
(getHeight() - 4), 13, 9));
g2.setStroke(new BasicStroke(2.5f));
g2.drawLine(5, getHeight() - 4, getWidth() - 5, getHeight() - 4);
g2.dispose();
}
public class TaschenrechnerUI{
private JWindow fenster;
private JMenuBar menu;
private JPanel tasten;
private GridBagConstraints c;
private RoundedButton buttonMC;
private RoundedButton buttonMPlus;
private RoundedButton buttonMMinus;
private RoundedButton buttonMR;
private RoundedButton buttonC;
private RoundedButton buttonPlusMinus;
private RoundedButton buttonGeteilt;
private RoundedButton buttonMal;
private RoundedButton buttonSieben;
private RoundedButton buttonAcht;
private RoundedButton buttonNeun;
private RoundedButton buttonMinus;
private RoundedButton buttonVier;
private RoundedButton buttonFuenf;
private RoundedButton buttonSechs;
private RoundedButton buttonPlus;
private RoundedButton buttonEins;
private RoundedButton buttonZwei;
private RoundedButton buttonDrei;
private RoundedButton buttonNull;
private JButton buttonKomma;
private JButton buttonGleich;
private RoundedBorder border;
private GridBagLayout layout;
public TaschenrechnerUI(){
fenster= new JWindow();
tasten= new JPanel(new GridBagLayout());
c= new GridBagConstraints();
layout= new GridBagLayout();
fenster.setBackground(Color.GRAY);
menu= new JMenuBar();
fenster.add(menu);
border= new RoundedBorder();
buttonMC= new RoundedButton("MC");
tasten.setBorder(new LineBorder(Color.GRAY, 10));
buttonMC.setBackground(Color.lightGray);
buttonMPlus= new RoundedButton("M+");
buttonMPlus.setBackground(Color.lightGray);
buttonMMinus= new RoundedButton("M-");
buttonMMinus.setBackground(Color.lightGray);
buttonMR= new RoundedButton("MR");
buttonMR.setBackground(Color.lightGray);
buttonC= new RoundedButton("C");
buttonC.setBackground(Color.lightGray);
buttonPlusMinus= new RoundedButton("±");
buttonPlusMinus.setBackground(Color.lightGray);
buttonGeteilt= new RoundedButton("÷");
buttonGeteilt.setBackground(Color.lightGray);
buttonMal= new RoundedButton("X");
buttonMal.setBackground(Color.lightGray);
buttonSieben= new RoundedButton("7");
buttonSieben.setBackground(Color.lightGray);
buttonAcht= new RoundedButton("8");
buttonAcht.setBackground(Color.lightGray);
buttonNeun= new RoundedButton("9");
buttonNeun.setBackground(Color.lightGray);
buttonMinus= new RoundedButton("-");
buttonMinus.setBackground(Color.lightGray);
buttonVier= new RoundedButton("4");
buttonVier.setBackground(Color.lightGray);
buttonFuenf= new RoundedButton("5");
buttonFuenf.setBackground(Color.lightGray);
buttonSechs= new RoundedButton("6");
buttonSechs.setBackground(Color.lightGray);
buttonPlus= new RoundedButton("+");
buttonPlus.setBackground(Color.lightGray);
buttonEins= new RoundedButton("1");
buttonEins.setBackground(Color.lightGray);
buttonZwei= new RoundedButton("2");
buttonZwei.setBackground(Color.lightGray);
buttonDrei= new RoundedButton("3");
buttonDrei.setBackground(Color.lightGray);
buttonNull= new RoundedButton("0");
buttonNull.setBackground(Color.lightGray);
buttonKomma= new RoundedButton(",");
buttonKomma.setBackground(Color.lightGray);
buttonGleich= new RoundedButton("=");
buttonGleich.setBackground(Color.lightGray);
tasten.setLayout(layout);
tasten.setBackground(Color.GRAY);
tasten.setBackground(Color.GRAY);
c.fill= GridBagConstraints.BOTH;
c.weightx=300;
c.weighty=400;
c.fill= GridBagConstraints.BOTH;
c.gridx=0;
c.gridy=2;
c.gridwidth=1;
c.gridheight=1;
layout.setConstraints(buttonMC, c);
tasten.add(buttonMC, c);
c.fill=GridBagConstraints.BOTH;
c.gridx = 1;
c.gridy = 2;
layout.setConstraints(buttonMPlus, c);
tasten.add(buttonMPlus);
c.fill=GridBagConstraints.BOTH;
c.gridx = 2;
c.gridy = 2;
layout.setConstraints(buttonMMinus, c);
tasten.add(buttonMMinus);
c.fill=GridBagConstraints.BOTH;
c.gridx = 3;
c.gridy = 2;
layout.setConstraints(buttonMR, c);
tasten.add(buttonMR);
c.fill= GridBagConstraints.BOTH;
c.gridx=0;
c.gridy=3;
layout.setConstraints(buttonC, c);
tasten.add(buttonC);
c.fill= GridBagConstraints.BOTH;
c.gridx=1;
c.gridy=3;
layout.setConstraints(buttonPlusMinus, c);
tasten.add(buttonPlusMinus);
c.fill= GridBagConstraints.BOTH;
c.gridx=2;
c.gridy=3;
layout.setConstraints(buttonGeteilt, c);
tasten.add(buttonGeteilt);
c.fill= GridBagConstraints.BOTH;
c.gridx=3;
c.gridy=3;
layout.setConstraints(buttonMal, c);
tasten.add(buttonMal);
c.fill= GridBagConstraints.BOTH;
c.gridx=0;
c.gridy=4;
layout.setConstraints(buttonSieben, c);
tasten.add(buttonSieben);
c.fill= GridBagConstraints.BOTH;
c.gridx=1;
c.gridy=4;
layout.setConstraints(buttonAcht, c);
tasten.add(buttonAcht);
c.fill= GridBagConstraints.BOTH;
c.gridx=2;
c.gridy=4;
layout.setConstraints(buttonNeun, c);
tasten.add(buttonNeun);
c.fill= GridBagConstraints.BOTH;
c.gridx=3;
c.gridy=4;
layout.setConstraints(buttonMinus, c);
tasten.add(buttonMinus);
c.fill=GridBagConstraints.BOTH;
c.gridx=0;
c.gridy=5;
layout.setConstraints(buttonVier, c);
tasten.add(buttonVier);
c.fill=GridBagConstraints.BOTH;
c.gridx=1;
c.gridy=5;
layout.setConstraints(buttonFuenf, c);
tasten.add(buttonFuenf);
c.fill=GridBagConstraints.BOTH;
c.gridx=2;
c.gridy=5;
layout.setConstraints(buttonSechs, c);
tasten.add(buttonSechs);
c.fill=GridBagConstraints.BOTH;
c.gridx=3;
c.gridy=5;
layout.setConstraints(buttonPlus, c);
tasten.add(buttonPlus);
c.fill=GridBagConstraints.BOTH;
c.gridx=0;
c.gridy=6;
layout.setConstraints(buttonEins, c);
tasten.add(buttonEins);
c.fill=GridBagConstraints.BOTH;
c.gridx=1;
c.gridy=6;
layout.setConstraints(buttonZwei, c);
tasten.add(buttonZwei);
c.fill=GridBagConstraints.BOTH;
c.gridx=2;
c.gridy=6;
layout.setConstraints(buttonDrei, c);
tasten.add(buttonDrei);
c.fill=GridBagConstraints.BOTH;
c.gridx=0;
c.gridy=7;
c.gridwidth=2;
layout.setConstraints(buttonNull, c);
tasten.add(buttonNull);
c.fill=GridBagConstraints.BOTH;
c.gridx=2;
c.gridy=7;
c.gridwidth=1;
layout.setConstraints(buttonKomma, c);
tasten.add(buttonKomma);
c.fill=GridBagConstraints.BOTH;
c.gridx=3;
c.gridy=6;
c.gridheight=2;
layout.setConstraints(buttonGleich, c);
tasten.add(buttonGleich);
c.fill=GridBagConstraints.BOTH;
c.gridx=0;
c.gridy=2;
c.gridwidth=4;
c.gridheight=7;
layout.setConstraints(tasten, c);
fenster.add(tasten);
fenster.setVisible(true);
fenster.setSize(300,420);
fenster.setBackground(Color.GRAY);
}
}
[/code]
Mein zweites Problem ist die Länge meines Quellcodes . Gibt es irgendeine Möglichkeit ihn kürzer zu fassen?
Mit lokalen Variablen dürfen wir nicht arbeiten ...
Ich gebe zu ich bin blutiger Anfänger
Wäre super, wenn ihr mir einen Tipp geben könntet. Ich komme jedenfalls nicht mehr weiter ...
Zuletzt bearbeitet: