java - Adjust size of JButtons and window -


the window must contain jpanel contains 8 x 8 array of jbuttons, each size of 69 x 69 pixels , displaying imageicon instead of text. @ bottom of window jlabel, right­aligned jtextfield , 2 more jbuttons shown. window must non­resizable size of 578 x 634 pixels. upon startup, window should show random arrangement of colored buttons shown.

this have far, , appreciated!

import java.awt.*; import javax.swing.*;  public class shinybuttons extends jframe  {     public static byte red = 0;     public static byte orange = 1;     public static byte yellow = 2;     public static byte green = 3;     public static byte blue = 4;     public static byte light_gray = 5;     public static byte dark_gray = 6;      public static byte rows = 8;      private byte[][] buttontable;      public shinybuttons()      {         buttontable = new byte[rows][rows];         resetbuttons();     }      private void resetbuttons()      {         (int r=0; r<rows; r++)             (int c=0; c<rows; c++)                 buttontable[r][c] = (byte)(math.random()*7);     }      public byte getbutton(int r, int c) {         return buttontable[r][c];     }      public shinybuttons (string title) {          super(title); // set title of window          setdefaultcloseoperation(exit_on_close); // allow window close          setsize(900, 300); // set size of window           }      public static imageicon[] icons = {              new imageicon("redbutton.png"),              new imageicon("orangebutton.png"),              new imageicon("yellowbutton.png"),              new imageicon("greenbutton.png"),              new imageicon("bluebutton.png"),              new imageicon("lightgraybutton.png"),              new imageicon("darkgraybutton.png")          };      public static void main(string[] args) {          shinybuttons frame;          frame = new shinybuttons("shiny buttons");  // create window              frame.setvisible(true); // show window          jpanel panel = new jpanel(new gridlayout(8, 8));         imageicon r= new imageicon("redbutton.png");         imageicon o= new imageicon("orangebutton.png");         imageicon y= new imageicon("yellowbutton.png");         imageicon g= new imageicon("greenbutton.png");         imageicon b= new imageicon("bluebutton.png");         imageicon l= new imageicon("lightgraybutton.png");         imageicon d= new imageicon("darkgraybutton.png");          jbutton btn;          btn = new jbutton(o);         btn.setpreferredsize(new dimension(200, 100));         panel.add(btn);          panel.add(new jbutton(r));         panel.add(new jbutton(o));         panel.add(new jbutton(y));         panel.add(new jbutton(g));         panel.add(new jbutton(b));         panel.add(new jbutton(l));         panel.add(new jbutton(d));          frame.add(panel);          frame.setvisible(true);     } } 

1

you can use pack allows frame use contents preferred sizes determine size of frame.

this important, method takes account windows border decorations, setsize doesn't

you should never makes assumptions pixel sizes, when you're dealing fonts, these may rendered @ different sizes on different systems

pixel perfect precision illusion, should focus more on usability , flow , let layout managers jobs...

also note, important make frame unresizable before pack it


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -