代碼如下:
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();
}
}