Guten Tag,
ich versuche gerade einen Taschenrechner zu erstellen. Die Oberfläche hab ich schon.
Habe gerade bei jemandem gesehen das die JButtons aufgeteilt wurden Button1,2,3 usw..
Wie kann ich bei mir die Buttons sozusagen vereinzeln?
Vielen Dank im Voraus
ich versuche gerade einen Taschenrechner zu erstellen. Die Oberfläche hab ich schon.
Habe gerade bei jemandem gesehen das die JButtons aufgeteilt wurden Button1,2,3 usw..
Wie kann ich bei mir die Buttons sozusagen vereinzeln?
Java:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
class CalcPanel
{
static void addComponent( Container cont,
GridBagLayout gbl,
Component c,
int x, int y,
int width, int height,
double weightx, double weighty )
{
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = x; gbc.gridy = y;
gbc.gridwidth = width; gbc.gridheight = height;
gbc.weightx = weightx; gbc.weighty = weighty;
gbl.setConstraints( c, gbc );
cont.add( c );
}
public static void main( String[] args )
{
JFrame f = new JFrame();
f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
Container c = f.getContentPane();
GridBagLayout gbl = new GridBagLayout();
c.setLayout( gbl );
// x y w h wx wy
addComponent( c, gbl, new JTextField(), 0, 0, 4, 1, 1.0, 1.0);
addComponent( c, gbl, new JButton("C"), 0, 1, 2, 1, 0 , 0 );
addComponent( c, gbl, new JButton("←"), 2, 1, 1, 1, 0 , 0 );
addComponent( c, gbl, new JButton("÷"), 3, 1, 1, 1, 0 , 0 );
addComponent( c, gbl, new JButton("7"), 0, 3, 1, 1, 0 , 0 );
addComponent( c, gbl, new JButton("4"), 0, 4, 1, 1, 0 , 0 );
addComponent( c, gbl, new JButton("1"), 0, 5, 1, 1, 0 , 0 );
addComponent( c, gbl, new JButton("+/-"), 0, 6, 1, 1, 0, 0 );
addComponent( c, gbl, new JButton("8"), 1, 3, 1, 1, 0 , 0 );
addComponent( c, gbl, new JButton("5"), 1, 4, 1, 1, 0 , 0 );
addComponent( c, gbl, new JButton("2"), 1, 5, 1, 1, 0 , 0 );
addComponent( c, gbl, new JButton("0"), 1, 6, 1, 1, 0 , 0 );
addComponent( c, gbl, new JButton("9"), 2, 3, 1, 1, 0 , 0 );
addComponent( c, gbl, new JButton("6"), 2, 4, 1, 1, 0 , 0 );
addComponent( c, gbl, new JButton("3"), 2, 5, 1, 1, 0 , 0 );
addComponent( c, gbl, new JButton(","), 2, 6, 1, 1, 0 , 0 );
addComponent( c, gbl, new JButton("×"), 3, 3, 1, 1, 0 , 0 );
addComponent( c, gbl, new JButton("-"), 3, 4, 1, 1, 0 , 0 );
addComponent( c, gbl, new JButton("+"), 3, 5, 1, 1, 0 , 0 );
addComponent( c, gbl, new JButton("="), 3, 6, 1, 1, 0 , 0 );
f.setSize( 250, 250);
f.setVisible( true );
}
}
Vielen Dank im Voraus