-
Gui Help
Hi guys
Im designing a GUI for my java and what I have done is added the buttons to the EAST side and a pic to the Centre. I like a border to split the button. Something in between to splits them apart so it looks much nicer. Can anyone suggest something to me and give me a example coding of how it shud be done.
Thanx
-
This is one way. The mainPanel can be substistuted
w. a JFrame (just remember to use the
getContentPane().add() instead of add()): The gridlayout
in the eastPanel is set up w. 0 rows and 1 column, this
makes the components line up in a column like big fat buttons.
Code:
// declare components
JPanel mainPanel = new JPanel();
JPanel eastPanel = new JPanel();
JPanel jPanel2 = new JPanel();
JPanel jPanel3 = new JPanel();
JPanel jPanel4 = new JPanel();
BorderLayout borderLayout1 = new BorderLayout();
GridLayout gridLayout1 = new GridLayout();
GridLayout gridLayout2 = new GridLayout();
GridLayout gridLayout3 = new GridLayout();
GridLayout gridLayout4 = new GridLayout();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JButton jButton3 = new JButton();
// set up gui
private void jbInit() throws Exception {
mainPanel.setLayout(borderLayout1);
eastPanel.setBorder(BorderFactory.createEtchedBorder());
eastPanel.setPreferredSize(new Dimension(90, 10));
eastPanel.setLayout(gridLayout1);
gridLayout1.setColumns(1);
gridLayout1.setRows(0);
jPanel4.setBorder(BorderFactory.createEtchedBorder());
jPanel4.setLayout(gridLayout2);
jPanel3.setBorder(BorderFactory.createEtchedBorder());
jPanel3.setLayout(gridLayout3);
jPanel2.setBorder(BorderFactory.createEtchedBorder());
jPanel2.setLayout(gridLayout4);
jButton1.setText("jButton1");
jButton2.setText("jButton2");
jButton3.setText("jButton3");
mainPanel.add(eastPanel, BorderLayout.WEST);
eastPanel.add(jPanel4, null);
jPanel4.add(jButton1, null);
eastPanel.add(jPanel3, null);
jPanel3.add(jButton2, null);
eastPanel.add(jPanel2, null);
jPanel2.add(jButton3, null);
}
eschew obfuscation
-
Hiyeahh
That code for me was too complexed loll
Heres what I have done at the moment.
I have added the buttons to the west putting the labels inbetween the buttons to give them space in the centre I have added a picture in the centre ..Now thought about button b being used for the border between the buttons and the pic but I tried and I cant do it..I left you my code at the moment which u can mess around with to produce what I what…..
Thank you a lot
public SalesControlFrame() {
setLayout(new BorderLayout());
setBackground(Color.blue);
setTitle("Sales Control & Stock");
setSize(new Dimension(600, 500));
leftPanel = new Panel(new GridLayout(9,2 ));
add(leftPanel, BorderLayout.WEST);
topPanel = new Panel(new GridLayout(2,1 ));
add(topPanel, BorderLayout.NORTH);
//Image Setting
pic = new ImageIcon("G:/img/vz.jpg");
picture1 = new JLabel(pic);
rightPanel = new Panel();
//Setting the image on the right
rightPanel = new Panel(new GridLayout(1,1));
add(rightPanel, BorderLayout.CENTER);
centerPanel = new Panel(new GridLayout(1,1));
add(centerPanel, BorderLayout.EAST);
//Adding New Buttons
viewButton= new Button("Moniter Material");
cuButton= new Button("Customer Order");
spButton = new Button("Reorder From Suppliers");
Exit = new Button("Exit");
b = new Button("");
//Adding labels for space on the buttons
JLabel A = new JLabel("");
JLabel B = new JLabel("");
JLabel C = new JLabel("");
JLabel D = new JLabel("");
JLabel cpLabel = new JLabel("Company Address");
JLabel uslabel = new JLabel( "User Details");
cpLabel.setFont(new Font("Arial", Font.BOLD, 28));
uslabel.setFont(new Font("Arial", Font.BOLD, 28));
leftPanel.add(D);
leftPanel.add(cuButton);
leftPanel.add(A);
leftPanel.add(spButton);
leftPanel.add(B);
leftPanel.add(viewButton);
leftPanel.add(C);
leftPanel.add(Exit);
leftPanel.add(b);
rightPanel.add(picture1);
topPanel.add(cpLabel);
topPanel.add(uslabel);
-
Too complicated, hmm ?
You are using labels as 'bricks' , also you are using a mixture of swing and awt components (JLabel, Panel) ??
Your solution ends up w. buttons that resize as you
resize the frame (and so does my last solution )
and that's not entirely kosher, in fact its a nono in gui
construction.
Try this;
Set the preferred size of the buttonspanel to say 120,10 (the height has no effect as it is stretched by
the borderlayout). Set the buttonspanel layout to:
FlowLayout(FlowLayout.LEFT). Set a fixed width for your
buttons so only one fits on each row. This will make the
buttons align left, stacked above each other and give
some right-space between the buttons and the picture.
And dont use labels for spacing here, use this:
FlowLayout leftPanFlow=new FlowLayout(FlowLayout.LEFT);
leftPanFlow.setVgap(30); //<- gives 30 pixels vertical gap.
leftPanel=new JPanel(leftPanFlow);
this.getContentPane().add(leftPanel,BorderLayout.WEST);
leftPanel.setPreferredSize(new Dimension(120,10));
// add the buttons
leftPanel.add(someButton);
.
.
this.validate();
The buttons should not change position or size as you
resize the frame, and this solution prevents that.
eschew obfuscation
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|