GridBagLayout..alle Buttons gleich gross machen...



  • Hi....

    Ich bin grade dabei nen kleinen Taschenrechner mit AWT Gui zu schreiben (gibs zwar schon viele aber ist nur zur Übung) nun hab ich ein Problem mit dem GridBagLayout.
    Und zwar hat mein Taschenrechner 26 Tasten in 4 Reihen angeordnet (die C-Taste geht ueber 2 Reihen und die =-Taste ueber 2 Spalten..deswegen nur 26). Das Problem ist das sich die breite der Buttons im GridBagLayout nach deren Beschriftung richtet... die setSize() Methode zeigt keine Wirkung....dadurch ist die Taste "MR" natuerlich breiter als die Taste "1"... wie bekomm ichs hin das alle Tasten (bis auf C und 🙂 die gleiche Breite haben....????

    Bin fuer jede Hilfe dankbar....

    Bye Darth Vader...

    Hier noch mein Code...

    import java.awt.*;
    import java.awt.event.*;
    
    class FramedPanel extends Panel
    {
    	public FramedPanel()
    	{
    		super();
    	}
    
    	public FramedPanel(LayoutManager layout)
    	{
    		super(layout);
    	}
    
        public void paint(Graphics g)
        {
            super.paint(g);
            g.drawRect(0,0,this.getWidth()-1,this.getHeight()-1);
    
        }
    }
    
    class FramedLabel extends Label
    {
    	public FramedLabel()
    	{
    		super();
    	}
    
    	public FramedLabel(String text)
    	{
    		super(text);
    	}
    
    	public FramedLabel(String text, int alignment)
    	{
    		super(text,alignment);
    	}
    
        public void paint(Graphics g)
        {
            super.paint(g);
            g.drawRect(0,0,this.getWidth()-1,this.getHeight()-1);
    
        }
    }
    
    public class CalculatorGui extends Frame
    {
    	private GridBagLayout keyGrid = new GridBagLayout();
        private GridBagConstraints kGc = new GridBagConstraints();
        private Panel mainPanel = new Panel(keyGrid);
        private Button keyButtons[] = new Button[26];
        private String keyLabels[] = {"MC","7","8","9","/","CE","<-",
        						      "MR","4","5","6","*","%","C",
        						      "MS","1","2","3","-","1/x",
        						      "M+","0","+/-",".","+","="};
    	private FramedLabel calcField = new FramedLabel("0.",Label.RIGHT);
    	private TextArea textArea = new TextArea(7,15);
    	private TextField memoryDisplay = new TextField();
    	private FramedPanel borderPanel = new FramedPanel();
    	private Panel basePanel = new Panel();
    
    	public CalculatorGui()
    	{
    	}
    
    	public CalculatorGui(String title)
    	{
    		super(title);
    		init();
    	}
    
    	private void init()
    	{
    		this.addWindowListener(new CloseMainWindow());
            kGc.insets = new Insets(3, 3, 3, 3);
            kGc.gridx=0;
            kGc.gridy=0;
        	kGc.gridheight=0;
            kGc.fill=kGc.BOTH;
            keyGrid.setConstraints(textArea,kGc);
            textArea.setEnabled(false);
            mainPanel.add(textArea);
    
            kGc.gridx=1;
            kGc.gridwidth=1;
            kGc.gridheight=1;
            keyGrid.setConstraints(memoryDisplay,kGc);
            memoryDisplay.setEnabled(false);
            memoryDisplay.setBackground(Color.lightGray);
            mainPanel.add(memoryDisplay);
    
            kGc.gridx=2;
            kGc.gridwidth=5;
            kGc.gridheight=1;
            keyGrid.setConstraints(calcField,kGc);
            mainPanel.add(calcField);
    
            for (int i=0, x=1, y=1;i<keyButtons.length;i++,x++)
            {
            	kGc.gridx=x;
            	kGc.gridy=y;
            	kGc.gridheight=1;
            	kGc.gridwidth=1;
    
            	if (i==6||i==13||i==19)
            	{
            		x=0;
            		++y;
            	}
    
            	if (i==13)
            	{
            		kGc.gridheight=2;
               	}
    
            	if (i==25)
            	{
            		kGc.gridwidth=2;
            	}
    
            	keyButtons[i]=new Button(keyLabels[i]);
            	keyGrid.setConstraints(keyButtons[i],kGc);
            	mainPanel.add(keyButtons[i]);
            }
    		mainPanel.setBackground(Color.lightGray);
    		borderPanel.setSize(mainPanel.getWidth()+2,mainPanel.getHeight()+2);
    		borderPanel.setBackground(Color.lightGray);
    		borderPanel.add(mainPanel);
    		basePanel.setSize(borderPanel.getWidth(),borderPanel.getHeight());
    		basePanel.add(borderPanel);
    		basePanel.setBackground(Color.lightGray);
    		this.add(basePanel,BorderLayout.CENTER);
    
    		this.setMenuBar(createMenuBar());
    
    		this.setResizable(false);
    		this.pack();
    		this.setLocation(300,250);
    		this.setVisible(true);
    	}
    
    	private MenuBar createMenuBar()
    	{
    		MenuBar menuBar = new MenuBar();
    
    		Menu fileMenu = new Menu("File");
    		Menu editMenu = new Menu("Edit");
    		Menu viewMenu = new Menu("View");
    		Menu aboutMenu = new Menu("About");
    
    		MenuItem exitMenuItem = new MenuItem("Exit");
    		MenuItem copyMenuItem = new MenuItem("Copy");
    		MenuItem pasteMenuItem = new MenuItem("Paste");
    		MenuItem aboutMenuItem = new MenuItem("About this Calculator");
    
    		fileMenu.add(exitMenuItem);
    		editMenu.add(copyMenuItem);
    		editMenu.add(pasteMenuItem);
    		aboutMenu.add(aboutMenuItem);
    
    		menuBar.add(fileMenu);
    		menuBar.add(editMenu);
    		menuBar.add(viewMenu);
    		menuBar.add(aboutMenu);
    
    		return menuBar;
    
    	}
    
    	public static void main(String[] args)
    	{
    		CalculatorGui gui= new CalculatorGui("Calculator");
    	}
    }
    
    class CloseMainWindow extends WindowAdapter
    {
    	public void windowClosing(WindowEvent we)
        {
        	System.exit(0); // Zero indicates success
        }
    
    }
    


  • Wenn deine Englischkenntnisse einigermaßen ausreichend sind, empfehle ich die API - Klasse (wen wunderts) GridBagLayout:

    http://java.sun.com/j2se/1.5.0/docs/api/java/awt/GridBagLayout.html

    Aber mal als Stichwort weightx / weighty
    z.B.

    GridBagLayout gridbag = new GridBagLayout();
    GridBagConstraints c = new GridBagConstraints();
    
    setLayout(gridbag);
    c.fill = GridBagConstraints.BOTH;
    c.weightx = 1.0;
    gridbag.setConstraints(buttonDerEinsGroßIst, c);
    c.gridwith = GridBagConstraints.REMAINDER;
    c.weightx = 2.0;
    gridbag.setConstraints(buttonDerZweiGroßist, c);
    c.weightx = 1.0;
    gridbag.setConstraints(buttonDerEinsGroßist1, c);
    gridbag.setConstraints(buttonDerEinsGroßist2, c);
    gridbag.setConstraints(buttonDerEinsGroßist3, c);
    

    sollte so aussehen:
    |B| |--B--|
    |B| |B| |B|


Anmelden zum Antworten