trying to size my gui window

Have code but I can't remember how to adjust the panel to fit app

Page 1 of 1

2 Replies - 720 Views - Last Post: 04 October 2010 - 08:06 PM Rate Topic: -----

#1 boosh  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 61
  • Joined: 14-January 10

trying to size my gui window

Posted 04 October 2010 - 07:16 PM

Hello, I am trying to remember how set the size of my japplet so that when it is run it opens in the right size. Here is my code. As a side note I would like to know how to code in the folder or link to a folder. Thanks!

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Mybuttons extends JApplet implements ActionListener
{
	private static final long serialVersionUID = 1L;
	JButton btn1 = new JButton("Open Folder 1");
	JButton btn2 = new JButton("Open Folder 2");
	JButton btn3 = new JButton("Open Folder 3");
	JButton btn4 = new JButton("Open Folder 4");
	JButton btn5 = new JButton("Open Folder 5");
	JButton btn6 = new JButton("Open Folder 6");
	JButton btn7 = new JButton("Open Folder 7");
	JButton btn8 = new JButton("Open Folder 8");
	JButton btn9 = new JButton("Open Folder 9");
	JButton btn10 = new JButton("Open Folder10");
	
	JPanel panel1 = new JPanel(new GridLayout(0,5,5,5));
	JPanel panel2 = new JPanel(new GridLayout(0,5,5,5));
	JPanel panel3 = new JPanel();
	JLabel test = new JLabel("");
	Container con = getContentPane();


	public void init()	
	{
		con.setLayout(new GridLayout(3,0,5,5));
		con.add(panel1);
		panel1.add(btn1);
		panel1.add(btn2);
		panel1.add(btn3);
		panel1.add(btn4);
		panel1.add(btn5);
		con.add(panel2);
		panel2.add(btn6);
		panel2.add(btn7);
		panel2.add(btn8);
		panel2.add(btn9);
		panel2.add(btn10);
		con.add(panel3);
		panel3.add(test); 
		
		btn1.addActionListener(this);
		btn2.addActionListener(this);
		btn3.addActionListener(this);
		btn4.addActionListener(this);
		btn5.addActionListener(this);
		btn6.addActionListener(this);
		btn7.addActionListener(this);
		btn8.addActionListener(this);
		btn9.addActionListener(this);
		btn10.addActionListener(this);
	}
	public void actionPerformed(ActionEvent e)
	{
		Object thisButton = e.getSource();
		

		if (thisButton == btn1)
			test.setText("ButtonFolder 1");
		else {
			if (thisButton == btn2)
				test.setText("Button Folder 2");
		else {
			if (thisButton == btn3)
		        test.setText("Button Folder 3");
		else {
			if (thisButton == btn4)
				test.setText("Button Folder 4");
		else {
			if (thisButton == btn5)
				test.setText("Button Folder 5");
		else {
			if (thisButton == btn6)
				test.setText("Button Folder 6");
		else {
			if (thisButton == btn7)
				test.setText("Button Folder 7");
		else {
			if (thisButton == btn8)
				test.setText("Button Folder 8");
		else {
			if (thisButton == btn9)
				test.setText("Button Folder 9");
		else {
			if (thisButton == btn10)
				test.setText("Button Folder 10");
}
}
}
}
}
}
}
}
}
}
}


This post has been edited by boosh: 04 October 2010 - 07:18 PM


Is This A Good Question/Topic? 0
  • +

Replies To: trying to size my gui window

#2 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon

Reputation: 2965
  • View blog
  • Posts: 11,222
  • Joined: 15-July 08

Re: trying to size my gui window

Posted 04 October 2010 - 07:27 PM

Is your setSize() method set and your HTML specify these things? If you are running it from an IDE, you will often get a different result than if you use a webpage. You should always try the latter.
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: trying to size my gui window

Posted 04 October 2010 - 08:06 PM

You are better to start to learn how to use arrys :)

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Mybuttons extends JApplet implements ActionListener
{
	private static final long serialVersionUID = 1L;
	JButton[] btn = new JButton[10];

	JPanel panel = new JPanel(new GridLayout(2,5,5,5));
	JLabel test = new JLabel("");


	public void init()	
	{
		for(int i = 0; i < btn.length; i++) {
			btn[i] = new JButton("Open Folder " + (i+1));	
			btn[i].addActionListener(this);
			panel.add(btn[i]);
		}
		Container con = getContentPane();
		con.setLayout(new BorderLayout());
		con.add(panel, BorderLayout.CENTER);
		con.add(test, BorderLayout.SOUTH); 
	}
	public void actionPerformed(ActionEvent e)
	{
		Object thisButton = e.getSource();
		for(int i = 0; i < btn.length; i++) {
			if(thisButton == btn[i]) {
				test.setText("Button " + (i+1));
                      break;
			}
		}

	}
}


This post has been edited by pbl: 04 October 2010 - 08:07 PM
Reason for edit:: added the break; statement

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1