Tuesday, April 29, 2014

JComponents remained on side when frame size was increased? How to fix it?




Below is a picture of a frame which needs is formed/opens as result of the button-click event of a button. I am copying the code of the entire action listener as well. The problem is that the size of the frame set by setSize(x, y), but the components don't appear accordingly. The size of the frame has increased (by setSize(x, y), i had set a different size earlier which was smaller) and so the components appear at one side on the frame.




Are there any properties or methods which can be used to correct this behavior? I'll be grateful if somebody can guide me about it.




JComponents remained on side when frame size was increased? How to fix it?-9.png






Java Code:






static class btninstr_Action implements ActionListener{
public void actionPerformed(ActionEvent arg0) {
JFrame frame2= new JFrame();
frame2.setVisible(true);
frame2.setSize(500,500);// add custom panel to get perfect work

frame2.setBackground(SystemColor.textInactiveText);
frame2.setLayout(null);

JPanel panel = new JPanel();
panel.setBackground(SystemColor.inactiveCaption);
panel.setBounds(10, 11, 131, 240);
frame2.add(panel);
panel.setLayout(null);

JButton btnNewButton = new JButton("New button");
btnNewButton.setBounds(10, 30, 111, 59);
panel.add(btnNewButton);

JButton btnNewButton_1 = new JButton("New button");
btnNewButton_1.setBounds(10, 100, 111, 59);
panel.add(btnNewButton_1);

JButton btnNewButton_2 = new JButton("New button");
btnNewButton_2.setBounds(10, 170, 111, 59);
panel.add(btnNewButton_2);

JLabel label = new JLabel("New label");
label.setBounds(10, 5, 111, 14);
panel.add(label);

JPanel panel_1 = new JPanel();
panel_1.setBackground(SystemColor.inactiveCaptionBorder);
panel_1.setBounds(151, 11, 228, 240);
frame2.add(panel_1);
panel_1.setLayout(null);

JPanel panel_2 = new JPanel();
panel_2.setBackground(SystemColor.textHighlightText);
panel_2.setBounds(10, 110, 208, 119);
panel_1.add(panel_2);
panel_2.setLayout(null);

JTable table = new JTable();
table.setBounds(200, 112, -194, -99);
panel_2.add(table);

JPanel panel_3 = new JPanel();
panel_3.setBackground(SystemColor.inactiveCaptionBorder);
panel_3.setBounds(136, 11, 82, 88);
panel_1.add(panel_3);

JLabel lblNewLabel_1 = new JLabel("New label");
panel_3.add(lblNewLabel_1);

JPanel panel_4 = new JPanel();
panel_4.setBackground(SystemColor.inactiveCaptionBorder);
panel_4.setBounds(10, 11, 116, 88);
panel_1.add(panel_4);

JLabel lblNewLabel = new JLabel("New label");
panel_4.add(lblNewLabel);





}

}







No comments:

Post a Comment