GUI Programming Using JAVA Language Swing

Keywords: Programming Java Windows

GUI Programming Using JAVA Language Swing

In the GUI design of mine sweeping S project, some problems have been encountered and solved.

  1. How to add components to JDIalog objects
  2. How to customize the location and size of components in containers
  3. How to control the height or width of a block in a container with a layout manager set
  4. How to return an ImageIcon object of specified size
  5. How to set three kinds of buttons (four states of Icon)
  6. How to Set Translucent Window

Note: Most API documents and forums are easy to find.

1. How to add components to JDIalog objects

Components in the dialog JDialog object can only be added by adding a JPanel and a component in the JPanel.

Reference Blog: https://blog.csdn.net/xietansheng/article/details/75948933

JFrame jf=new JFrame();
jf.setSize(1200,960);		
jf.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
JDialog dialog=new JDialog(jf);
dialog.setContentPane(contentPanel);
dialog.setVisible(true)

2. How to customize the location and size of components in containers

If the layout manager used by a container is null, the setBounds(x,y,width,height) function of the component can be used to set the position of the component in the container.

JFrame jf=new JFrame();//
jf.setLayout(null);
jf.setSize(1200,960);		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setResizable(false);
jf.setLocation(343, 36); 
		//Settings without decoration
jf.setUndecorated(true);
//		jf.setBackground(new Color(0,255,0));
//Setting Transparency
jf.setOpacity(0.95f);
jf.add(component);//Add components to it. Require components to use setBounds to specify locations
jf.setVisible(true);

3. How to control the height or width of a block in a container with a layout manager set

If a layout manager is used in a container, but you want to control one of the properties of a component, such as the BorderLayout layout manager, but you want to control the height of a component, such as north or center, then you can use the component's setPreferred Size (preferred Size) function. The parameter is a width and height. Object, the function is used to indicate that the direction fits the layout manager if a value is 0, otherwise it is set to its own size.

Reference Blog: https://codeday.me/bug/20170621/28652.html

JPanel panel=new JPanel();
panel.setPreferredSize(new Dimension(0,250));//The ORTH position of panel in the BorderLayout layout manager in JFrame, which makes the panel 250 pixels in height and width adaptive Layout (in this case, the width of the whole panel)

4. Returns an ImageIcon object of specified size

We can create an ImageIcon object of''Specified Picture Path''first, then get the image object in it, then use the img.getScaledInstance(width, height, hints) of the image object to get an object of the same size (the two objects are not the same object), and then use the setImage of ImageIcon. The function sets the image.

 public ImageIcon getSpcImageIcon(String filename,int d)
{
		ImageIcon icon=new ImageIcon(filename);
		Image img=icon.getImage();
		img=img.getScaledInstance(d, d, Image.SCALE_DEFAULT);
		icon.setImage(img);
		return icon;
}

5. How to set three kinds of buttons (four states of Icon)

There are three main states of the button: general, press and roll over. It can be implemented with setIcon(defaultIcon), setPressedIcon(pressedIcon) and setRollover Icon (rollover Icon).

public ImageIcon getSpcImageIcon(String filename,int d)
{//Returns an image Icon object with a d*d size and a picture path in filename
		ImageIcon icon=new ImageIcon(filename);
		Image img=icon.getImage();
		img=img.getScaledInstance(d, d, Image.SCALE_DEFAULT);
		icon.setImage(img);
		return icon;
}
public JButton setButtonImage(JButton button,int d,String s1,String s2,String s3) {
		//Specify a default button image, Press image, onMouse image,
		button.setIcon(getSpcImageIcon(s1,d));
		button.setPressedIcon(getSpcImageIcon(s2,d));
		button.setRolloverIcon(getSpcImageIcon(s3,d));
//		button.setOpaque(false);
		return button;
	}

But sometimes when the setEnable function fails to set the button, the button will turn grey. We just use the setDisabled Icon (ImageIcon) function of the button to set the failed Icon.

6. How to set translucency for windows

jdk1.7 provides, setOpacity(float f); methods. The frame settings are transparent. By default, however, all components in the container become translucent

Reference Blog: https://blog.csdn.net/JavaBuilt/article/details/79897042

JFrame jf=new JFrame();
jf.setOpacity(0.95f);

Posted by dilum on Wed, 04 Sep 2019 20:46:40 -0700