swing中JPanel如何實現分組框的效果以及設置邊框顏色


代碼如下:

import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class JFrameBackground extends JFrame {
 public JFrameBackground(){
  this.setTitle("我的swing界面"); 
  this.setLayout(new FlowLayout());
  JPanel buttonPanel = new JPanel();
  buttonPanel.setBorder(BorderFactory.createTitledBorder("分組框")); //設置面板邊框,實現分組框的效果,此句代碼為關鍵代碼

  buttonPanel.setBorder(BorderFactory.createLineBorder(Color.red));//設置面板邊框顏色
  JButton button = new JButton("我的按鈕");
  buttonPanel.add(button);
  this.setSize(300, 300);
  this.getContentPane().add(buttonPanel);
  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  this.setVisible(true);
 }
 
 public static void main(String[] args) {
  new JFrameBackground();
 }

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM